diff --git a/src/SDK/Client/Client.ts b/src/SDK/Client/Client.ts index b785edd8..43304c8d 100644 --- a/src/SDK/Client/Client.ts +++ b/src/SDK/Client/Client.ts @@ -84,6 +84,8 @@ export class Client extends EventEmitter { this.dapiClient = new DAPIClient(dapiClientOptions); + let dashpayPlugin; + let dashpaySyncWorker; // Initialize a wallet if `wallet` option is preset if (this.options.wallet !== undefined) { if (this.options.wallet.network !== undefined && this.options.wallet.network !== this.network) { @@ -103,9 +105,12 @@ export class Client extends EventEmitter { !this.options.wallet.privateKey && this.options.wallet.offlineMode !== true ){ + dashpayPlugin = new DashPay(); + dashpaySyncWorker = new DashPaySyncWorker(); //@ts-ignore - walletOptions.plugins = [new DashPay(), new DashPaySyncWorker()]; + walletOptions.plugins = [dashpayPlugin, dashpaySyncWorker]; } + //@ts-ignore this.wallet = new Wallet(walletOptions); // @ts-ignore @@ -132,6 +137,11 @@ export class Client extends EventEmitter { driveProtocolVersion: this.options.driveProtocolVersion, }); + if(dashpaySyncWorker && dashpayPlugin){ + dashpayPlugin.inject('platform', this.platform, true) + dashpaySyncWorker.inject('platform', this.platform, true) + } + } /** @@ -150,16 +160,12 @@ export class Client extends EventEmitter { index: this.defaultAccountIndex, ...options, } - const account = await wallet.getAccount(options); try { - const dashpayPlugin = account.getPlugin('dashpay'); const dashpayworker = account.getWorker('DashPaySyncWorker'); - // @ts-ignore - await dashpayPlugin.inject('platform', this.platform, true) - // @ts-ignore - await dashpayworker.inject('platform', this.platform, true) + //@ts-ignore + await dashpayworker.execute(); } catch {} return account; diff --git a/src/SDK/Client/Platform/methods/identities/register.ts b/src/SDK/Client/Platform/methods/identities/register.ts index 69d89ba1..19161f12 100644 --- a/src/SDK/Client/Platform/methods/identities/register.ts +++ b/src/SDK/Client/Platform/methods/identities/register.ts @@ -36,10 +36,11 @@ export default async function register( await broadcastStateTransition(this, identityCreateTransition); // If state transition was broadcast without any errors, import identity to the account - account.storage.insertIdentityIdAtIndex( - account.walletId, - identity.getId().toString(), - identityIndex, + account.storage + .getWalletStore(account.walletId) + .insertIdentityIdAtIndex( + identity.getId().toString(), + identityIndex, ); return identity; diff --git a/src/SDK/Client/plugins/DashPay/DashPay.ts b/src/SDK/Client/plugins/DashPay/DashPay.ts index c0fbe37e..7d860aa3 100644 --- a/src/SDK/Client/plugins/DashPay/DashPay.ts +++ b/src/SDK/Client/plugins/DashPay/DashPay.ts @@ -26,6 +26,7 @@ export class DashPay extends plugins.StandardPlugin { fetchReceivedContactRequests: any; fetchSentContactRequests: any; sendContactRequest: any; + private contacts: any[]; constructor() { super({ name: 'DashPay', @@ -35,10 +36,11 @@ export class DashPay extends plugins.StandardPlugin { workerIntervalTime: 60 * 1000, dependencies: [ 'storage', - 'keyChain', 'decrypt', 'encrypt', + 'keyChainStore', 'walletId', + 'network', 'identities', 'getUnusedIdentityIndex' ], @@ -46,6 +48,7 @@ export class DashPay extends plugins.StandardPlugin { after: ['IdentitySyncWorker'] } }); + this.contacts = [] } } DashPay.prototype.acceptContactRequest = acceptContactRequest; diff --git a/src/SDK/Client/plugins/DashPay/methods/fetchEstablishedContacts.ts b/src/SDK/Client/plugins/DashPay/methods/fetchEstablishedContacts.ts index c85fdd31..0dfa6e1a 100644 --- a/src/SDK/Client/plugins/DashPay/methods/fetchEstablishedContacts.ts +++ b/src/SDK/Client/plugins/DashPay/methods/fetchEstablishedContacts.ts @@ -1,5 +1,6 @@ import { Contact } from "../types/Contact"; import { HDPublicKey } from "@dashevo/dashcore-lib"; +import {KeyChain} from "@dashevo/wallet-lib"; /** * Fetch establish contact from a specific timestamp @@ -50,7 +51,8 @@ export async function fetchEstablishedContacts(this: any, fromTimestamp = 0) { } }); await Promise.all(infoFetchingPromises); - const selfIdentitiesIds = this.storage.getIndexedIdentityIds(this.walletId); + const walletStore = this.storage.getWalletStore(this.walletId) + const selfIdentitiesIds = walletStore.getIndexedIdentityIds(); const selfIdentitiesPrimises = []; const selfIdentities = []; selfIdentitiesIds.forEach((selfIdentitiesId) => { @@ -84,13 +86,41 @@ export async function fetchEstablishedContacts(this: any, fromTimestamp = 0) { depth: Buffer.from('07', 'hex'), childIndex: Buffer.alloc(4), }) + // @ts-ignore - const extendedPrivateKey = this.keyChain.getDIP15ExtendedKey('0x'+ `${selfIdentities[0].getId().toString()}`, '0x'+`${establishedContact.identity.getId().toString()}`); + const extendedPrivateKey = this.keyChainStore.getMasterKeyChain().getDIP15ExtendedKey('0x'+ `${selfIdentities[0].getId().toString()}`, '0x'+`${establishedContact.identity.getId().toString()}`); establishedContact.setHDKeys({ sending: receivedContactPublicKey, receiving: extendedPrivateKey }); + + const lookAheadOpts = { + paths:{ + 'm/0': 10, + }, + isWatched: true + } + //@ts-ignore + const sendingKeyChain = new KeyChain({HDPublicKey: receivedContactPublicKey, lookAheadOpts }); + //@ts-ignore + const receivingKeyChain = new KeyChain({HDPublicKey: extendedPrivateKey, lookAheadOpts }); + //@ts-ignore + const issuedSendingKeyChainPaths = sendingKeyChain.getIssuedPaths(); + //@ts-ignore + const issuedReceivingKeyChainPaths = receivingKeyChain.getIssuedPaths(); + establishedContact.setKeyChains({ + sending: sendingKeyChain, + receiving: receivingKeyChain, + }); + const chainStore = this.storage.getChainStore(this.network); + + issuedReceivingKeyChainPaths.forEach((issuedPath) => { + chainStore.importAddress(issuedPath.address.toString()); + }); + issuedSendingKeyChainPaths.forEach((issuedPath) => { + chainStore.importAddress(issuedPath.address.toString()); + }); }) return establishedContacts; } diff --git a/src/SDK/Client/plugins/DashPay/methods/fetchReceivedContactRequests.ts b/src/SDK/Client/plugins/DashPay/methods/fetchReceivedContactRequests.ts index 36ae2fa8..42c728d2 100644 --- a/src/SDK/Client/plugins/DashPay/methods/fetchReceivedContactRequests.ts +++ b/src/SDK/Client/plugins/DashPay/methods/fetchReceivedContactRequests.ts @@ -2,8 +2,8 @@ * */ export async function fetchReceivedContactRequests(this: any, fromTimestamp = 0){ - // @ts-ignore - const identities = this.storage.getIndexedIdentityIds(this.walletId); + const walletStore = this.storage.getWalletStore(this.walletId) + const identities = walletStore.getIndexedIdentityIds(); if(!identities.length){ throw new Error('Require an identity to fetch sent contact requests'); } diff --git a/src/SDK/Client/plugins/DashPay/methods/fetchSentContactRequests.ts b/src/SDK/Client/plugins/DashPay/methods/fetchSentContactRequests.ts index 80dbe508..cbd0c68a 100644 --- a/src/SDK/Client/plugins/DashPay/methods/fetchSentContactRequests.ts +++ b/src/SDK/Client/plugins/DashPay/methods/fetchSentContactRequests.ts @@ -2,8 +2,8 @@ * */ export async function fetchSentContactRequests(this: any, fromTimestamp = 0){ - // @ts-ignore - const identities = this.storage.getIndexedIdentityIds(this.walletId); + const walletStore = this.storage.getWalletStore(this.walletId) + const identities = walletStore.getIndexedIdentityIds(); if(!identities.length){ throw new Error('Require an identity to fetch sent contact requests'); } diff --git a/src/SDK/Client/plugins/DashPay/methods/sendContactRequest.ts b/src/SDK/Client/plugins/DashPay/methods/sendContactRequest.ts index 7b8fb3ae..cad9208a 100644 --- a/src/SDK/Client/plugins/DashPay/methods/sendContactRequest.ts +++ b/src/SDK/Client/plugins/DashPay/methods/sendContactRequest.ts @@ -5,7 +5,9 @@ */ export async function sendContactRequest(this: any, contactName, accountLabel = 'Default account'){ // @ts-ignore - const identities = this.storage.getIndexedIdentityIds(this.walletId); + const walletStore = this.storage.getWalletStore(this.walletId); + // @ts-ignore + const identities = walletStore.getIndexedIdentityIds(); if(!identities.length){ throw new Error('Require an identity to send a contact request'); } @@ -24,7 +26,7 @@ export async function sendContactRequest(this: any, contactName, accountLabel = const receiverPublicKey = receiverIdentity.toJSON().publicKeys[0].data; const receiverPublicKeyBuffer = Buffer.from(receiverPublicKey, 'base64'); - const extendedPrivateKey = this.keyChain.getDIP15ExtendedKey('0x'+ senderDashUniqueIdentityId, '0x'+contactDashUniqueIdentityId); + const extendedPrivateKey = this.keyChainStore.getMasterKeyChain().getDIP15ExtendedKey('0x'+ senderDashUniqueIdentityId, '0x'+contactDashUniqueIdentityId); const extendedPublicKey = extendedPrivateKey.hdPublicKey; const extendedPublicKeyBuffers = Buffer.concat([extendedPublicKey._buffers.parentFingerPrint, extendedPublicKey._buffers.chainCode, extendedPublicKey._buffers.publicKey]); diff --git a/src/SDK/Client/plugins/DashPay/types/Contact.ts b/src/SDK/Client/plugins/DashPay/types/Contact.ts index 722bd03d..6f2f202c 100644 --- a/src/SDK/Client/plugins/DashPay/types/Contact.ts +++ b/src/SDK/Client/plugins/DashPay/types/Contact.ts @@ -6,6 +6,7 @@ export class Contact { public identity?: any; public profile?: any; public keys?: any; + public keychains?: any; constructor(identityId, sentRequest, receivedRequest) { this.username = null; @@ -30,5 +31,16 @@ export class Contact { sending }; } + setKeyChains(keychainsSet){ + const { receiving, sending } = keychainsSet; + this.keychains = { + receiving, + sending + }; + } + getUnusedAddress(type = 'receiving'){ + const keychain = this.keychains[type]; + return keychain.getFirstUnusedAddress(); + } } diff --git a/src/SDK/Client/plugins/DashPaySyncWorker/DashPaySyncWorker.ts b/src/SDK/Client/plugins/DashPaySyncWorker/DashPaySyncWorker.ts index e68762c3..c6214183 100644 --- a/src/SDK/Client/plugins/DashPaySyncWorker/DashPaySyncWorker.ts +++ b/src/SDK/Client/plugins/DashPaySyncWorker/DashPaySyncWorker.ts @@ -1,4 +1,4 @@ -import { plugins } from "@dashevo/wallet-lib" +import {KeyChain, plugins} from "@dashevo/wallet-lib" export class DashPaySyncWorker extends plugins.Worker { private fromTimestamp: number; @@ -8,6 +8,7 @@ export class DashPaySyncWorker extends plugins.Worker { private getPlugin: any; private storage: any; private contacts: any[]; + private keyChainStore: any; constructor() { super({ @@ -18,9 +19,9 @@ export class DashPaySyncWorker extends plugins.Worker { workerIntervalTime: 60 * 1000, dependencies: [ 'storage', + 'keyChainStore', 'getWorker', 'getPlugin', - 'keyChain', 'walletId', 'identities', 'getUnusedIdentityIndex', @@ -36,10 +37,15 @@ export class DashPaySyncWorker extends plugins.Worker { this.fromTimestamp = 0; } + async onStart(){ + + } + async execute() { if (this.platform && this.platform.identities) { const dashPay = await this.getPlugin('DashPay'); - const identities = this.storage.getIndexedIdentityIds(this.walletId); + const walletStore = this.storage.getWalletStore(this.walletId) + const identities = walletStore.getIndexedIdentityIds(this.walletId); // We require an identity to fetch contacts if (identities.length) { @@ -47,29 +53,24 @@ export class DashPaySyncWorker extends plugins.Worker { // set 10 minute before last query // see: https://github.com/dashpay/dips/blob/master/dip-0015.md#fetching-contact-requests this.fromTimestamp = +new Date() - 10 * 60 * 1000; - const addressesStore = this.storage.store.wallets[this.walletId].addresses; + // const walletStore = this.storage.getWalletStore(this.walletId); + // const addressesStore = this.storage.store.wallets[this.walletId].addresses; + //@ts-ignore + const txStreamSyncWorker = await this.getWorker('TransactionSyncStreamWorker'); contacts .forEach((contact) => { console.log(`DashPaySyncWorker - Fetched contact ${contact.username}`); this.contacts.push(contact); - const { receiving } = contact.keys; - const receiverRootPath = `${contact.sentRequest.ownerId}/${contact.receivedRequest.ownerId}` - const sendingRootPath = `${contact.receivedRequest.ownerId}/${contact.sentRequest.ownerId}` + dashPay.contacts.push(contact); - addressesStore.misc[`${receiverRootPath}/0`] = { - path: `${sendingRootPath}/0`, - index: 0, - transactions: [], - address: receiving.deriveChild(0).privateKey.toAddress(), - balanceSat: 0, - unconfirmedBalanceSat: 0, - utxos: {}, - fetchedLast: 0, - used: false - }; + this.keyChainStore.addKeyChain(contact.keychains.receiving); + this.keyChainStore.addKeyChain(contact.keychains.sending); + //@ts-ignore }) + await txStreamSyncWorker.onStop() + await txStreamSyncWorker.onStart(); } } } diff --git a/tsconfig.json b/tsconfig.json index 2bceef6c..16f801e1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,7 +13,7 @@ "allowJs": true, "sourceMap": true, "declaration": true, - "declarationDir": "./dist" + "declarationDir": "./dist", }, "include": ["src"], "typeRoots": ["node_modules/@types"]