diff --git a/packages/wallet-lib/src/types/DerivableKeyChain/DerivableKeyChain.d.ts b/packages/wallet-lib/src/types/DerivableKeyChain/DerivableKeyChain.d.ts new file mode 100644 index 00000000000..406d2baf5ff --- /dev/null +++ b/packages/wallet-lib/src/types/DerivableKeyChain/DerivableKeyChain.d.ts @@ -0,0 +1,50 @@ +import {PrivateKey, Network,} from "../types"; +import {HDPrivateKey, HDPublicKey} from "@dashevo/dashcore-lib"; +import {Transaction} from "@dashevo/dashcore-lib/typings/transaction/Transaction"; + +export declare namespace DerivableKeyChain { + interface IDerivableKeyChainOptions { + network?: Network; + keys?: [Keys] + } +} + +export declare class DerivableKeyChain { + constructor(options?: DerivableKeyChain.IDerivableKeyChainOptions); + network: Network; + keys: [Keys]; + + type: HDKeyTypesParam|PrivateKeyTypeParam; + HDPrivateKey?: HDPrivateKey; + privateKey?: PrivateKey; + + generateKeyForChild(index: number, type?: HDKeyTypesParam): HDPrivateKey|HDPublicKey; + generateKeyForPath(path: string, type?: HDKeyTypesParam): HDPrivateKey|HDPublicKey; + + getDIP15ExtendedKey(userUniqueId: string, contactUniqueId: string, index?: number, accountIndex?: number, type?: HDKeyTypesParam): HDKeyTypes; + getHardenedDIP15AccountKey(index?: number, type?: HDKeyTypesParam): HDKeyTypes; + getHardenedBIP44HDKey(type?: HDKeyTypesParam): HDKeyTypes; + getHardenedDIP9FeatureHDKey(type?: HDKeyTypesParam): HDKeyTypes; + getKeyForChild(index: number, type?: HDKeyTypesParam): HDKeyTypes; + getKeyForPath(path: string, type?: HDKeyTypesParam): HDKeyTypes; + getPrivateKey(): PrivateKey; + + sign(object: Transaction|any, privateKeys:[PrivateKey], sigType: number): any; +} + +type HDKeyTypes = HDPublicKey | HDPrivateKey; + +export declare enum HDKeyTypesParam { + HDPrivateKey="HDPrivateKey", + HDPublicKey="HDPrivateKey", +} +export declare enum PrivateKeyTypeParam { + privateKey='privateKey' +} +export declare interface Keys { + [path: string]: { + path: string + }; +} + + diff --git a/packages/wallet-lib/src/types/DerivableKeyChain/DerivableKeyChain.js b/packages/wallet-lib/src/types/DerivableKeyChain/DerivableKeyChain.js new file mode 100644 index 00000000000..9191f40d7e2 --- /dev/null +++ b/packages/wallet-lib/src/types/DerivableKeyChain/DerivableKeyChain.js @@ -0,0 +1,107 @@ +const { Networks, HDPrivateKey, HDPublicKey } = require('@dashevo/dashcore-lib'); +const { PrivateKey, PublicKey } = require('@dashevo/dashcore-lib'); +const { doubleSha256 } = require('../../utils/crypto'); +const { mnemonicToHDPrivateKey } = require('../../utils/mnemonic'); + +function generateKeyChainId(key) { + const keyChainIdSuffix = doubleSha256(key.toString()).toString('hex').slice(0, 10); + return `kc${keyChainIdSuffix}`; +} + +function fromOptions(opts) { + let rootKey; + let rootKeyType; + let network = Networks.testnet.toString(); + let passphrase = ''; + + if (opts) { + if (opts.passphrase) { + passphrase = opts.passphrase; + } + if (opts.mnemonic) { + rootKeyType = 'HDPrivateKey'; + rootKey = (typeof opts.mnemonic === 'string') ? HDPrivateKey(opts.HDPrivateKey) : opts.HDPrivateKey; + } + if (opts.network) { + network = opts.network; + } + if (opts.HDPrivateKey) { + rootKeyType = 'HDPrivateKey'; + rootKey = (typeof opts.HDPrivateKey === 'string') ? HDPrivateKey(opts.HDPrivateKey) : opts.HDPrivateKey; + network = rootKey.network.toString(); + } else if (opts.HDPublicKey) { + rootKeyType = 'HDPublicKey'; + rootKey = (typeof opts.HDPublicKey === 'string') ? HDPublicKey(opts.HDPublicKey) : opts.HDPublicKey; + network = rootKey.network.toString(); + } else if (opts.privateKey) { + rootKeyType = 'privateKey'; + rootKey = (typeof opts.privateKey === 'string') ? new PrivateKey(opts.privateKey, opts.network) : opts.privateKey; + network = rootKey.network.toString(); + } else if (opts.publicKey) { + rootKeyType = 'publicKey'; + rootKey = (typeof opts.publicKey === 'string') ? new PublicKey(opts.publicKey, opts.network) : opts.publicKey; + network = rootKey.network.toString(); + } else if (opts.address) { + rootKeyType = 'address'; + rootKey = opts.address.toString(); + } else if (opts.mnemonic) { + return fromOptions({ + ...opts, + HDPrivateKey: mnemonicToHDPrivateKey(opts.mnemonic, network, passphrase), + }); + } + } + + const lookAheadOpts = { + isWatched: true, + paths: {}, + ...opts.lookAheadOpts, + }; + + return { + rootKeyType, + rootKey, + network, + passphrase, + lookAheadOpts, + }; +} + +class DerivableKeyChain { + constructor(opts = {}) { + const { + rootKey, + rootKeyType, + network, + lookAheadOpts, + } = fromOptions(opts); + if (!rootKeyType || !rootKey) { + throw new Error('Expect one of [mnemonic, HDPrivateKey, HDPublicKey, privateKey, publicKey, address] to be provided.'); + } + this.keyChainId = generateKeyChainId(rootKey); + + this.rootKey = rootKey; + this.network = network; + this.rootKeyType = rootKeyType; + this.lookAheadOpts = { isWatched: true, ...lookAheadOpts }; + + this.issuedPaths = new Map(); + + this.maybeLookAhead(); + } +} +DerivableKeyChain.prototype.getForPath = require('./methods/getForPath'); +DerivableKeyChain.prototype.getForAddress = require('./methods/getForAddress'); +DerivableKeyChain.prototype.getDIP15ExtendedKey = require('./methods/getDIP15ExtendedKey'); +DerivableKeyChain.prototype.getFirstUnusedAddress = require('./methods/getFirstUnusedAddress'); +DerivableKeyChain.prototype.getHardenedBIP44HDKey = require('./methods/getHardenedBIP44HDKey'); +DerivableKeyChain.prototype.getHardenedDIP9FeatureHDKey = require('./methods/getHardenedDIP9FeatureHDKey'); +DerivableKeyChain.prototype.getHardenedDIP15AccountKey = require('./methods/getHardenedDIP15AccountKey'); +DerivableKeyChain.prototype.getRootKey = require('./methods/getRootKey'); +DerivableKeyChain.prototype.getWatchedAddresses = require('./methods/getWatchedAddresses'); +DerivableKeyChain.prototype.getIssuedPaths = require('./methods/getIssuedPaths'); +DerivableKeyChain.prototype.maybeLookAhead = require('./methods/maybeLookAhead'); +DerivableKeyChain.prototype.markAddressAsUsed = require('./methods/markAddressAsUsed'); +DerivableKeyChain.prototype.sign = require('./methods/sign'); + +module.exports = DerivableKeyChain; diff --git a/packages/wallet-lib/src/types/DerivableKeyChain/DerivableKeyChain.spec.js b/packages/wallet-lib/src/types/DerivableKeyChain/DerivableKeyChain.spec.js new file mode 100644 index 00000000000..02ae0c03abd --- /dev/null +++ b/packages/wallet-lib/src/types/DerivableKeyChain/DerivableKeyChain.spec.js @@ -0,0 +1,181 @@ +const Dashcore = require('@dashevo/dashcore-lib'); +const { expect } = require('chai'); +const DerivableKeyChain = require('./DerivableKeyChain'); +const { mnemonicToHDPrivateKey } = require('../../utils/mnemonic'); + +let derivableKeyChain; +let derivableKeyChain2; +const mnemonic = 'during develop before curtain hazard rare job language become verb message travel'; +const mnemonic2 = 'birth kingdom trash renew flavor utility donkey gasp regular alert pave layer'; +const hdPublicKey = 'xpub661MyMwAqRbcFGB6XSWBsD725rJDUbFUpy4zWe2u22nJ2BxpoHFxtVDfKnTnvVQHohnY7AsVpRTHDv6PyPQTYu1KxFPKw29MAVXPEpz1G7V'; +const expectedRootDIP15AccountKey_0 = 'tprv8hRzmheQujhJN5XP2dj955nAFCKeEoSifJRWuutdbwWRtusdDQ426jbp75EqErUSuTxmPyxYmP1TpcF5qdxGhXLNXRLMGsRLG6NFCv1WnaQ'; +const expectedRootDIP15AccountKey_1 = 'tprv8hRzmheQujhJQyCtFTuUFHxB3Ag5VLB994zhH4CfxbA41cq73HT2mpYq5M33V54oJyn6g514saxxVJB886G55eYX56J6D6x87UNNT6iQHkR'; +const expectedKeyForChild_0 = 'tprv8d4podc2Tg459CH2bwLHXj3vdJFBT2rdsk5Nr1djH7hzHdt5LRdvN6QyFwMiDy7ffRdik7fEVRKKgsHB4F18sh8xF6jFXpKq4sUgGBoSbKw'; + +describe('DerivableKeyChain', function suite() { + this.timeout(1000); + it('should create a DerivableKeyChain', () => { + const expectedException1 = 'Expect one of [mnemonic, HDPrivateKey, HDPublicKey, privateKey, publicKey, address] to be provided.'; + expect(() => new DerivableKeyChain()).to.throw(expectedException1); + + derivableKeyChain = new DerivableKeyChain({ mnemonic: mnemonic, network: 'testnet' }); + expect(derivableKeyChain.rootKeyType).to.equal('HDPrivateKey'); + expect(derivableKeyChain.network.toString()).to.equal('testnet'); + expect(derivableKeyChain.rootKey.network.toString()).to.equal('testnet'); + + derivableKeyChain2 = new DerivableKeyChain({ mnemonic: mnemonic2, network: 'livenet' }); + }); + + it('should generate key for full path', () => { + const path = 'm/44\'/1\'/0\'/0/0'; + const pk2 = derivableKeyChain.getForPath(path).key; + const address = new Dashcore.Address(pk2.publicKey.toAddress()).toString(); + expect(address).to.equal('yNfUebksUc5HoSfg8gv98ruC3jUNJUM8pT'); + }); + + it('should get DIP15 account key', function () { + const rootDIP15AccountKey_0 = derivableKeyChain.getHardenedDIP15AccountKey(0); + expect(rootDIP15AccountKey_0.toString()).to.deep.equal(expectedRootDIP15AccountKey_0); + const rootDIP15AccountKey_1 = derivableKeyChain.getHardenedDIP15AccountKey(1); + expect(rootDIP15AccountKey_1.toString()).to.deep.equal(expectedRootDIP15AccountKey_1); + }); + + it('should get DIP15 extended key', function () { + const userUniqueId = '0x555d3854c910b7dee436869c4724bed2fe0784e198b8a39f02bbb49d8ebcfc3a'; + const contactUniqueId = '0xa137439f36d04a15474ff7423e4b904a14373fafb37a41db74c84f1dbb5c89b5'; + + // m/9'/5'/15'/0'/0x555d3854c910b7dee436869c4724bed2fe0784e198b8a39f02bbb49d8ebcfc3a'/0xa137439f36d04a15474ff7423e4b904a14373fafb37a41db74c84f1dbb5c89b5'/0 + const DIP15ExtPubKey_0 = derivableKeyChain2.getDIP15ExtendedKey(userUniqueId, contactUniqueId, 0, 0, 'HDPublicKey'); + expect(DIP15ExtPubKey_0.toString()).to.equal('xpub6LTkTQFSb8KMgMSz4B6sMZLpkQAY6wSTDprDkHDmLwWLpnjxazuxZn13FrSLKUafitsxuaaffM5a49P6aswhpppWUuYW6eFnwBXshR2W2eY'); + expect(DIP15ExtPubKey_0.publicKey.toString()).to.equal('038030c88ab0106e1f4af3b939db2bafc56f892554106f08da1ce1f9ef10f807bd') + + const DIP15ExtPrivKey_0 = derivableKeyChain2.getDIP15ExtendedKey(userUniqueId, contactUniqueId, 0, 0); + expect(DIP15ExtPrivKey_0.toString()).to.equal('xprvA7UQ3tiYkkm4TsNWx9ZrzRQ6CNL3hUibrbvcwtp9nbyMwzQp3Tbi1ygZQaPoigDhCf8XUjMmGK2NbnB2kLXPYg99Lp6e3iki318sdWcFN3q'); + expect(DIP15ExtPrivKey_0.privateKey.toString()).to.equal('fac40790776d171ee1db90899b5eb2df2f7d2aaf35ad56f07ffb8ed2c57f8e60') + expect(DIP15ExtPrivKey_0.publicKey.toString()).to.equal('038030c88ab0106e1f4af3b939db2bafc56f892554106f08da1ce1f9ef10f807bd') + + // This comes from the test factor of DIP-15 + const userAhash = "0xa11ce14f698b32e9bb306dba7bbbee831263dcf658abeebb39930460ead117e5"; + const userBhash = "0xb0b052ff075c5ca3c16c3e20e9ac8223834475cc1324ab07889cb24ce6a62793"; + const DIP15ExtKey_1 = derivableKeyChain.getDIP15ExtendedKey(userAhash, userBhash, 0, 0); + expect(DIP15ExtKey_1.privateKey.toString()).to.equal('60581b6dca8244d3fb3cfe619b5a22277e5423b01e5285f356981f247e0f4a60') + expect(DIP15ExtKey_1.publicKey.toString()).to.equal('03deaac00f721151307fbc7bf80d7b8afab98c1f026d67e5f56b21e2013f551ce6') + }); + + it('should derive from hardened path and get address', () => { + const hardenedHDKey = derivableKeyChain.getHardenedBIP44HDKey(); + const pk2 = derivableKeyChain.getForPath(`m/44'/1'`).key; + expect(pk2.toString()).to.equal(hardenedHDKey.toString()); + expect(hardenedHDKey.toString()).to.deep.equal('tprv8dtrJNytYHRiZY585hmHGbguS6VjGpK49puSB7oXZjLHcQfrAzQkF4ZCxM2DkEbyY85J4EYcZ8EjT5ZCU8ozB727TDdodbfXet5GkGau2RQ'); + const derivedPk = hardenedHDKey.deriveChild(0, true).deriveChild(0).deriveChild(0); + // m/44'/1'/0'/0/0 (this is first external address of the account 0) + const address = new Dashcore.Address(derivedPk.publicKey.toAddress()).toString(); + expect(address).to.equal('yNfUebksUc5HoSfg8gv98ruC3jUNJUM8pT'); + }); + + it('should get hardened DIP9FeatureHDKey', function () { + const hardenedHDKey = derivableKeyChain.getHardenedDIP9FeatureHDKey(); + const pk2 = derivableKeyChain.getForPath(`m/9'/1'`).key; + expect(pk2.toString()).to.equal(hardenedHDKey.toString()); + expect(hardenedHDKey.toString()).to.deep.equal('tprv8fBJjWoGgCpGRCbyzE9RUA59rmoN1RUijhLnXGL4VHnLxvSe523yVg4GrGzbR6TyXtdynAEh5z8UX55EXt2Cb3xjvrsx2PgTY9BHxzFVkWn'); + }); + + it('should get key for path using the HDPrivateKey', () => { + const derivableKeyChain2 = new DerivableKeyChain({ HDPrivateKey: mnemonicToHDPrivateKey(mnemonic, 'testnet') }); + const keyForChild = derivableKeyChain2.getForPath('m/0').key; + expect(keyForChild.toString()).to.equal(expectedKeyForChild_0); + }); + + it('should mark address watched and get watched addresses', function () { + const key0 = derivableKeyChain.getForPath('m/0'); + derivableKeyChain.getForPath('m/0').isWatched = true + key0.isWatched = true; + derivableKeyChain.getForPath('m/1', { isWatched: false }); + derivableKeyChain.getForPath('m/2', { isWatched: true }); + + const watchedAddresses = derivableKeyChain.getWatchedAddresses(); + let expectedWatchedAddresses = [ + derivableKeyChain.getForPath('m/0').address.toString(), + derivableKeyChain.getForPath('m/2').address.toString() + ]; + expect(watchedAddresses).to.deep.equal(expectedWatchedAddresses); + }); + + it('should get watched addresses', function () { + derivableKeyChain.getForPath('m/1').isWatched = true + const watchedAddresses = derivableKeyChain.getWatchedAddresses(); + const expectedWatchedAddresses = [ + 'ybQDfNwiDjk8ZH5UUmHQzAMEmjbrbK5dAj', + 'yhFX5rseJPitV45HUCaa9haeGHtLuooBaq', + 'yhqxsmYk6jfoGWf1hJKq7d4U2cGHCgzpFU' + ] + expect(watchedAddresses).to.deep.equal(expectedWatchedAddresses); + }); + + it('should remove an address from watched addresses', function () { + derivableKeyChain.getForPath('m/0', { isWatched: false }); + derivableKeyChain.getForPath('m/1'); + const data2 = derivableKeyChain.getForPath('m/2'); + data2.isWatched = false; + + expect(derivableKeyChain.getWatchedAddresses().length).to.equal(1); + }); + + it('should get address for path', function (){ + const address0_1 = derivableKeyChain.getForPath('m/1').address; + expect(address0_1.toString()).to.equal('yhFX5rseJPitV45HUCaa9haeGHtLuooBaq') + }) + + it('should mark address as used', function () { + const address0_0 = derivableKeyChain.getForPath('m/0').address; + derivableKeyChain.markAddressAsUsed(address0_0); + expect(derivableKeyChain.issuedPaths.get('m/0').isUsed).to.equal(true) + }); +}); + +describe('DerivableKeyChain - HDPublicKey', function suite(){ + let hdpubDerivableKeyChain; + it('should initiate from a HDPublicKey', function () { + hdpubDerivableKeyChain = new DerivableKeyChain({ + HDPublicKey: new Dashcore.HDPublicKey(hdPublicKey), + network: 'testnet' + }); + // As the HDPublicKey starts with xpub, it's livenet and should take priority over our network being set. + expect(hdpubDerivableKeyChain.network.toString()).to.equal('livenet'); + expect(hdpubDerivableKeyChain.keyChainId).to.equal('kc5059442d66'); + expect(hdpubDerivableKeyChain.getRootKey().toString()).to.equal(hdPublicKey); + }); + + it('should derivate', function () { + const key0_1 = hdpubDerivableKeyChain.getForPath('m/1').key; + expect(key0_1.publicKey.toAddress(hdpubDerivableKeyChain.network).toString()).to.equal('XoL5LcBiDWcj6L7fFwytsFoX5Vz7BVXw9w') + }); + + it('should get address for path', function (){ + const address0_1 = hdpubDerivableKeyChain.getForPath('m/2').address; + expect(address0_1.toString()).to.equal('XwAzpxQKbgebaLiadq1c6rDeFJ4FKPUufy') + }) +}) + +describe('DerivableKeyChain - single privateKey', function suite() { + this.timeout(10000); + + it('should correctly throw errors out when not a HDPublicKey (privateKey)', () => { + const privateKey = Dashcore.PrivateKey().toString(); + const network = 'livenet'; + const pkDerivableKeyChain = new DerivableKeyChain({ privateKey, network }); + expect(pkDerivableKeyChain.network).to.equal(network); + expect(pkDerivableKeyChain.rootKeyType).to.equal('privateKey'); + expect(pkDerivableKeyChain.rootKey.toString()).to.equal(privateKey); + + const expectedException1 = 'Wallet is not loaded from a mnemonic or a HDPrivateKey, impossible to derivate keys for path m/0'; + expect(() => pkDerivableKeyChain.getForPath('m/0')).to.throw(expectedException1); + }); + + it('should get private key', () => { + const privateKey = Dashcore.PrivateKey().toString(); + const pkDerivableKeyChain = new DerivableKeyChain({ privateKey, network: 'livenet' }); + expect(pkDerivableKeyChain.getRootKey().toString()).to.equal(privateKey); + expect(pkDerivableKeyChain.rootKey.toString()).to.equal(privateKey); + }); +}); diff --git a/packages/wallet-lib/src/types/DerivableKeyChain/methods/getDIP15ExtendedKey.js b/packages/wallet-lib/src/types/DerivableKeyChain/methods/getDIP15ExtendedKey.js new file mode 100644 index 00000000000..b65dc10d7da --- /dev/null +++ b/packages/wallet-lib/src/types/DerivableKeyChain/methods/getDIP15ExtendedKey.js @@ -0,0 +1,26 @@ +/** + * Return the extended key of the relationship between two dashpay contacts. + * @param userUniqueId - Current userID + * @param contactUniqueId - Contact userID + * @param index - the key index. + * @param accountIndex[=0] - the internal wallet account from which derivation is done + * @param type {HDPrivateKey|HDPublicKey} [type=HDPrivateKey] - set the type of returned keys + * @return {HDPrivateKey|HDPublicKey} + */ +function getDIP15ExtendedKey(userUniqueId, contactUniqueId, index = 0, accountIndex = 0, type = 'HDPrivateKey') { + if (!['HDPrivateKey', 'HDPublicKey'].includes(this.rootKeyType)) { + throw new Error('Wallet is not loaded from a mnemonic or a HDPubKey, impossible to derivate keys'); + } + if (!userUniqueId || !contactUniqueId) throw new Error('Required userUniqueId and contactUniqueId to be defined'); + + // Require a HDPrivateKey for hardened derivation + const extendedPrivateKey = this + .getHardenedDIP15AccountKey(accountIndex, 'HDPrivateKey') + .deriveChild((userUniqueId), true) + .deriveChild((contactUniqueId), true) + .deriveChild(index, false); + + return (type === 'HDPublicKey' ? extendedPrivateKey.hdPublicKey : extendedPrivateKey); +} + +module.exports = getDIP15ExtendedKey; diff --git a/packages/wallet-lib/src/types/DerivableKeyChain/methods/getFirstUnusedAddress.js b/packages/wallet-lib/src/types/DerivableKeyChain/methods/getFirstUnusedAddress.js new file mode 100644 index 00000000000..ab7f3fefeeb --- /dev/null +++ b/packages/wallet-lib/src/types/DerivableKeyChain/methods/getFirstUnusedAddress.js @@ -0,0 +1,12 @@ +function getFirstUnusedAddress() { + const allUnused = this.getIssuedPaths() + .filter((path) => path.isUsed === false); + + const firstUnused = allUnused.slice(0, 1)[0]; + + return { + path: firstUnused.path, + address: firstUnused.address.toString(), + }; +} +module.exports = getFirstUnusedAddress; diff --git a/packages/wallet-lib/src/types/DerivableKeyChain/methods/getForAddress.js b/packages/wallet-lib/src/types/DerivableKeyChain/methods/getForAddress.js new file mode 100644 index 00000000000..4b8a01ddf04 --- /dev/null +++ b/packages/wallet-lib/src/types/DerivableKeyChain/methods/getForAddress.js @@ -0,0 +1,12 @@ +function getForAddress(address) { + const searchResult = [...this.issuedPaths.entries()] + .find(([, el]) => el.address.toString() === address.toString()); + + if (!searchResult) { + return null; + } + const [path] = searchResult; + return this.getForPath(path); +} + +module.exports = getForAddress; diff --git a/packages/wallet-lib/src/types/DerivableKeyChain/methods/getForPath.js b/packages/wallet-lib/src/types/DerivableKeyChain/methods/getForPath.js new file mode 100644 index 00000000000..1388abdf05f --- /dev/null +++ b/packages/wallet-lib/src/types/DerivableKeyChain/methods/getForPath.js @@ -0,0 +1,42 @@ +const logger = require('../../../logger'); + +function getForPath(path, opts = {}) { + if (path === undefined) throw new Error('Expect a valid path to derivate'); + const stringifiedPath = path.toString(); + logger.silly(`KeyChain.getForPath(${stringifiedPath})`); + const isUsed = (opts && opts.isUsed !== undefined) ? opts.isUsed : false; + const isWatched = (opts && opts.isWatched !== undefined) ? opts.isWatched : false; + const isDerivable = ['HDPrivateKey', 'HDPublicKey'].includes(this.rootKeyType); + if (!isDerivable && stringifiedPath !== '0') { + throw new Error(`Wallet is not loaded from a mnemonic or a HDPrivateKey, impossible to derivate keys for path ${stringifiedPath}`); + } + + let data; + if (this.issuedPaths.has(stringifiedPath)) { + data = this.issuedPaths.get(stringifiedPath); + if (opts && opts.isWatched !== undefined && data.isWatched !== opts.isWatched) { + data.isWatched = opts.isWatched; + } + if (opts && opts.isUsed !== undefined && data.isUsed !== opts.isUsed) { + data.isUsed = opts.isUsed; + } + return data; + } + + const key = (isDerivable) ? this.rootKey.derive(stringifiedPath) : this.getRootKey(); + + data = { + path: stringifiedPath, + key, + isUsed, + isWatched, + address: key.publicKey.toAddress(this.network), + issuedTime: +new Date(), + }; + + this.issuedPaths.set(stringifiedPath, data); + + return data; +} + +module.exports = getForPath; diff --git a/packages/wallet-lib/src/types/DerivableKeyChain/methods/getHardenedBIP44HDKey.js b/packages/wallet-lib/src/types/DerivableKeyChain/methods/getHardenedBIP44HDKey.js new file mode 100644 index 00000000000..48da61c1616 --- /dev/null +++ b/packages/wallet-lib/src/types/DerivableKeyChain/methods/getHardenedBIP44HDKey.js @@ -0,0 +1,11 @@ +const { BIP44_TESTNET_ROOT_PATH, BIP44_LIVENET_ROOT_PATH } = require('../../../CONSTANTS'); + +/** + * Return a safier root keys to derivate from + * @return {HDPrivateKey|HDPublicKey} + */ +function getHardenedBIP44HDKey() { + const pathRoot = (this.network.toString() === 'testnet') ? BIP44_TESTNET_ROOT_PATH : BIP44_LIVENET_ROOT_PATH; + return this.getForPath(pathRoot).key; +} +module.exports = getHardenedBIP44HDKey; diff --git a/packages/wallet-lib/src/types/DerivableKeyChain/methods/getHardenedDIP15AccountKey.js b/packages/wallet-lib/src/types/DerivableKeyChain/methods/getHardenedDIP15AccountKey.js new file mode 100644 index 00000000000..c01f240fc85 --- /dev/null +++ b/packages/wallet-lib/src/types/DerivableKeyChain/methods/getHardenedDIP15AccountKey.js @@ -0,0 +1,14 @@ +/** + * Return a safier root path to derivate from + * @param {number} [accountIndex=0] - set the account index + * @param {HDPrivateKey|HDPublicKey} [type=HDPrivateKey] - set the type of returned keys + * @return {HDPrivateKey|HDPublicKey} + */ +function getHardenedDIP15AccountKey(accountIndex = 0, type = 'HDPrivateKey') { + const hardenedFeatureRootKey = this.getHardenedDIP9FeatureHDKey(type); + + // Feature is set to 15' for all DashPay Incoming Funds derivation paths (see DIP15). + const featureKey = hardenedFeatureRootKey.deriveChild(15, true); + return featureKey.deriveChild(accountIndex, true); +} +module.exports = getHardenedDIP15AccountKey; diff --git a/packages/wallet-lib/src/types/DerivableKeyChain/methods/getHardenedDIP9FeatureHDKey.js b/packages/wallet-lib/src/types/DerivableKeyChain/methods/getHardenedDIP9FeatureHDKey.js new file mode 100644 index 00000000000..422c9fab667 --- /dev/null +++ b/packages/wallet-lib/src/types/DerivableKeyChain/methods/getHardenedDIP9FeatureHDKey.js @@ -0,0 +1,11 @@ +const { DIP9_LIVENET_ROOT_PATH, DIP9_TESTNET_ROOT_PATH } = require('../../../CONSTANTS'); + +/** + * Return a safier root path to derivate from + * @return {HDPrivateKey|HDPublicKey} + */ +function getHardenedDIP9FeatureHDKey() { + const pathRoot = (this.network.toString() === 'testnet') ? DIP9_TESTNET_ROOT_PATH : DIP9_LIVENET_ROOT_PATH; + return this.getForPath(pathRoot).key; +} +module.exports = getHardenedDIP9FeatureHDKey; diff --git a/packages/wallet-lib/src/types/DerivableKeyChain/methods/getIssuedPaths.js b/packages/wallet-lib/src/types/DerivableKeyChain/methods/getIssuedPaths.js new file mode 100644 index 00000000000..324750853ba --- /dev/null +++ b/packages/wallet-lib/src/types/DerivableKeyChain/methods/getIssuedPaths.js @@ -0,0 +1,5 @@ +function getWatchedAddresses() { + return [...this.issuedPaths.values()]; +} + +module.exports = getWatchedAddresses; diff --git a/packages/wallet-lib/src/types/DerivableKeyChain/methods/getRootKey.js b/packages/wallet-lib/src/types/DerivableKeyChain/methods/getRootKey.js new file mode 100644 index 00000000000..f2b54723a48 --- /dev/null +++ b/packages/wallet-lib/src/types/DerivableKeyChain/methods/getRootKey.js @@ -0,0 +1,5 @@ +function getRootKey() { + return this.rootKey; +} + +module.exports = getRootKey; diff --git a/packages/wallet-lib/src/types/DerivableKeyChain/methods/getWatchedAddresses.js b/packages/wallet-lib/src/types/DerivableKeyChain/methods/getWatchedAddresses.js new file mode 100644 index 00000000000..8154329119a --- /dev/null +++ b/packages/wallet-lib/src/types/DerivableKeyChain/methods/getWatchedAddresses.js @@ -0,0 +1,7 @@ +function getWatchedAddresses() { + return [...this.issuedPaths.entries()] + .filter(([, el]) => el.isWatched === true) + .map(([, el]) => el.address.toString()); +} + +module.exports = getWatchedAddresses; diff --git a/packages/wallet-lib/src/types/DerivableKeyChain/methods/markAddressAsUsed.js b/packages/wallet-lib/src/types/DerivableKeyChain/methods/markAddressAsUsed.js new file mode 100644 index 00000000000..cecf2922e1f --- /dev/null +++ b/packages/wallet-lib/src/types/DerivableKeyChain/methods/markAddressAsUsed.js @@ -0,0 +1,17 @@ +const logger = require('../../../logger'); + +function markAddressAsUsed(address) { + const searchResult = [...this.issuedPaths.entries()] + .find(([, el]) => el.address.toString() === address.toString()); + + if (searchResult) { + const [, addressData] = searchResult; + logger.silly(`KeyChain - Marking ${address} ${addressData.path} as used`); + addressData.isUsed = true; + + return this.maybeLookAhead(); + } + + return false; +} +module.exports = markAddressAsUsed; diff --git a/packages/wallet-lib/src/types/DerivableKeyChain/methods/maybeLookAhead.js b/packages/wallet-lib/src/types/DerivableKeyChain/methods/maybeLookAhead.js new file mode 100644 index 00000000000..fd3841f40bd --- /dev/null +++ b/packages/wallet-lib/src/types/DerivableKeyChain/methods/maybeLookAhead.js @@ -0,0 +1,82 @@ +function maybeLookAhead() { + const { lookAheadOpts } = this; + const generatedPaths = []; + + if (Object.keys(lookAheadOpts.paths).length === 0) { + return generatedPaths; + } + + const usedPaths = [...this.issuedPaths.entries()] + .filter(([, el]) => el.isUsed === true) + .map(([path]) => path); + + const sortedUsedPathByBase = {}; + + usedPaths + .forEach((usedPath) => { + const splitted = usedPath.split('/'); + // Removes the index to sort which and how many base path has been generated + const basePath = splitted.splice(0, splitted.length - 1).join('/'); + if (!sortedUsedPathByBase[basePath]) sortedUsedPathByBase[basePath] = []; + sortedUsedPathByBase[basePath].push(usedPath); + }); + + const lastUsedIndexes = {}; + const lastGeneratedIndexes = {}; + + Object + .entries(lookAheadOpts.paths) + .forEach(([basePath]) => { + lastUsedIndexes[basePath] = -1; + lastGeneratedIndexes[basePath] = -1; + }); + + Object + .entries(sortedUsedPathByBase) + .forEach(([basePath, basePaths]) => { + // Sorting by index is also needed as the user might have manually issue a key + // and set it up to watched or used outside of lookAhead bounds + const sortedBasePaths = basePaths.sort((a, b) => a.split('/').splice(-1) - b.split('/').splice(-1)); + + sortedBasePaths.forEach((path) => { + const addressData = this.issuedPaths.get(path); + + const currentIndex = parseInt(path.split('/').splice(-1), 10); + + if (addressData.isUsed) { + lastUsedIndexes[basePath] = currentIndex; + } + + lastGeneratedIndexes[basePath] = currentIndex; + }); + }); + + const isWatched = lookAheadOpts.isWatched || false; + + Object + .entries(lastGeneratedIndexes) + .forEach(([basePath]) => { + const lastUsedAndLastGenGap = lastGeneratedIndexes[basePath] - lastUsedIndexes[basePath]; + const pathAmountToGenerate = lookAheadOpts.paths[basePath] - lastUsedAndLastGenGap; + + if (pathAmountToGenerate > 0) { + const lastIndex = lastGeneratedIndexes[basePath]; + const lastIndexToGenerate = lastIndex + pathAmountToGenerate; + + if (lastIndexToGenerate > lastIndex) { + for ( + let index = lastIndex + 1; + index <= lastIndexToGenerate; + index += 1) { + const timeNow = +new Date(); + const pathData = this.getForPath(`${basePath}/${index}`, { isWatched }); + if (pathData.issuedTime >= timeNow) { + generatedPaths.push(pathData); + } + } + } + } + }); + return generatedPaths; +} +module.exports = maybeLookAhead; diff --git a/packages/wallet-lib/src/types/DerivableKeyChain/methods/sign.js b/packages/wallet-lib/src/types/DerivableKeyChain/methods/sign.js new file mode 100644 index 00000000000..b10b561f2c2 --- /dev/null +++ b/packages/wallet-lib/src/types/DerivableKeyChain/methods/sign.js @@ -0,0 +1,29 @@ +const { + crypto, Transaction, Message, +} = require('@dashevo/dashcore-lib'); + +/** + * Allow to sign any transaction or a transition object from a valid privateKeys list + * @param {Transaction|any} object + * @param {[PrivateKey]} privateKeys + * @param {number} [sigType=crypto.Signature.SIGHASH_ALL] + */ +function sign(object, privateKeys, sigType = crypto.Signature.SIGHASH_ALL) { + const handledTypes = [Transaction.name, Transaction.Payload.SubTxRegisterPayload, Message.name]; + if (!privateKeys) throw new Error('Require one or multiple privateKeys to sign'); + if (!object) throw new Error('Nothing to sign'); + if (!handledTypes.includes(object.constructor.name)) { + throw new Error(`Keychain sign : Unhandled object of type ${object.constructor.name}`); + } + const obj = object.sign(privateKeys, sigType); + + if (obj.isFullySigned && !obj.isFullySigned()) { + throw new Error('Not fully signed transaction'); + } + if (object.constructor.name === 'Message') { + // When signed, message are in string form. + return Message(obj); + } + return obj; +} +module.exports = sign; diff --git a/packages/wallet-lib/src/types/Identities/methods/getIdentityIds.spec.js b/packages/wallet-lib/src/types/Identities/methods/getIdentityIds.spec.js index df90868e4c2..df2e887176b 100644 --- a/packages/wallet-lib/src/types/Identities/methods/getIdentityIds.spec.js +++ b/packages/wallet-lib/src/types/Identities/methods/getIdentityIds.spec.js @@ -1,7 +1,6 @@ const { expect } = require('chai'); const mockedStore = require('../../../../fixtures/sirentonight-fullstore-snapshot-1562711703'); const getIdentityIds = require('./getIdentityIds'); -const searchTransaction = require('../../Storage/methods/searchTransaction'); let mockedWallet; let fetchTransactionInfoCalledNb = 0; @@ -12,7 +11,6 @@ describe('Wallet#getIdentityIds', function suite() { store: mockedStore, getStore: () => mockedStore, mappedAddress: {}, - searchTransaction, getIndexedIdentityIds: () => mockedStore.wallets[Object.keys(mockedStore.wallets)].identityIds, }; const walletId = Object.keys(mockedStore.wallets)[0]; diff --git a/packages/wallet-lib/src/types/KeyChain/KeyChain.d.ts b/packages/wallet-lib/src/types/KeyChain/KeyChain.d.ts index 0c1ee65a322..0d774d60b6f 100644 --- a/packages/wallet-lib/src/types/KeyChain/KeyChain.d.ts +++ b/packages/wallet-lib/src/types/KeyChain/KeyChain.d.ts @@ -3,48 +3,46 @@ import {HDPrivateKey, HDPublicKey} from "@dashevo/dashcore-lib"; import {Transaction} from "@dashevo/dashcore-lib/typings/transaction/Transaction"; export declare namespace KeyChain { - interface IKeyChainOptions { - network?: Network; - keys?: [Keys] - } + interface IKeyChainOptions { + network?: Network; + keys?: [Keys] + } } export declare class KeyChain { - constructor(options?: KeyChain.IKeyChainOptions); - network: Network; - keys: [Keys]; - - type: HDKeyTypesParam|PrivateKeyTypeParam; - HDPrivateKey?: HDPrivateKey; - privateKey?: PrivateKey; - - generateKeyForChild(index: number, type?: HDKeyTypesParam): HDPrivateKey|HDPublicKey; - generateKeyForPath(path: string, type?: HDKeyTypesParam): HDPrivateKey|HDPublicKey; - - getDIP15ExtendedKey(userUniqueId: string, contactUniqueId: string, index?: number, accountIndex?: number, type?: HDKeyTypesParam): HDKeyTypes; - getHardenedDIP15AccountKey(index?: number, type?: HDKeyTypesParam): HDKeyTypes; - getHardenedBIP44HDKey(type?: HDKeyTypesParam): HDKeyTypes; - getHardenedDIP9FeatureHDKey(type?: HDKeyTypesParam): HDKeyTypes; - getKeyForChild(index: number, type?: HDKeyTypesParam): HDKeyTypes; - getKeyForPath(path: string, type?: HDKeyTypesParam): HDKeyTypes; - getPrivateKey(): PrivateKey; - - sign(object: Transaction|any, privateKeys:[PrivateKey], sigType: number): any; + constructor(options?: KeyChain.IKeyChainOptions); + network: Network; + keys: [Keys]; + + type: HDKeyTypesParam|PrivateKeyTypeParam; + HDPrivateKey?: HDPrivateKey; + privateKey?: PrivateKey; + + generateKeyForChild(index: number, type?: HDKeyTypesParam): HDPrivateKey|HDPublicKey; + generateKeyForPath(path: string, type?: HDKeyTypesParam): HDPrivateKey|HDPublicKey; + + getDIP15ExtendedKey(userUniqueId: string, contactUniqueId: string, index?: number, accountIndex?: number, type?: HDKeyTypesParam): HDKeyTypes; + getHardenedDIP15AccountKey(index?: number, type?: HDKeyTypesParam): HDKeyTypes; + getHardenedBIP44HDKey(type?: HDKeyTypesParam): HDKeyTypes; + getHardenedDIP9FeatureHDKey(type?: HDKeyTypesParam): HDKeyTypes; + getKeyForChild(index: number, type?: HDKeyTypesParam): HDKeyTypes; + getKeyForPath(path: string, type?: HDKeyTypesParam): HDKeyTypes; + getPrivateKey(): PrivateKey; + + sign(object: Transaction|any, privateKeys:[PrivateKey], sigType: number): any; } type HDKeyTypes = HDPublicKey | HDPrivateKey; export declare enum HDKeyTypesParam { - HDPrivateKey="HDPrivateKey", - HDPublicKey="HDPrivateKey", + HDPrivateKey="HDPrivateKey", + HDPublicKey="HDPrivateKey", } export declare enum PrivateKeyTypeParam { - privateKey='privateKey' + privateKey='privateKey' } export declare interface Keys { - [path: string]: { - path: string - }; + [path: string]: { + path: string + }; } - - diff --git a/packages/wallet-lib/src/types/KeyChainStore/KeyChainStore.js b/packages/wallet-lib/src/types/KeyChainStore/KeyChainStore.js new file mode 100644 index 00000000000..fd5c24dfacb --- /dev/null +++ b/packages/wallet-lib/src/types/KeyChainStore/KeyChainStore.js @@ -0,0 +1,16 @@ +class KeyChainStore { + constructor() { + this.keyChains = new Map(); + this.walletKeyChainId = null; + this.masterKeyChainId = null; + this.accountKeyChains = new Map(); + } +} + +KeyChainStore.prototype.addKeyChain = require('./methods/addKeyChain'); +KeyChainStore.prototype.getKeyChain = require('./methods/getKeyChain'); +KeyChainStore.prototype.getKeyChains = require('./methods/getKeyChains'); +KeyChainStore.prototype.makeChildKeyChainStore = require('./methods/makeChildKeyChainStore'); +KeyChainStore.prototype.getMasterKeyChain = require('./methods/getMasterKeyChain'); + +module.exports = KeyChainStore; diff --git a/packages/wallet-lib/src/types/KeyChainStore/KeyChainStore.spec.js b/packages/wallet-lib/src/types/KeyChainStore/KeyChainStore.spec.js new file mode 100644 index 00000000000..bfb74a6cd81 --- /dev/null +++ b/packages/wallet-lib/src/types/KeyChainStore/KeyChainStore.spec.js @@ -0,0 +1,47 @@ +const {HDPrivateKey} = require("@dashevo/dashcore-lib"); +const KeyChainsStore = require('./KeyChainStore'); +const DerivableKeyChain = require("../DerivableKeyChain/DerivableKeyChain"); +const { expect } = require('chai'); + +describe('KeyChainStore', function suite() { + let keyChainsStore; + let hdPrivateKey = new HDPrivateKey() + let hdPublicKey = new HDPrivateKey().hdPublicKey + let keyChain = new DerivableKeyChain({HDPrivateKey: hdPrivateKey}) + let keyChainPublic = new DerivableKeyChain({HDPublicKey: hdPublicKey}) + let walletKeyChain = new DerivableKeyChain({HDPrivateKey:new HDPrivateKey()}); + it('should create a KeyChainStore', () => { + keyChainsStore = new KeyChainsStore(); + expect(keyChainsStore).to.exist; + expect(keyChainsStore.keyChains).to.be.a('Map') + }); + it('should be able to add a keyChain', function () { + keyChainsStore.addKeyChain(keyChain) + expect(keyChainsStore.keyChains.has(keyChain.keyChainId)).to.equal(true); + keyChainsStore.addKeyChain(keyChainPublic) + expect(keyChainsStore.keyChains.has(keyChainPublic.keyChainId)).to.equal(true); + }); + it('should allow to specify a specific master keychain', function () { + keyChainsStore.addKeyChain(walletKeyChain, { isMasterKeyChain: true }); + expect(keyChainsStore.keyChains.has(walletKeyChain.keyChainId)).to.equal(true); + }); + it('should get all keyChains', function () { + const keyChains = keyChainsStore.getKeyChains() + expect(keyChains).to.deep.equal([keyChain, keyChainPublic, walletKeyChain]); + }); + it('should get a keychain by its ID', () => { + const requestedKeychain = keyChainsStore.getKeyChain(keyChainPublic.keyChainId); + expect(requestedKeychain).to.equal(keyChainPublic); + }) + it('should get a master keychain', function () { + const requestedWalletKeyChain = keyChainsStore.getMasterKeyChain(); + expect(requestedWalletKeyChain).to.equal(walletKeyChain); + }); + it('should make a child key chain store', function () { + const childKeyChainStore = keyChainsStore.makeChildKeyChainStore('m/0') + expect(childKeyChainStore).to.exist; + expect(childKeyChainStore.keyChains).to.be.a('Map') + expect(childKeyChainStore.getMasterKeyChain().rootKeyType).to.be.equal(HDPrivateKey.name) + }); +}); + diff --git a/packages/wallet-lib/src/types/KeyChainStore/methods/addKeyChain.js b/packages/wallet-lib/src/types/KeyChainStore/methods/addKeyChain.js new file mode 100644 index 00000000000..04e60a50739 --- /dev/null +++ b/packages/wallet-lib/src/types/KeyChainStore/methods/addKeyChain.js @@ -0,0 +1,15 @@ +function addKeyChain(keychain, opts = {}) { + if (this.keyChains.has(keychain.keyChainId)) { + throw new Error(`Trying to add already existing keyChain ${keychain.keyChainId}`); + } + + this.keyChains.set(keychain.keyChainId, keychain); + + if (opts) { + if (opts.isMasterKeyChain && !this.masterKeyChainId) { + this.masterKeyChainId = keychain.keyChainId; + } + } +} + +module.exports = addKeyChain; diff --git a/packages/wallet-lib/src/types/KeyChainStore/methods/getKeyChain.js b/packages/wallet-lib/src/types/KeyChainStore/methods/getKeyChain.js new file mode 100644 index 00000000000..d964ba395ee --- /dev/null +++ b/packages/wallet-lib/src/types/KeyChainStore/methods/getKeyChain.js @@ -0,0 +1,5 @@ +function getKeyChain(keyChainId) { + return this.keyChains.get(keyChainId); +} + +module.exports = getKeyChain; diff --git a/packages/wallet-lib/src/types/KeyChainStore/methods/getKeyChains.js b/packages/wallet-lib/src/types/KeyChainStore/methods/getKeyChains.js new file mode 100644 index 00000000000..4e0e16160cd --- /dev/null +++ b/packages/wallet-lib/src/types/KeyChainStore/methods/getKeyChains.js @@ -0,0 +1,5 @@ +function getKeyChains() { + return Array.from(this.keyChains.values()); +} + +module.exports = getKeyChains; diff --git a/packages/wallet-lib/src/types/KeyChainStore/methods/getMasterKeyChain.js b/packages/wallet-lib/src/types/KeyChainStore/methods/getMasterKeyChain.js new file mode 100644 index 00000000000..512d6a28133 --- /dev/null +++ b/packages/wallet-lib/src/types/KeyChainStore/methods/getMasterKeyChain.js @@ -0,0 +1,6 @@ +function getMasterKeyChain() { + const keyChainId = this.masterKeyChainId; + return this.keyChains.get(keyChainId); +} + +module.exports = getMasterKeyChain; diff --git a/packages/wallet-lib/src/types/KeyChainStore/methods/makeChildKeyChainStore.js b/packages/wallet-lib/src/types/KeyChainStore/methods/makeChildKeyChainStore.js new file mode 100644 index 00000000000..383baee3ce0 --- /dev/null +++ b/packages/wallet-lib/src/types/KeyChainStore/methods/makeChildKeyChainStore.js @@ -0,0 +1,19 @@ +const DerivableKeyChain = require('../../DerivableKeyChain/DerivableKeyChain'); +const logger = require('../../../logger'); + +function makeChildKeyChainStore(path, opts) { + logger.debug(`KeyChainStore - make a child keychainstore for ${path}`); + const masterKeyChain = this.getMasterKeyChain(); + if (!masterKeyChain) throw new Error('Requires a master keychain to be added first.'); + + const childKeyChainStore = new this.constructor(); + const keyChainOpts = { network: masterKeyChain.network, ...opts }; + + // Accessing the type from getKeyForPath would behave on browser differently due to mangling. + keyChainOpts[masterKeyChain.rootKeyType] = masterKeyChain.getForPath(path).key; + const childKeyChain = new DerivableKeyChain(keyChainOpts); + childKeyChainStore.addKeyChain(childKeyChain, { isMasterKeyChain: true }); + return childKeyChainStore; +} + +module.exports = makeChildKeyChainStore;