UNPKG

swapable

Version:
150 lines (149 loc) 5.01 kB
"use strict"; /** * This file is part of Swapable shared under AGPL-3.0 * Copyright (C) 2021 Using Blockchain Ltd, Reg No.: 12658136, United Kingdom * * @package Swapable * @author Grégory Saive for Using Blockchain Ltd <greg@ubc.digital> * @license AGPL-3.0 */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Accountable = exports.Signer = exports.Reader = void 0; const symbol_sdk_1 = require("symbol-sdk"); const symbol_hd_wallets_1 = require("symbol-hd-wallets"); /** * @class Reader implements BaseReader * @package Swapable * @subpackage Adapters * @since v1.0.0 * @description Class that describes the blockchain network adapter * for Symbol from NEM compatible network nodes. * @link https://symbolplatform.com */ class Reader { /** * Construct a network configuration object * * @param {string} gatewayUrl * @param {NetworkType} networkType * @param {string} generationHash * @param {number} epochAdjustment * @param {MosaicId} feeMosaicId */ constructor( /** * @description The REST endpoint URL */ gatewayUrl, /** * @description The network type */ networkType, /** * @description The network generation hash */ generationHash, /** * @description The network epoch adjustment */ epochAdjustment, /** * @description The network fee mosaic id */ feeMosaicId, /** * @description (Optional) The node public key */ nodePublicKey) { this.gatewayUrl = gatewayUrl; this.networkType = networkType; this.generationHash = generationHash; this.epochAdjustment = epochAdjustment; this.feeMosaicId = feeMosaicId; this.nodePublicKey = nodePublicKey; this.factoryHttp = new symbol_sdk_1.RepositoryFactoryHttp(gatewayUrl, { networkType, generationHash, epochAdjustment, nodePublicKey, }); } } exports.Reader = Reader; ; /** * @class Signer * @package Swapable * @subpackage Adapters * @since v1.0.0 * @description Class that describes the blockchain adapter for * Symbol from NEM compatible digital signatures. * @link https://symbolplatform.com */ class Signer { constructor() { this.keyChain = symbol_hd_wallets_1.Network.CATAPULT; /// end-region implements Adapter } /// region implements Adapter /** * Creates a key provider for the current blockchain network. * * @see symbol-hd-wallets * @param {Buffer} seed The password encrypted mnemonic seed (bip39). * @return {Wallet} Returns a key provider. */ getKeyProvider(seed) { // - Create a wallet based on a "extended private key" return new symbol_hd_wallets_1.Wallet(symbol_hd_wallets_1.ExtendedKey.createFromSeed(seed.toString('hex'), this.keyChain)); } /** * Unlock a key tree for signing. This method returns * a private key. * * :warning: It is important to never reveal the data * returned by this method, to anyone. * * @param {Buffer} seed The password encrypted mnemonic seed (bip39). * @param {string} derivationPath The account derivation path. * @return {Buffer} */ getPrivateKey(seed, derivationPath) { // - Derive a child account based on a mnemonic seed const privateKey = this.getKeyProvider(seed).getChildAccountPrivateKey(derivationPath); // - Return binary representation return Buffer.from(privateKey, 'hex'); } } exports.Signer = Signer; ; /** * @class Accountable * @package Swapable * @subpackage Adapters * @since v1.0.0 * @description Class that describes the blockchain adapter for * Symbol from NEM compatible child accounts. * @link https://symbolplatform.com */ class Accountable { /** * Derives a child account from a mnemonic \a seed, * and \a derivationPath. By default this method uses * the TESTNET network type. * * @note This method returns sensitive information. * @static * @access public * @param {Buffer} seed The password encrypted mnemonic seed (bip39). * @param {string} derivationPath The account derivation path. * @param {NetworkType} networkType The account network bits (TESTNET/MAINNET). * @param {Signer} provider The signature provider implementation type. * @return {Account} The child account. The returned information shall be secured. */ static derive(seed, derivationPath, networkType = symbol_sdk_1.NetworkType.TEST_NET, provider = new Signer()) { const pkey = provider.getPrivateKey(seed, derivationPath).toString('hex'); return symbol_sdk_1.Account.createFromPrivateKey(pkey, networkType); } } exports.Accountable = Accountable;