From 444d2f26c825cc918b90303f642cce7ac95ef593 Mon Sep 17 00:00:00 2001 From: Matthew Date: Mon, 4 Sep 2023 18:07:50 -0700 Subject: [PATCH] Disable Piratechain This is a temporary measure due to incompatibility between Zcash and Piratechain. The engine will still load and return the keys but will not start synchronizing nor provide any offline tools. --- package.json | 2 - src/piratechain/PiratechainEngine.ts | 17 ++- src/piratechain/PiratechainTools.ts | 198 ++++++++++++++------------- src/react-native.ts | 104 +++++++------- yarn.lock | 7 - 5 files changed, 163 insertions(+), 165 deletions(-) diff --git a/package.json b/package.json index 88694670c..ade2a04b8 100644 --- a/package.json +++ b/package.json @@ -140,7 +140,6 @@ "path-browserify": "^1.0.1", "prettier": "^2.2.0", "process": "^0.11.10", - "react-native-piratechain": "^0.3.2", "react-native-zcash": "0.4.0", "rimraf": "^3.0.2", "stream-browserify": "^2.0.2", @@ -154,7 +153,6 @@ "webpack-dev-server": "^4.11.1" }, "peerDependencies": { - "react-native-piratechain": "^0.3.2", "react-native-zcash": "^0.4.0" } } diff --git a/src/piratechain/PiratechainEngine.ts b/src/piratechain/PiratechainEngine.ts index 098dad2b3..4bc99608e 100644 --- a/src/piratechain/PiratechainEngine.ts +++ b/src/piratechain/PiratechainEngine.ts @@ -177,9 +177,9 @@ export class PiratechainEngine extends CurrencyEngine< async startEngine(): Promise { this.initData() - this.synchronizer = await this.makeSynchronizer(this.initializer) - await this.synchronizer.start() - this.initSubscriptions() + // this.synchronizer = await this.makeSynchronizer(this.initializer) + // await this.synchronizer.start() + // this.initSubscriptions() await super.startEngine() } @@ -272,7 +272,7 @@ export class PiratechainEngine extends CurrencyEngine< } async killEngine(): Promise { - await this.synchronizer.stop() + // await this.synchronizer.stop() await super.killEngine() } @@ -285,12 +285,13 @@ export class PiratechainEngine extends CurrencyEngine< await super.killEngine() await this.clearBlockchainCache() await this.startEngine() - this.synchronizer - .rescan(this.walletInfo.keys.birthdayHeight) - .catch((e: any) => this.warn('resyncBlockchain failed: ', e)) + // this.synchronizer + // .rescan(this.walletInfo.keys.birthdayHeight) + // .catch((e: any) => this.warn('resyncBlockchain failed: ', e)) } async getMaxSpendable(): Promise { + if (!this.isSynced()) throw new Error('Cannot spend until wallet is synced') const spendableBalance = sub( this.availableZatoshi, this.networkInfo.defaultNetworkFee @@ -365,6 +366,8 @@ export class PiratechainEngine extends CurrencyEngine< edgeTransaction: EdgeTransaction, opts?: EdgeEnginePrivateKeyOptions ): Promise { + if (!this.isSynced()) + throw new Error('Cannot broadcast until wallet is synced') const piratechainPrivateKeys = asPiratechainPrivateKeys(this.pluginId)( opts?.privateKeys ) diff --git a/src/piratechain/PiratechainTools.ts b/src/piratechain/PiratechainTools.ts index daa818261..32be496fc 100644 --- a/src/piratechain/PiratechainTools.ts +++ b/src/piratechain/PiratechainTools.ts @@ -1,5 +1,5 @@ import { div } from 'biggystring' -import { entropyToMnemonic, mnemonicToSeed, validateMnemonic } from 'bip39' +import { entropyToMnemonic /* mnemonicToSeed, validateMnemonic */ } from 'bip39' import { Buffer } from 'buffer' import { EdgeCurrencyInfo, @@ -12,20 +12,20 @@ import { EdgeWalletInfo, JsonObject } from 'edge-core-js/types' -import { - AddressTool as AddressToolType, - KeyTool as KeyToolType -} from 'react-native-piratechain' +// import { +// AddressTool as AddressToolType, +// KeyTool as KeyToolType +// } from 'react-native-piratechain' import { PluginEnvironment } from '../common/innerPlugin' -import { asIntegerString } from '../common/types' +// import { asIntegerString } from '../common/types' import { encodeUriCommon, parseUriCommon } from '../common/uriHelpers' import { getLegacyDenomination } from '../common/utils' import { asArrrPublicKey, - asPiratechainPrivateKeys, - PiratechainNetworkInfo, - UnifiedViewingKey + // asPiratechainPrivateKeys, + PiratechainNetworkInfo + // UnifiedViewingKey } from './piratechainTypes' export class PiratechainTools implements EdgeCurrencyTools { @@ -34,8 +34,8 @@ export class PiratechainTools implements EdgeCurrencyTools { io: EdgeIo networkInfo: PiratechainNetworkInfo - KeyTool: typeof KeyToolType - AddressTool: typeof AddressToolType + // KeyTool: typeof KeyToolType + // AddressTool: typeof AddressToolType constructor(env: PluginEnvironment) { const { builtinTokens, currencyInfo, io, networkInfo } = env @@ -48,28 +48,30 @@ export class PiratechainTools implements EdgeCurrencyTools { if (RNAccountbased == null) { throw new Error('Need opts') } - const { KeyTool, AddressTool } = RNAccountbased.piratechain + // const { KeyTool, AddressTool } = RNAccountbased.piratechain - this.KeyTool = KeyTool - this.AddressTool = AddressTool + // this.KeyTool = KeyTool + // this.AddressTool = AddressTool } async getNewWalletBirthdayBlockheight(): Promise { - try { - return await this.KeyTool.getBirthdayHeight( - this.networkInfo.rpcNode.defaultHost, - this.networkInfo.rpcNode.defaultPort - ) - } catch (e: any) { - return this.networkInfo.defaultBirthday - } + throw new Error('getNewWalletBirthdayBlockheight is disabled') + // try { + // return await this.KeyTool.getBirthdayHeight( + // this.networkInfo.rpcNode.defaultHost, + // this.networkInfo.rpcNode.defaultPort + // ) + // } catch (e: any) { + // return this.networkInfo.defaultBirthday + // } } async isValidAddress(address: string): Promise { - return ( - (await this.AddressTool.isValidShieldedAddress(address)) || - (await this.AddressTool.isValidTransparentAddress(address)) - ) + return false + // return ( + // (await this.AddressTool.isValidShieldedAddress(address)) || + // (await this.AddressTool.isValidTransparentAddress(address)) + // ) } // will actually use MNEMONIC version of private key @@ -77,40 +79,41 @@ export class PiratechainTools implements EdgeCurrencyTools { userInput: string, opts: JsonObject = {} ): Promise { - const { pluginId } = this.currencyInfo - const isValid = validateMnemonic(userInput) - if (!isValid) - throw new Error(`Invalid ${this.currencyInfo.currencyCode} mnemonic`) - const hexBuffer = await mnemonicToSeed(userInput) - const hex = hexBuffer.toString('hex') - const spendKey = await this.KeyTool.deriveSpendingKey( - hex, - this.networkInfo.rpcNode.networkName - ) - if (typeof spendKey !== 'string') throw new Error('Invalid spendKey type') - - // Get current network height for the birthday height - const currentNetworkHeight = await this.getNewWalletBirthdayBlockheight() - - let height = currentNetworkHeight - - const { birthdayHeight } = opts - if (birthdayHeight != null) { - asIntegerString(birthdayHeight) - - const birthdayHeightInt = parseInt(birthdayHeight) - - if (birthdayHeightInt > currentNetworkHeight) { - throw new Error('InvalidBirthdayHeight') // must be less than current block height (assuming query was successful) - } - height = birthdayHeightInt - } - - return { - [`${pluginId}Mnemonic`]: userInput, - [`${pluginId}SpendKey`]: spendKey, - [`${pluginId}BirthdayHeight`]: height - } + throw new Error('importPrivateKey is disabled') + // const { pluginId } = this.currencyInfo + // const isValid = validateMnemonic(userInput) + // if (!isValid) + // throw new Error(`Invalid ${this.currencyInfo.currencyCode} mnemonic`) + // const hexBuffer = await mnemonicToSeed(userInput) + // const hex = hexBuffer.toString('hex') + // const spendKey = await this.KeyTool.deriveSpendingKey( + // hex, + // this.networkInfo.rpcNode.networkName + // ) + // if (typeof spendKey !== 'string') throw new Error('Invalid spendKey type') + + // // Get current network height for the birthday height + // const currentNetworkHeight = await this.getNewWalletBirthdayBlockheight() + + // let height = currentNetworkHeight + + // const { birthdayHeight } = opts + // if (birthdayHeight != null) { + // asIntegerString(birthdayHeight) + + // const birthdayHeightInt = parseInt(birthdayHeight) + + // if (birthdayHeightInt > currentNetworkHeight) { + // throw new Error('InvalidBirthdayHeight') // must be less than current block height (assuming query was successful) + // } + // height = birthdayHeightInt + // } + + // return { + // [`${pluginId}Mnemonic`]: userInput, + // [`${pluginId}SpendKey`]: spendKey, + // [`${pluginId}BirthdayHeight`]: height + // } } async createPrivateKey(walletType: string): Promise { @@ -133,34 +136,35 @@ export class PiratechainTools implements EdgeCurrencyTools { } async derivePublicKey(walletInfo: EdgeWalletInfo): Promise { - const { pluginId } = this.currencyInfo - const piratechainPrivateKeys = asPiratechainPrivateKeys(pluginId)( - walletInfo.keys - ) - if (walletInfo.type !== this.currencyInfo.walletType) { - throw new Error('InvalidWalletType') - } - - const mnemonic = piratechainPrivateKeys.mnemonic - if (typeof mnemonic !== 'string') { - throw new Error('InvalidMnemonic') - } - const hexBuffer = await mnemonicToSeed(mnemonic) - const hex = hexBuffer.toString('hex') - const unifiedViewingKeys: UnifiedViewingKey = - await this.KeyTool.deriveViewingKey( - hex, - this.networkInfo.rpcNode.networkName - ) - const shieldedAddress = await this.AddressTool.deriveShieldedAddress( - unifiedViewingKeys.extfvk, - this.networkInfo.rpcNode.networkName - ) - return { - birthdayHeight: piratechainPrivateKeys.birthdayHeight, - publicKey: shieldedAddress, - unifiedViewingKeys - } + return { publicKey: 'derivePublicKey is disabled' } + // const { pluginId } = this.currencyInfo + // const piratechainPrivateKeys = asPiratechainPrivateKeys(pluginId)( + // walletInfo.keys + // ) + // if (walletInfo.type !== this.currencyInfo.walletType) { + // throw new Error('InvalidWalletType') + // } + + // const mnemonic = piratechainPrivateKeys.mnemonic + // if (typeof mnemonic !== 'string') { + // throw new Error('InvalidMnemonic') + // } + // const hexBuffer = await mnemonicToSeed(mnemonic) + // const hex = hexBuffer.toString('hex') + // const unifiedViewingKeys: UnifiedViewingKey = + // await this.KeyTool.deriveViewingKey( + // hex, + // this.networkInfo.rpcNode.networkName + // ) + // const shieldedAddress = await this.AddressTool.deriveShieldedAddress( + // unifiedViewingKeys.extfvk, + // this.networkInfo.rpcNode.networkName + // ) + // return { + // birthdayHeight: piratechainPrivateKeys.birthdayHeight, + // publicKey: shieldedAddress, + // unifiedViewingKeys + // } } async parseUri( @@ -172,8 +176,8 @@ export class PiratechainTools implements EdgeCurrencyTools { const networks = { [pluginId]: true } const { - edgeParsedUri, - edgeParsedUri: { publicAddress } + edgeParsedUri + // edgeParsedUri: { publicAddress } } = parseUriCommon( this.currencyInfo, uri, @@ -182,9 +186,9 @@ export class PiratechainTools implements EdgeCurrencyTools { customTokens ) - if (publicAddress == null || !(await this.isValidAddress(publicAddress))) { - throw new Error('InvalidPublicAddressError') - } + // if (publicAddress == null || !(await this.isValidAddress(publicAddress))) { + // throw new Error('InvalidPublicAddressError') + // } return edgeParsedUri } @@ -194,11 +198,11 @@ export class PiratechainTools implements EdgeCurrencyTools { customTokens: EdgeMetaToken[] = [] ): Promise { const { pluginId } = this.currencyInfo - const { nativeAmount, currencyCode, publicAddress } = obj + const { nativeAmount, currencyCode /* publicAddress */ } = obj - if (!(await this.isValidAddress(publicAddress))) { - throw new Error('InvalidPublicAddressError') - } + // if (!(await this.isValidAddress(publicAddress))) { + // throw new Error('InvalidPublicAddressError') + // } let amount if (nativeAmount != null) { diff --git a/src/react-native.ts b/src/react-native.ts index b5a38bc3b..19c21e732 100644 --- a/src/react-native.ts +++ b/src/react-native.ts @@ -1,11 +1,11 @@ import { EdgeOtherMethods } from 'edge-core-js/types' import { NativeModules } from 'react-native' -import { - AddressTool as PiratechainAddressTool, - KeyTool as PiratechainKeyTool, - makeSynchronizer as PiratechainMakeSynchronizer, - Synchronizer as PirateSynchronizer -} from 'react-native-piratechain' +// import { +// AddressTool as PiratechainAddressTool, +// KeyTool as PiratechainKeyTool, +// makeSynchronizer as PiratechainMakeSynchronizer, +// Synchronizer as PirateSynchronizer +// } from 'react-native-piratechain' import { makeSynchronizer as ZcashMakeSynchronizer, Synchronizer as ZcashSynchronizer, @@ -13,7 +13,7 @@ import { } from 'react-native-zcash' import { bridgifyObject, emit, onMethod } from 'yaob' -import { PiratechainInitializerConfig } from './piratechain/piratechainTypes' +// import { PiratechainInitializerConfig } from './piratechain/piratechainTypes' import { ZcashInitializerConfig } from './zcash/zcashTypes' const { EdgeCurrencyAccountbasedModule } = NativeModules @@ -22,44 +22,44 @@ const { sourceUri } = EdgeCurrencyAccountbasedModule.getConstants() export const pluginUri = sourceUri export const debugUri = 'http://localhost:8082/edge-currency-accountbased.js' -const makePiratechainSynchronizer = async ( - config: PiratechainInitializerConfig -): Promise => { - const realSynchronizer = await PiratechainMakeSynchronizer(config) +// const makePiratechainSynchronizer = async ( +// config: PiratechainInitializerConfig +// ): Promise => { +// const realSynchronizer = await PiratechainMakeSynchronizer(config) - realSynchronizer.subscribe({ - onStatusChanged(status): void { - emit(out, 'statusChanged', status) - }, - onUpdate(event): void { - emit(out, 'update', event) - } - }) +// realSynchronizer.subscribe({ +// onStatusChanged(status): void { +// emit(out, 'statusChanged', status) +// }, +// onUpdate(event): void { +// emit(out, 'update', event) +// } +// }) - const out: PirateSynchronizer = bridgifyObject({ - // @ts-expect-error - on: onMethod, - start: async () => { - return await realSynchronizer.start() - }, - getTransactions: async blockRange => { - return await realSynchronizer.getTransactions(blockRange) - }, - rescan: height => { - return realSynchronizer.rescan(height) - }, - sendToAddress: async spendInfo => { - return await realSynchronizer.sendToAddress(spendInfo) - }, - getShieldedBalance: async () => { - return await realSynchronizer.getShieldedBalance() - }, - stop: async () => { - return await realSynchronizer.stop() - } - }) - return out -} +// const out: PirateSynchronizer = bridgifyObject({ +// // @ts-expect-error +// on: onMethod, +// start: async () => { +// return await realSynchronizer.start() +// }, +// getTransactions: async blockRange => { +// return await realSynchronizer.getTransactions(blockRange) +// }, +// rescan: height => { +// return realSynchronizer.rescan(height) +// }, +// sendToAddress: async spendInfo => { +// return await realSynchronizer.sendToAddress(spendInfo) +// }, +// getShieldedBalance: async () => { +// return await realSynchronizer.getShieldedBalance() +// }, +// stop: async () => { +// return await realSynchronizer.stop() +// } +// }) +// return out +// } const makeZcashSynchronizer = async ( config: ZcashInitializerConfig @@ -101,8 +101,8 @@ const makeZcashSynchronizer = async ( } export function makePluginIo(): EdgeOtherMethods { - bridgifyObject(PiratechainKeyTool) - bridgifyObject(PiratechainAddressTool) + // bridgifyObject(PiratechainKeyTool) + // bridgifyObject(PiratechainAddressTool) bridgifyObject(ZcashNativeTools) return { @@ -118,13 +118,13 @@ export function makePluginIo(): EdgeOtherMethods { })) ) }, - piratechain: bridgifyObject({ - KeyTool: PiratechainKeyTool, - AddressTool: PiratechainAddressTool, - async makeSynchronizer(config: PiratechainInitializerConfig) { - return await makePiratechainSynchronizer(config) - } - }), + // piratechain: bridgifyObject({ + // KeyTool: PiratechainKeyTool, + // AddressTool: PiratechainAddressTool, + // async makeSynchronizer(config: PiratechainInitializerConfig) { + // return await makePiratechainSynchronizer(config) + // } + // }), zcash: bridgifyObject({ Tools: ZcashNativeTools, async makeSynchronizer(config: ZcashInitializerConfig) { diff --git a/yarn.lock b/yarn.lock index 7ce9960e7..d730545a6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6674,13 +6674,6 @@ react-is@^16.12.0: resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4" integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ== -react-native-piratechain@^0.3.2: - version "0.3.2" - resolved "https://registry.yarnpkg.com/react-native-piratechain/-/react-native-piratechain-0.3.2.tgz#5374ddacb0803374b00160ff4dacdc291647ec80" - integrity sha512-2DLzBwtduKvRWsQ3q28PTABGmEhBk2nfO4f0qzEdQ63lgTrZBmkCM1CSM2942AM78+4gEAxOa8RB0JsZg1061g== - dependencies: - rfc4648 "^1.3.0" - react-native-zcash@0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/react-native-zcash/-/react-native-zcash-0.4.0.tgz#cd6cc045673a5bf843a2c5e342b66ee243239555"