@okxweb3/coin-stellar
Version:
@ok/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
45 lines • 1.75 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.allowTrust = void 0;
const xdr_1 = __importDefault(require("../xdr"));
const keypair_1 = require("../keypair");
const strkey_1 = require("../strkey");
function allowTrust(opts) {
if (!strkey_1.StrKey.isValidEd25519PublicKey(opts.trustor)) {
throw new Error('trustor is invalid');
}
const attributes = {};
attributes.trustor = keypair_1.Keypair.fromPublicKey(opts.trustor).xdrAccountId();
if (opts.assetCode.length <= 4) {
const code = opts.assetCode.padEnd(4, '\0');
attributes.asset = xdr_1.default.AssetCode.assetTypeCreditAlphanum4(code);
}
else if (opts.assetCode.length <= 12) {
const code = opts.assetCode.padEnd(12, '\0');
attributes.asset = xdr_1.default.AssetCode.assetTypeCreditAlphanum12(code);
}
else {
throw new Error('Asset code must be 12 characters at max.');
}
if (typeof opts.authorize === 'boolean') {
if (opts.authorize) {
attributes.authorize = xdr_1.default.TrustLineFlags.authorizedFlag().value;
}
else {
attributes.authorize = 0;
}
}
else {
attributes.authorize = opts.authorize;
}
const allowTrustOp = new xdr_1.default.AllowTrustOp(attributes);
const opAttributes = {};
opAttributes.body = xdr_1.default.OperationBody.allowTrust(allowTrustOp);
this.setSourceAccount(opAttributes, opts);
return new xdr_1.default.Operation(opAttributes);
}
exports.allowTrust = allowTrust;
//# sourceMappingURL=allow_trust.js.map