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

210 lines 8.43 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Transaction = void 0; const xdr_1 = __importDefault(require("./xdr")); const hashing_1 = require("./hashing"); const strkey_1 = require("./strkey"); const operation_1 = require("./operation"); const memo_1 = require("./memo"); const transaction_base_1 = require("./transaction_base"); const decode_encode_muxed_account_1 = require("./util/decode_encode_muxed_account"); class Transaction extends transaction_base_1.TransactionBase { constructor(envelope, networkPassphrase) { if (typeof envelope === 'string') { const buffer = Buffer.from(envelope, 'base64'); envelope = xdr_1.default.TransactionEnvelope.fromXDR(buffer); } const envelopeType = envelope.switch(); if (!(envelopeType === xdr_1.default.EnvelopeType.envelopeTypeTxV0() || envelopeType === xdr_1.default.EnvelopeType.envelopeTypeTx())) { throw new Error(`Invalid TransactionEnvelope: expected an envelopeTypeTxV0 or envelopeTypeTx but received an ${envelopeType.name}.`); } const txEnvelope = envelope.value(); const tx = txEnvelope.tx(); const fee = tx.fee().toString(); const signatures = (txEnvelope.signatures() || []).slice(); super(tx, signatures, fee, networkPassphrase); this._envelopeType = envelopeType; this._memo = tx.memo(); this._sequence = tx.seqNum().toString(); switch (this._envelopeType) { case xdr_1.default.EnvelopeType.envelopeTypeTxV0(): this._source = strkey_1.StrKey.encodeEd25519PublicKey(this.tx.sourceAccountEd25519()); break; default: this._source = (0, decode_encode_muxed_account_1.encodeMuxedAccountToAddress)(this.tx.sourceAccount()); break; } let cond = null; let timeBounds = null; switch (this._envelopeType) { case xdr_1.default.EnvelopeType.envelopeTypeTxV0(): timeBounds = tx.timeBounds(); break; case xdr_1.default.EnvelopeType.envelopeTypeTx(): switch (tx.cond().switch()) { case xdr_1.default.PreconditionType.precondTime(): timeBounds = tx.cond().timeBounds(); break; case xdr_1.default.PreconditionType.precondV2(): cond = tx.cond().v2(); timeBounds = cond.timeBounds(); break; default: break; } break; default: break; } if (timeBounds) { this._timeBounds = { minTime: timeBounds.minTime().toString(), maxTime: timeBounds.maxTime().toString() }; } if (cond) { const ledgerBounds = cond.ledgerBounds(); if (ledgerBounds) { this._ledgerBounds = { minLedger: ledgerBounds.minLedger(), maxLedger: ledgerBounds.maxLedger() }; } const minSeq = cond.minSeqNum(); if (minSeq) { this._minAccountSequence = minSeq.toString(); } this._minAccountSequenceAge = cond.minSeqAge(); this._minAccountSequenceLedgerGap = cond.minSeqLedgerGap(); this._extraSigners = cond.extraSigners(); } const operations = tx.operations() || []; this._operations = operations.map((op) => operation_1.Operation.fromXDRObject(op)); } get timeBounds() { return this._timeBounds; } set timeBounds(value) { throw new Error('Transaction is immutable'); } get ledgerBounds() { return this._ledgerBounds; } set ledgerBounds(value) { throw new Error('Transaction is immutable'); } get minAccountSequence() { return this._minAccountSequence; } set minAccountSequence(value) { throw new Error('Transaction is immutable'); } get minAccountSequenceAge() { return this._minAccountSequenceAge; } set minAccountSequenceAge(value) { throw new Error('Transaction is immutable'); } get minAccountSequenceLedgerGap() { return this._minAccountSequenceLedgerGap; } set minAccountSequenceLedgerGap(value) { throw new Error('Transaction is immutable'); } get extraSigners() { return this._extraSigners; } set extraSigners(value) { throw new Error('Transaction is immutable'); } get sequence() { return this._sequence; } set sequence(value) { throw new Error('Transaction is immutable'); } get source() { return this._source; } set source(value) { throw new Error('Transaction is immutable'); } get operations() { return this._operations; } set operations(value) { throw new Error('Transaction is immutable'); } get memo() { return memo_1.Memo.fromXDRObject(this._memo); } set memo(value) { throw new Error('Transaction is immutable'); } signatureBase() { let tx = this.tx; if (this._envelopeType === xdr_1.default.EnvelopeType.envelopeTypeTxV0()) { tx = xdr_1.default.Transaction.fromXDR(Buffer.concat([ xdr_1.default.PublicKeyType.publicKeyTypeEd25519().toXDR(), tx.toXDR() ])); } const taggedTransaction = new xdr_1.default.TransactionSignaturePayloadTaggedTransaction.envelopeTypeTx(tx); const txSignature = new xdr_1.default.TransactionSignaturePayload({ networkId: xdr_1.default.Hash.fromXDR((0, hashing_1.hash)(this.networkPassphrase)), taggedTransaction }); return txSignature.toXDR(); } toEnvelope() { const rawTx = this.tx.toXDR(); const signatures = this.signatures.slice(); let envelope; switch (this._envelopeType) { case xdr_1.default.EnvelopeType.envelopeTypeTxV0(): envelope = new xdr_1.default.TransactionEnvelope.envelopeTypeTxV0(new xdr_1.default.TransactionV0Envelope({ tx: xdr_1.default.TransactionV0.fromXDR(rawTx), signatures })); break; case xdr_1.default.EnvelopeType.envelopeTypeTx(): envelope = new xdr_1.default.TransactionEnvelope.envelopeTypeTx(new xdr_1.default.TransactionV1Envelope({ tx: xdr_1.default.Transaction.fromXDR(rawTx), signatures })); break; default: throw new Error(`Invalid TransactionEnvelope: expected an envelopeTypeTxV0 or envelopeTypeTx but received an ${this._envelopeType.name}.`); } return envelope; } getClaimableBalanceId(opIndex) { if (!Number.isInteger(opIndex) || opIndex < 0 || opIndex >= this.operations.length) { throw new RangeError('invalid operation index'); } let op = this.operations[opIndex]; try { op = operation_1.Operation.createClaimableBalance(op); } catch (err) { throw new TypeError(`expected createClaimableBalance, got ${op.type}: ${err}`); } const account = strkey_1.StrKey.decodeEd25519PublicKey((0, decode_encode_muxed_account_1.extractBaseAddress)(this.source)); const operationId = xdr_1.default.HashIdPreimage.envelopeTypeOpId(new xdr_1.default.HashIdPreimageOperationId({ sourceAccount: xdr_1.default.AccountId.publicKeyTypeEd25519(account), seqNum: xdr_1.default.SequenceNumber.fromString(this.sequence), opNum: opIndex })); const opIdHash = (0, hashing_1.hash)(operationId.toXDR('raw')); const balanceId = xdr_1.default.ClaimableBalanceId.claimableBalanceIdTypeV0(opIdHash); return balanceId.toXDR('hex'); } } exports.Transaction = Transaction; //# sourceMappingURL=transaction.js.map