@okxweb3/coin-stellar
Version:
@okxweb3/coin-stellar is a Stellar SDK for building Web3 wallets and applications. It supports Stellar and PI blockchains, enabling private key management, address generation, transaction signing, trustline creation, and asset transfers
66 lines • 2.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.walkInvocationTree = exports.buildInvocationTree = void 0;
const asset_1 = require("./asset");
const address_1 = require("./address");
const scval_1 = require("./scval");
function buildInvocationTree(root) {
const fn = root.function();
const output = {};
const inner = fn.value();
switch (fn.switch().value) {
case 0:
output.type = 'execute';
output.args = {
source: address_1.Address.fromScAddress(inner.contractAddress()).toString(),
function: inner.functionName(),
args: inner.args().map((arg) => (0, scval_1.scValToNative)(arg))
};
break;
case 1: {
output.type = 'create';
output.args = {};
const [exec, preimage] = [inner.executable(), inner.contractIdPreimage()];
if (!!exec.switch().value !== !!preimage.switch().value) {
throw new Error(`creation function appears invalid: ${JSON.stringify(inner)} (should be wasm+address or token+asset)`);
}
switch (exec.switch().value) {
case 0: {
const details = preimage.fromAddress();
output.args.type = 'wasm';
output.args.wasm = {
salt: details.salt().toString('hex'),
hash: exec.wasmHash().toString('hex'),
address: address_1.Address.fromScAddress(details.address()).toString()
};
break;
}
case 1:
output.args.type = 'sac';
output.args.asset = asset_1.Asset.fromOperation(preimage.fromAsset()).toString();
break;
default:
throw new Error(`unknown creation type: ${JSON.stringify(exec)}`);
}
break;
}
default:
throw new Error(`unknown invocation type (${fn.switch()}): ${JSON.stringify(fn)}`);
}
output.invocations = root.subInvocations().map((i) => buildInvocationTree(i));
return output;
}
exports.buildInvocationTree = buildInvocationTree;
function walkInvocationTree(root, callback) {
walkHelper(root, 1, callback);
}
exports.walkInvocationTree = walkInvocationTree;
function walkHelper(node, depth, callback, parent) {
if (callback(node, depth, parent) === false) {
return;
}
node
.subInvocations()
.forEach((i) => walkHelper(i, depth + 1, callback, node));
}
//# sourceMappingURL=invocation.js.map