@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
133 lines • 4.86 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Keypair = void 0;
const signing_1 = require("./signing");
const strkey_1 = require("./strkey");
const hashing_1 = require("./hashing");
const xdr_1 = __importDefault(require("./xdr"));
const crypto_lib_1 = require("@okxweb3/crypto-lib");
class Keypair {
constructor(keys) {
if (keys.type !== 'ed25519') {
throw new Error('Invalid keys type');
}
this.type = keys.type;
if (keys.secretKey) {
keys.secretKey = Buffer.from(keys.secretKey);
if (keys.secretKey.length !== 32) {
throw new Error('secretKey length is invalid');
}
this._secretSeed = keys.secretKey;
this._publicKey = (0, signing_1.generate)(keys.secretKey);
this._secretKey = Buffer.concat([keys.secretKey, this._publicKey]);
if (keys.publicKey &&
!this._publicKey.equals(Buffer.from(keys.publicKey))) {
throw new Error('secretKey does not match publicKey');
}
}
else {
this._publicKey = Buffer.from(keys.publicKey);
if (this._publicKey.length !== 32) {
throw new Error('publicKey length is invalid');
}
}
}
static fromSecret(secret) {
const rawSecret = strkey_1.StrKey.decodeEd25519SecretSeed(secret);
return this.fromRawEd25519Seed(rawSecret);
}
static fromRawEd25519Seed(rawSeed) {
return new this({ type: 'ed25519', secretKey: rawSeed });
}
static master(networkPassphrase) {
if (!networkPassphrase) {
throw new Error('No network selected. Please pass a network argument, e.g. `Keypair.master(Networks.PUBLIC)`.');
}
return this.fromRawEd25519Seed((0, hashing_1.hash)(networkPassphrase));
}
static fromPublicKey(publicKey) {
publicKey = strkey_1.StrKey.decodeEd25519PublicKey(publicKey);
if (publicKey.length !== 32) {
throw new Error('Invalid Stellar public key');
}
return new this({ type: 'ed25519', publicKey });
}
static random() {
const secret = crypto_lib_1.base.randomBytes(32);
return this.fromRawEd25519Seed(secret);
}
xdrAccountId() {
return new xdr_1.default.AccountId.publicKeyTypeEd25519(this._publicKey);
}
xdrPublicKey() {
return new xdr_1.default.PublicKey.publicKeyTypeEd25519(this._publicKey);
}
xdrMuxedAccount(id) {
if (typeof id !== 'undefined') {
if (typeof id !== 'string') {
throw new TypeError(`expected string for ID, got ${typeof id}`);
}
return xdr_1.default.MuxedAccount.keyTypeMuxedEd25519(new xdr_1.default.MuxedAccountMed25519({
id: xdr_1.default.Uint64.fromString(id),
ed25519: this._publicKey
}));
}
return new xdr_1.default.MuxedAccount.keyTypeEd25519(this._publicKey);
}
rawPublicKey() {
return this._publicKey;
}
signatureHint() {
const a = this.xdrAccountId().toXDR();
return a.slice(a.length - 4);
}
publicKey() {
return strkey_1.StrKey.encodeEd25519PublicKey(this._publicKey);
}
secret() {
if (!this._secretSeed) {
throw new Error('no secret key available');
}
if (this.type === 'ed25519') {
return strkey_1.StrKey.encodeEd25519SecretSeed(this._secretSeed);
}
throw new Error('Invalid Keypair type');
}
rawSecretKey() {
return this._secretSeed;
}
canSign() {
return !!this._secretKey;
}
sign(data) {
if (!this.canSign()) {
throw new Error('cannot sign: no secret key available');
}
return (0, signing_1.sign)(data, this._secretKey);
}
verify(data, signature) {
return (0, signing_1.verify)(data, signature, this._publicKey);
}
signDecorated(data) {
const signature = this.sign(data);
const hint = this.signatureHint();
return new xdr_1.default.DecoratedSignature({ hint, signature });
}
signPayloadDecorated(data) {
const signature = this.sign(data);
const keyHint = this.signatureHint();
let hint = Buffer.from(data.slice(-4));
if (hint.length < 4) {
hint = Buffer.concat([hint, Buffer.alloc(4 - data.length, 0)]);
}
return new xdr_1.default.DecoratedSignature({
hint: hint.map((byte, i) => byte ^ keyHint[i]),
signature
});
}
}
exports.Keypair = Keypair;
//# sourceMappingURL=keypair.js.map