Skip to content

Commit

Permalink
Disable Piratechain
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
peachbits committed Sep 5, 2023
1 parent 7a6ae37 commit 444d2f2
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 165 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -154,7 +153,6 @@
"webpack-dev-server": "^4.11.1"
},
"peerDependencies": {
"react-native-piratechain": "^0.3.2",
"react-native-zcash": "^0.4.0"
}
}
17 changes: 10 additions & 7 deletions src/piratechain/PiratechainEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ export class PiratechainEngine extends CurrencyEngine<

async startEngine(): Promise<void> {
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()
}

Expand Down Expand Up @@ -272,7 +272,7 @@ export class PiratechainEngine extends CurrencyEngine<
}

async killEngine(): Promise<void> {
await this.synchronizer.stop()
// await this.synchronizer.stop()
await super.killEngine()
}

Expand All @@ -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<string> {
if (!this.isSynced()) throw new Error('Cannot spend until wallet is synced')
const spendableBalance = sub(
this.availableZatoshi,
this.networkInfo.defaultNetworkFee
Expand Down Expand Up @@ -365,6 +366,8 @@ export class PiratechainEngine extends CurrencyEngine<
edgeTransaction: EdgeTransaction,
opts?: EdgeEnginePrivateKeyOptions
): Promise<EdgeTransaction> {
if (!this.isSynced())
throw new Error('Cannot broadcast until wallet is synced')
const piratechainPrivateKeys = asPiratechainPrivateKeys(this.pluginId)(
opts?.privateKeys
)
Expand Down
198 changes: 101 additions & 97 deletions src/piratechain/PiratechainTools.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 {
Expand All @@ -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<PiratechainNetworkInfo>) {
const { builtinTokens, currencyInfo, io, networkInfo } = env
Expand All @@ -48,69 +48,72 @@ 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<number> {
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<boolean> {
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
async importPrivateKey(
userInput: string,
opts: JsonObject = {}
): Promise<Object> {
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<Object> {
Expand All @@ -133,34 +136,35 @@ export class PiratechainTools implements EdgeCurrencyTools {
}

async derivePublicKey(walletInfo: EdgeWalletInfo): Promise<Object> {
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(
Expand All @@ -172,8 +176,8 @@ export class PiratechainTools implements EdgeCurrencyTools {
const networks = { [pluginId]: true }

const {
edgeParsedUri,
edgeParsedUri: { publicAddress }
edgeParsedUri
// edgeParsedUri: { publicAddress }
} = parseUriCommon(
this.currencyInfo,
uri,
Expand All @@ -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
}
Expand All @@ -194,11 +198,11 @@ export class PiratechainTools implements EdgeCurrencyTools {
customTokens: EdgeMetaToken[] = []
): Promise<string> {
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) {
Expand Down
Loading

0 comments on commit 444d2f2

Please sign in to comment.