openchain-sdk-yxl-ts
Version:
OpenChain SDK for browser
57 lines (47 loc) • 1.53 kB
JavaScript
import protobuf from 'protobufjs';
import is from 'is-type-of';
import long from 'long';
import toUint8Array from '../buffer-utils.js';
import bundleJson from '../../crypto/protobuf/bundle.js';
/**
* createAccount operation: Activate Account
* @param {string} args
* @returns {string}
*/
export default function (args) {
try {
const { sourceAddress, destAddress, initBalance, metadata } = args;
const root = protobuf.Root.fromJSON(bundleJson);
const accountThreshold = root.lookupType('protocol.AccountThreshold');
const accountThresholdMsg = accountThreshold.create({
txThreshold: 1,
});
const accountPrivilege = root.lookupType('protocol.AccountPrivilege');
const accountPrivilegeMsg = accountPrivilege.create({
masterWeight: 1,
thresholds: accountThresholdMsg,
});
const createAccount = root.lookupType('protocol.OperationCreateAccount');
const createAccountMsg = createAccount.create({
destAddress,
initBalance: long.fromValue(initBalance),
priv: accountPrivilegeMsg,
});
const operation = root.lookupType('protocol.Operation');
const payload = {
createAccount: createAccountMsg,
type: operation.Type.CREATE_ACCOUNT,
sourceAddress,
};
if (metadata) {
payload.metadata = toUint8Array(Buffer.from(metadata, 'utf8'));
}
const err = operation.verify(payload);
if (err) {
throw Error(err);
}
return operation.create(payload);
} catch (err) {
throw err;
}
};