@stellar/stellar-sdk
Version:
A library for working with the Stellar network, including communication with the Horizon and Soroban RPC servers.
97 lines (92 loc) • 3.65 kB
JavaScript
;
var buffer = require('buffer');
var transaction_builder = require('../base/transaction_builder.js');
var hashing = require('../base/hashing.js');
class KeypairSigner {
/**
* @param keypair - the {@link Keypair} to sign with. Signing throws
* `cannot sign: no secret key available` if it holds only a public key.
* @param networkPassphrase - passphrase of the network to sign for, used
* whenever the caller does not pass one at signing time
*/
constructor(keypair, networkPassphrase) {
this.keypair = keypair;
this.networkPassphrase = networkPassphrase;
this.address = keypair.publicKey();
}
keypair;
networkPassphrase;
/**
* The keypair's Ed25519 account address (`G…`), always `keypair.publicKey()`.
*/
address;
/* Arrow instance properties rather than prototype methods because
`basicNodeSigner` destructures them into a plain object, which would lose
`this` on a prototype method. (The normalizers below tolerate either, since
they bind what they extract.)
They stay `async` despite having nothing to await (hence the rule
suppressions): that way a synchronous failure — malformed XDR, or a keypair
holding no secret key — rejects the returned promise instead of throwing,
preserving the behavior `basicNodeSigner` had before it delegated here. */
// eslint-disable-next-line @typescript-eslint/require-await
signTransaction = async (xdr, opts) => {
const t = transaction_builder.TransactionBuilder.fromXDR(
xdr,
opts?.networkPassphrase || this.networkPassphrase
);
t.sign(this.keypair);
return {
signedTxXdr: t.toXDR(),
signerAddress: this.address
};
};
// eslint-disable-next-line @typescript-eslint/require-await
signAuthEntry = async (authEntry) => {
const signedAuthEntry = this.keypair.sign(hashing.hash(buffer.Buffer.from(authEntry, "base64"))).toString("base64");
return {
signedAuthEntry,
signerAddress: this.address
};
};
}
function isKeypairLike(value) {
return "publicKey" in value && typeof value.publicKey === "function" && "sign" in value && typeof value.sign === "function" && "signDecorated" in value && typeof value.signDecorated === "function";
}
function signerAddress(value) {
if (value == null || typeof value !== "object") return void 0;
if ("signTransaction" in value && typeof value.address === "string") {
return value.address;
}
return isKeypairLike(value) ? value.publicKey() : void 0;
}
function toSignTransaction(value, networkPassphrase) {
if (value == null) return void 0;
if (typeof value === "function") return value;
if (typeof value !== "object") return void 0;
if ("signTransaction" in value) {
const fn = value.signTransaction;
return typeof fn === "function" ? fn.bind(value) : void 0;
}
if (isKeypairLike(value)) {
return new KeypairSigner(value, networkPassphrase).signTransaction;
}
return void 0;
}
function toSignAuthEntry(value, networkPassphrase) {
if (value == null) return void 0;
if (typeof value === "function") return value;
if (typeof value !== "object") return void 0;
if ("signTransaction" in value) {
const fn = value.signAuthEntry;
return typeof fn === "function" ? fn.bind(value) : void 0;
}
if (isKeypairLike(value)) {
return new KeypairSigner(value, networkPassphrase).signAuthEntry;
}
return void 0;
}
exports.KeypairSigner = KeypairSigner;
exports.signerAddress = signerAddress;
exports.toSignAuthEntry = toSignAuthEntry;
exports.toSignTransaction = toSignTransaction;
//# sourceMappingURL=signer.js.map