FioJS is a Utility SDK for packing, signing and encryption functionality for the FIO blockchain. It is used by the FIO TypeScript SDK
npm install @shapeshiftoss/fiojsFor information on FIO, visit the FIO website.
For information on the FIO Chain, API, and SDKs visit the FIO Protocol Developer Hub.
// Include textDecoder and textEncoder when using in Node, React Native, IE11 or Edge Browsers.
const { TextEncoder, TextDecoder } = require('util'); // node only; native TextEncoder/Decoder
const { TextEncoder, TextDecoder } = require('text-encoding'); // React Native, IE11, and Edge Browsers only
npm run testFio JS Library instance:info = await rpc.get_info();
blockInfo = await rpc.get_block(info.last_irreversible_block_num);
currentDate = new Date();
timePlusTen = currentDate.getTime() + 10000;
timeInISOString = (new Date(timePlusTen)).toISOString();
expiration = timeInISOString.substr(0, timeInISOString.length - 1);
// hash the public key of the payer/sender
const actorAccountHash = Fio.accountHash('FIO7bxrQUTbQ4mqcoefhWPz1aFieN4fA9RQAiozRz7FrUChHZ7Rb8');
expect(accountHash).toEqual('5kmx4qbqlpld');
// sending 1 FIO token using the trnsfiopubkey ACTION (1 FIO token = 1,000,000,000 SUFs)
transaction = {
expiration,
ref_block_num: blockInfo.block_num & 0xffff,
ref_block_prefix: blockInfo.ref_block_prefix,
actions: [{
account: 'fio.token',
name: 'trnsfiopubky',
authorization: [{
actor: actorAccountHash,
permission: 'active',
}],
data: {
payeePublicKey: 'FIO5VE6Dgy9FUmd1mFotXwF88HkQN1KysCWLPqpVnDMjRvGRi1YrM',
amount: '1000000000',
maxFee: 200000000,
technologyProviderId: ''
},
}]
};
abiMap = new Map()
tokenRawAbi = await rpc.get_raw_abi('fio.token')
abiMap.set('fio.token', tokenRawAbi)
tx = await Fio.prepareTransaction({transaction, chainId, privateKeys, abiMap,
textDecoder: new TextDecoder(), textEncoder: new TextEncoder()});
pushResult = await fetch(httpEndpoint + '/v1/chain/push_transaction', {
body: JSON.stringify(tx),
method: 'POST',
});
json = await pushResult.json()
if (json.processed && json.processed.except) {
throw new RpcError(json);
}
expect(Object.keys(json)).toContain('transaction_id');
const accountHash = Fio.accountHash('FIO7bxrQUTbQ4mqcoefhWPz1aFieN4fA9RQAiozRz7FrUChHZ7Rb8');
expect(accountHash).toEqual('5kmx4qbqlpld');
new_funds_request there is a content field. The content field is encrypted by Alice and decrypted by Bob.newFundsContent = {
payee_public_address: 'purse@alice',
amount: '1.75',
chain_code: 'FIO',
token_code: 'FIO',
memo: null,
hash: null,
offline_url: null
}
privateKeyAlice = '5J9bWm2ThenDm3tjvmUgHtWCVMUdjRR1pxnRtnJjvKA4b2ut5WK';
publicKeyAlice = 'FIO7zsqi7QUAjTAdyynd6DVe8uv4K8gCTRHnAoMN9w9CA1xLCTDVv';
privateKeyBob = '5JoQtsKQuH8hC9MyvfJAqo6qmKLm8ePYNucs7tPu2YxG12trzBt';
publicKeyBob = 'FIO5VE6Dgy9FUmd1mFotXwF88HkQN1KysCWLPqpVnDMjRvGRi1YrM';
cipherAlice = Fio.createSharedCipher({privateKey: privateKeyAlice, publicKey: publicKeyBob, textEncoder: new TextEncoder(), textDecoder: new TextDecoder()});
cipherAliceHex = cipherAlice.encrypt('new_funds_content', newFundsContent);
// Alice sends cipherAliceHex to Bob via new_funds_request
cipherBob = Fio.createSharedCipher({privateKey: privateKeyBob, publicKey: publicKeyAlice, textEncoder: new TextEncoder(), textDecoder: new TextDecoder()});
newFundsContentBob = cipherBob.decrypt('new_funds_content', cipherAliceHex);
expect(newFundsContentBob).toEqual(newFundsContent);
See src/encryption-fio.abi.json for other message types like new_funds_content.
docs/message_encryption.md