@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
108 lines • 3.54 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TransactionBase = void 0;
const xdr_1 = __importDefault(require("./xdr"));
const hashing_1 = require("./hashing");
const keypair_1 = require("./keypair");
class TransactionBase {
constructor(tx, signatures, fee, networkPassphrase) {
if (typeof networkPassphrase !== 'string') {
throw new Error(`Invalid passphrase provided to Transaction: expected a string but got a ${typeof networkPassphrase}`);
}
this._networkPassphrase = networkPassphrase;
this._tx = tx;
this._signatures = signatures;
this._fee = fee;
}
get signatures() {
return this._signatures;
}
set signatures(value) {
throw new Error('Transaction is immutable');
}
get tx() {
return this._tx;
}
set tx(value) {
throw new Error('Transaction is immutable');
}
get fee() {
return this._fee;
}
set fee(value) {
throw new Error('Transaction is immutable');
}
get networkPassphrase() {
return this._networkPassphrase;
}
set networkPassphrase(networkPassphrase) {
this._networkPassphrase = networkPassphrase;
}
sign(...keypairs) {
const txHash = this.hash();
keypairs.forEach((kp) => {
const sig = kp.signDecorated(txHash);
this.signatures.push(sig);
});
}
getKeypairSignature(keypair) {
return keypair.sign(this.hash()).toString('base64');
}
addSignature(publicKey = '', signature = '') {
if (!signature || typeof signature !== 'string') {
throw new Error('Invalid signature');
}
if (!publicKey || typeof publicKey !== 'string') {
throw new Error('Invalid publicKey');
}
let keypair;
let hint;
const signatureBuffer = Buffer.from(signature, 'base64');
try {
keypair = keypair_1.Keypair.fromPublicKey(publicKey);
hint = keypair.signatureHint();
}
catch (e) {
throw new Error('Invalid publicKey');
}
if (!keypair.verify(this.hash(), signatureBuffer)) {
throw new Error('Invalid signature');
}
this.signatures.push(new xdr_1.default.DecoratedSignature({
hint,
signature: signatureBuffer
}));
}
addDecoratedSignature(signature) {
this.signatures.push(signature);
}
signHashX(preimage) {
if (typeof preimage === 'string') {
preimage = Buffer.from(preimage, 'hex');
}
if (preimage.length > 64) {
throw new Error('preimage cannnot be longer than 64 bytes');
}
const signature = preimage;
const hashX = (0, hashing_1.hash)(preimage);
const hint = hashX.slice(hashX.length - 4);
this.signatures.push(new xdr_1.default.DecoratedSignature({ hint, signature }));
}
hash() {
return (0, hashing_1.hash)(this.signatureBase());
}
signatureBase() {
throw new Error('Implement in subclass');
}
toEnvelope() {
throw new Error('Implement in subclass');
}
toXDR() {
return this.toEnvelope().toXDR().toString('base64');
}
}
exports.TransactionBase = TransactionBase;
//# sourceMappingURL=transaction_base.js.map