@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
89 lines • 4.57 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setOptions = void 0;
const xdr_1 = __importDefault(require("../xdr"));
const keypair_1 = require("../keypair");
const strkey_1 = require("../strkey");
function weightCheckFunction(value, name) {
if (value >= 0 && value <= 255) {
return true;
}
throw new Error(`${name} value must be between 0 and 255`);
}
function setOptions(opts) {
const attributes = {};
if (opts.inflationDest) {
if (!strkey_1.StrKey.isValidEd25519PublicKey(opts.inflationDest)) {
throw new Error('inflationDest is invalid');
}
attributes.inflationDest = keypair_1.Keypair.fromPublicKey(opts.inflationDest).xdrAccountId();
}
attributes.clearFlags = this._checkUnsignedIntValue('clearFlags', opts.clearFlags);
attributes.setFlags = this._checkUnsignedIntValue('setFlags', opts.setFlags);
attributes.masterWeight = this._checkUnsignedIntValue('masterWeight', opts.masterWeight, weightCheckFunction);
attributes.lowThreshold = this._checkUnsignedIntValue('lowThreshold', opts.lowThreshold, weightCheckFunction);
attributes.medThreshold = this._checkUnsignedIntValue('medThreshold', opts.medThreshold, weightCheckFunction);
attributes.highThreshold = this._checkUnsignedIntValue('highThreshold', opts.highThreshold, weightCheckFunction);
if (opts.homeDomain !== undefined && typeof opts.homeDomain !== 'string') {
throw new TypeError('homeDomain argument must be of type String');
}
attributes.homeDomain = opts.homeDomain;
if (opts.signer) {
const weight = this._checkUnsignedIntValue('signer.weight', opts.signer.weight, weightCheckFunction);
let key;
let setValues = 0;
if (opts.signer.ed25519PublicKey) {
if (!strkey_1.StrKey.isValidEd25519PublicKey(opts.signer.ed25519PublicKey)) {
throw new Error('signer.ed25519PublicKey is invalid.');
}
const rawKey = strkey_1.StrKey.decodeEd25519PublicKey(opts.signer.ed25519PublicKey);
key = new xdr_1.default.SignerKey.signerKeyTypeEd25519(rawKey);
setValues += 1;
}
if (opts.signer.preAuthTx) {
if (typeof opts.signer.preAuthTx === 'string') {
opts.signer.preAuthTx = Buffer.from(opts.signer.preAuthTx, 'hex');
}
if (!(Buffer.isBuffer(opts.signer.preAuthTx) &&
opts.signer.preAuthTx.length === 32)) {
throw new Error('signer.preAuthTx must be 32 bytes Buffer.');
}
key = new xdr_1.default.SignerKey.signerKeyTypePreAuthTx(opts.signer.preAuthTx);
setValues += 1;
}
if (opts.signer.sha256Hash) {
if (typeof opts.signer.sha256Hash === 'string') {
opts.signer.sha256Hash = Buffer.from(opts.signer.sha256Hash, 'hex');
}
if (!(Buffer.isBuffer(opts.signer.sha256Hash) &&
opts.signer.sha256Hash.length === 32)) {
throw new Error('signer.sha256Hash must be 32 bytes Buffer.');
}
key = new xdr_1.default.SignerKey.signerKeyTypeHashX(opts.signer.sha256Hash);
setValues += 1;
}
if (opts.signer.ed25519SignedPayload) {
if (!strkey_1.StrKey.isValidSignedPayload(opts.signer.ed25519SignedPayload)) {
throw new Error('signer.ed25519SignedPayload is invalid.');
}
const rawKey = strkey_1.StrKey.decodeSignedPayload(opts.signer.ed25519SignedPayload);
const signedPayloadXdr = xdr_1.default.SignerKeyEd25519SignedPayload.fromXDR(rawKey);
key = xdr_1.default.SignerKey.signerKeyTypeEd25519SignedPayload(signedPayloadXdr);
setValues += 1;
}
if (setValues !== 1) {
throw new Error('Signer object must contain exactly one of signer.ed25519PublicKey, signer.sha256Hash, signer.preAuthTx.');
}
attributes.signer = new xdr_1.default.Signer({ key, weight });
}
const setOptionsOp = new xdr_1.default.SetOptionsOp(attributes);
const opAttributes = {};
opAttributes.body = xdr_1.default.OperationBody.setOptions(setOptionsOp);
this.setSourceAccount(opAttributes, opts);
return new xdr_1.default.Operation(opAttributes);
}
exports.setOptions = setOptions;
//# sourceMappingURL=set_options.js.map