UNPKG

@nekoproject/wallets

Version:

Cross-chain Wallet

260 lines (201 loc) 6.3 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var bip39 = require('bip39'); var web3 = require('@solana/web3.js'); var bs58 = require('bs58'); var ed25519HdKey = require('ed25519-hd-key'); var ethers = require('ethers'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } function _interopNamespace(e) { if (e && e.__esModule) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n["default"] = e; return Object.freeze(n); } var bip39__namespace = /*#__PURE__*/_interopNamespace(bip39); var web3__namespace = /*#__PURE__*/_interopNamespace(web3); var bs58__default = /*#__PURE__*/_interopDefaultLegacy(bs58); /* eslint-disable @typescript-eslint/no-unused-vars */ class Wallet { constructor(wallet) { this._wallet = void 0; this._wallet = wallet; } get address() { throw new Error('Abstract Method has no implementation'); } get signer() { return this._wallet; } getPrivateKey() { throw new Error('Abstract Method has no implementation'); } getSeedsPhrase() { throw new Error('Abstract Method has no implementation'); } getSecretKey() { throw new Error('Abstract Method has no implementation'); } static validate(privateKey) { throw new Error('Abstract Method has no implementation'); } // @solana static generateWallet() { throw new Error('Abstract Method has no implementation'); } // @solana static fromSeed(seed) { throw new Error('Abstract Method has no implementation'); } // @solana static fromSecretKey(secretKey) { throw new Error('Abstract Method has no implementation'); } static fromPrivateKey(secretKey) { throw new Error('Abstract Method has no implementation'); } static async mnemonicToSeed(mnemonic) { if (!bip39__namespace.validateMnemonic(mnemonic)) { throw new Error('Invalid seed phrase'); } return await bip39__namespace.mnemonicToSeed(mnemonic); } static async fromMnemonic(mnemonic) { throw new Error('Abstract Method has no implementation'); } static async generateWalletWithIndex(seed, index) { throw new Error('Abstract Method has no implementation'); } } class SPLWallet extends Wallet { constructor(wallet) { super(wallet); } get address() { return this.signer.publicKey.toString(); } getPrivateKey() { const secretKey = this.signer.secretKey; const encoded = bs58__default["default"].encode(secretKey); return encoded; } getSecretKey() { return this.signer.secretKey; } static validate(privateKey) { try { const toBase58 = bs58__default["default"].decode(privateKey); const keypair = web3__namespace.Keypair.fromSecretKey(toBase58); if (keypair instanceof web3__namespace.Keypair) { return true; } return false; } catch (error) { return false; } } static generateWallet() { const keypair = web3__namespace.Keypair.generate(); return new SPLWallet({ publicKey: keypair.publicKey, secretKey: keypair.secretKey }); } /**@internal*/ static fromSeed(seed) { const keypair = web3__namespace.Keypair.fromSeed(seed); return new SPLWallet({ publicKey: keypair.publicKey, secretKey: keypair.secretKey }); } static async fromMnemonic(mnemonic, delivePath = "m/44'/501'/0'/0'") { const seed = await this.mnemonicToSeed(mnemonic); // type Buffer const delivedSeed = ed25519HdKey.derivePath(delivePath, seed.toString('hex')).key; return this.fromSeed(delivedSeed); } static async generateWalletWithIndex(seed, index) { const delivePath = `m/44'/501'/${index}'/0'`; // 512-bit seed const delivedSeed = ed25519HdKey.derivePath(delivePath, seed.toString('hex')).key; // @TODO return this.fromSeed(delivedSeed); } static fromSecretKey(secretKey) { const keypair = web3__namespace.Keypair.fromSecretKey(secretKey); return new SPLWallet({ publicKey: keypair.publicKey, secretKey: keypair.secretKey }); } static fromPrivateKey(secretKey) { const toBase58 = bs58__default["default"].decode(secretKey); const keypair = web3__namespace.Keypair.fromSecretKey(toBase58); return new SPLWallet({ publicKey: keypair.publicKey, secretKey: keypair.secretKey }); } } class ETHWallet extends Wallet { constructor(wallet) { super(wallet); } get address() { return this.signer.address; } getPrivateKey() { const privateKey = this.signer.privateKey; return privateKey; } getSeedsPhrase() { return this.signer.mnemonic; } static validate(privateKey) { try { const etherWallet = new ethers.ethers.Wallet(privateKey); if (etherWallet instanceof ethers.ethers.Wallet) { return true; } return false; } catch (error) { return false; } } static generateWallet() { const etherWallet = ethers.ethers.Wallet.createRandom(); return new ETHWallet(etherWallet); } /**@internal*/ static hdNodeFromSeed(seed, derivePath) { const hdNode = ethers.ethers.utils.HDNode.fromSeed(seed).derivePath(derivePath); const etherWallet = new ethers.ethers.Wallet(hdNode); return new ETHWallet(etherWallet); } static fromSeed(seed) { const derivePath = "m/44'/60'/0'/0/0"; return this.hdNodeFromSeed(seed, derivePath); } static fromPrivateKey(secretKey) { const etherWallet = new ethers.ethers.Wallet(secretKey); return new ETHWallet(etherWallet); } static async fromMnemonic(mnemonic) { const seed = await this.mnemonicToSeed(mnemonic); return this.fromSeed(seed); } static async generateWalletWithIndex(seed, index) { const derivePath = `m/44'/60'/0'/0/${index}`; return this.hdNodeFromSeed(seed, derivePath); } } exports.ETHWallet = ETHWallet; exports.SPLWallet = SPLWallet; exports.Wallet = Wallet; //# sourceMappingURL=index.commonjs.js.map