UNPKG

@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

93 lines 4 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.uploadContractWasm = exports.createStellarAssetContract = exports.createCustomContract = exports.invokeContractFunction = exports.invokeHostFunction = void 0; const xdr_1 = __importDefault(require("../xdr")); const keypair_1 = require("../keypair"); const address_1 = require("../address"); const asset_1 = require("../asset"); function invokeHostFunction(opts) { if (!opts.func) { throw new TypeError(`host function invocation ('func') required (got ${JSON.stringify(opts)})`); } const invokeHostFunctionOp = new xdr_1.default.InvokeHostFunctionOp({ hostFunction: opts.func, auth: opts.auth || [] }); const opAttributes = { body: xdr_1.default.OperationBody.invokeHostFunction(invokeHostFunctionOp) }; this.setSourceAccount(opAttributes, opts); return new xdr_1.default.Operation(opAttributes); } exports.invokeHostFunction = invokeHostFunction; function invokeContractFunction(opts) { const c = new address_1.Address(opts.contract); if (c._type !== 'contract') { throw new TypeError(`expected contract strkey instance, got ${c}`); } return this.invokeHostFunction({ source: opts.source, auth: opts.auth, func: xdr_1.default.HostFunction.hostFunctionTypeInvokeContract(new xdr_1.default.InvokeContractArgs({ contractAddress: c.toScAddress(), functionName: opts.function, args: opts.args })) }); } exports.invokeContractFunction = invokeContractFunction; function createCustomContract(opts) { const salt = Buffer.from(opts.salt || getSalty()); if (!opts.wasmHash || opts.wasmHash.length !== 32) { throw new TypeError(`expected hash(contract WASM) in 'opts.wasmHash', got ${opts.wasmHash}`); } if (salt.length !== 32) { throw new TypeError(`expected 32-byte salt in 'opts.salt', got ${opts.wasmHash}`); } return this.invokeHostFunction({ source: opts.source, auth: opts.auth, func: xdr_1.default.HostFunction.hostFunctionTypeCreateContract(new xdr_1.default.CreateContractArgs({ executable: xdr_1.default.ContractExecutable.contractExecutableWasm(Buffer.from(opts.wasmHash)), contractIdPreimage: xdr_1.default.ContractIdPreimage.contractIdPreimageFromAddress(new xdr_1.default.ContractIdPreimageFromAddress({ address: opts.address.toScAddress(), salt })) })) }); } exports.createCustomContract = createCustomContract; function createStellarAssetContract(opts) { let asset = opts.asset; if (typeof asset === 'string') { const [code, issuer] = asset.split(':'); asset = new asset_1.Asset(code, issuer); } if (!(asset instanceof asset_1.Asset)) { throw new TypeError(`expected Asset in 'opts.asset', got ${asset}`); } return this.invokeHostFunction({ source: opts.source, auth: opts.auth, func: xdr_1.default.HostFunction.hostFunctionTypeCreateContract(new xdr_1.default.CreateContractArgs({ executable: xdr_1.default.ContractExecutable.contractExecutableStellarAsset(), contractIdPreimage: xdr_1.default.ContractIdPreimage.contractIdPreimageFromAsset(asset.toXDRObject()) })) }); } exports.createStellarAssetContract = createStellarAssetContract; function uploadContractWasm(opts) { return this.invokeHostFunction({ source: opts.source, auth: opts.auth, func: xdr_1.default.HostFunction.hostFunctionTypeUploadContractWasm(Buffer.from(opts.wasm)) }); } exports.uploadContractWasm = uploadContractWasm; function getSalty() { return keypair_1.Keypair.random().xdrPublicKey().value(); } //# sourceMappingURL=invoke_host_function.js.map