Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lightMode support to Solana and EVM engines #788

Merged
merged 7 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

## Unreleased

- added: (Solana/Ethereum) Add optional `lightMode` support to disable transaction query
- added: Add optional `testPrivateKeys` function to `parseUriCommon`
- changed: (Zcash) Updated address explorer
- changed: (Tron) Special case the `usdt-trc20` uri prefix
- changed: Upgrade edge-core-js to v2.9.0
- fixed: Prevent fatal error reporting for missing txlist json file
- fixed: (Zcash) Use additional insufficient funds check before sending amount to synchronizer

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
"chai": "^4.2.0",
"clipanion": "^4.0.0-rc.2",
"crypto-browserify": "^3.12.0",
"edge-core-js": "^2.5.0",
"edge-core-js": "^2.9.0",
"esbuild-loader": "^2.20.0",
"eslint": "^8.19.0",
"eslint-config-standard-kit": "0.15.1",
Expand Down
10 changes: 5 additions & 5 deletions src/algorand/AlgorandTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ export class AlgorandTools implements EdgeCurrencyTools {
const { pluginId } = this.currencyInfo
const networks = { [pluginId]: true }

const { parsedUri, edgeParsedUri } = parseUriCommon(
this.currencyInfo,
const { parsedUri, edgeParsedUri } = await parseUriCommon({
currencyInfo: this.currencyInfo,
uri,
networks,
this.builtinTokens,
currencyCode ?? this.currencyInfo.currencyCode,
builtinTokens: this.builtinTokens,
currencyCode: currencyCode ?? this.currencyInfo.currencyCode,
customTokens
)
})

let address = ''

Expand Down
10 changes: 5 additions & 5 deletions src/binance/BinanceTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ export class BinanceTools implements EdgeCurrencyTools {
): Promise<EdgeParsedUri> {
const networks = { binance: true }

const { parsedUri, edgeParsedUri } = parseUriCommon(
this.currencyInfo,
const { parsedUri, edgeParsedUri } = await parseUriCommon({
currencyInfo: this.currencyInfo,
uri,
networks,
this.builtinTokens,
currencyCode ?? 'BNB',
builtinTokens: this.builtinTokens,
currencyCode: currencyCode ?? 'BNB',
customTokens
)
})
const address = edgeParsedUri.publicAddress ?? ''

const valid = checkAddress(address, 'bnb')
Expand Down
10 changes: 5 additions & 5 deletions src/cardano/CardanoTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ export class CardanoTools implements EdgeCurrencyTools {
): Promise<EdgeParsedUri> {
const networks = { cardano: true }

const { parsedUri, edgeParsedUri } = parseUriCommon(
this.currencyInfo,
const { parsedUri, edgeParsedUri } = await parseUriCommon({
currencyInfo: this.currencyInfo,
uri,
networks,
this.builtinTokens,
currencyCode ?? 'ADA',
builtinTokens: this.builtinTokens,
currencyCode: currencyCode ?? 'ADA',
customTokens
)
})

Cardano.Address.from_bech32(edgeParsedUri.publicAddress ?? '')

Expand Down
22 changes: 19 additions & 3 deletions src/common/CurrencyEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ import {
} from 'edge-core-js/types'

import { PluginEnvironment } from './innerPlugin'
import { makeMetaTokens, validateToken } from './tokenHelpers'
import {
getTokenIdFromCurrencyCode,
makeMetaTokens,
validateToken
} from './tokenHelpers'
import {
asWalletLocalData,
DATA_STORE_FILE,
Expand Down Expand Up @@ -568,7 +572,13 @@ export class CurrencyEngine<
this.walletLocalData.totalBalances[currencyCode] = balance
this.walletLocalDataDirty = true
this.warn(`${currencyCode}: token Address balance: ${balance}`)
this.currencyEngineCallbacks.onBalanceChanged(currencyCode, balance)
const tokenId = getTokenIdFromCurrencyCode(
currencyCode,
this.currencyInfo.currencyCode,
this.allTokensMap
)
if (tokenId === undefined) return
this.currencyEngineCallbacks.onTokenBalanceChanged(tokenId, balance)
}
this.tokenCheckBalanceStatus[currencyCode] = 1
this.updateOnAddressesChecked()
Expand Down Expand Up @@ -634,8 +644,14 @@ export class CurrencyEngine<
protected doInitialBalanceCallback(): void {
for (const currencyCode of this.enabledTokens) {
try {
this.currencyEngineCallbacks.onBalanceChanged(
const tokenId = getTokenIdFromCurrencyCode(
currencyCode,
this.currencyInfo.currencyCode,
this.allTokensMap
)
if (tokenId === undefined) continue
this.currencyEngineCallbacks.onTokenBalanceChanged(
tokenId,
this.walletLocalData.totalBalances[currencyCode] ?? '0'
)
} catch (e: any) {
Expand Down
11 changes: 9 additions & 2 deletions src/common/tokenHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { gt, lt } from 'biggystring'
import { asMaybe, asObject, asString } from 'cleaners'
import { EdgeMetaToken, EdgeToken, EdgeTokenMap } from 'edge-core-js/types'
import {
EdgeMetaToken,
EdgeToken,
EdgeTokenId,
EdgeTokenMap
} from 'edge-core-js/types'

/**
* The `networkLocation` field is untyped,
Expand Down Expand Up @@ -35,8 +40,10 @@ export function makeMetaTokens(tokens: EdgeTokenMap): EdgeMetaToken[] {

export const getTokenIdFromCurrencyCode = (
currencyCode: string,
mainnetCurrencyCode: string,
allTokensMap: EdgeTokenMap
): string | undefined => {
): EdgeTokenId | undefined => {
if (currencyCode === mainnetCurrencyCode) return null
swansontec marked this conversation as resolved.
Show resolved Hide resolved
for (const tokenId of Object.keys(allTokensMap)) {
if (allTokensMap[tokenId].currencyCode === currencyCode) return tokenId
}
Expand Down
38 changes: 29 additions & 9 deletions src/common/uriHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
EdgeEncodeUri,
EdgeMetaToken,
EdgeParsedUri,
EdgeTokenMap
EdgeTokenMap,
JsonObject
} from 'edge-core-js/types'
import { serialize } from 'uri-js'
import parse from 'url-parse'
Expand All @@ -13,14 +14,25 @@ import { getLegacyDenomination } from './utils'

type ParsedUri = parse<Record<string, string | undefined>>

export function parseUriCommon(
currencyInfo: EdgeCurrencyInfo,
uri: string,
networks: { [network: string]: boolean },
builtinTokens: EdgeTokenMap,
currencyCode?: string,
customTokens: EdgeMetaToken[] = []
): { edgeParsedUri: EdgeParsedUri; parsedUri: ParsedUri } {
export async function parseUriCommon(opts: {
currencyInfo: EdgeCurrencyInfo
uri: string
networks: { [network: string]: boolean }
builtinTokens: EdgeTokenMap
currencyCode?: string
customTokens?: EdgeMetaToken[]
testPrivateKeys?: (input: string) => Promise<JsonObject>
}): Promise<{ edgeParsedUri: EdgeParsedUri; parsedUri: ParsedUri }> {
const {
currencyInfo,
uri,
networks,
builtinTokens,
customTokens = [],
testPrivateKeys
} = opts
let { currencyCode } = opts

const parsedUri = { ...parse(uri, {}, true) }

// Add support for renproject Gateway URI type
Expand Down Expand Up @@ -90,6 +102,14 @@ export function parseUriCommon(
edgeParsedUri.currencyCode = currencyCode
}

if (testPrivateKeys != null) {
try {
await testPrivateKeys(uri)
edgeParsedUri.privateKeys = [uri]
edgeParsedUri.publicAddress = undefined
} catch (e) {}
}

return { edgeParsedUri, parsedUri }
}

Expand Down
10 changes: 5 additions & 5 deletions src/cosmos/CosmosTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,14 @@ export class CosmosTools implements EdgeCurrencyTools {
const { pluginId } = this.currencyInfo
const networks = { [pluginId]: true }

const { parsedUri, edgeParsedUri } = parseUriCommon(
this.currencyInfo,
const { parsedUri, edgeParsedUri } = await parseUriCommon({
currencyInfo: this.currencyInfo,
uri,
networks,
this.builtinTokens,
currencyCode ?? this.currencyInfo.currencyCode,
builtinTokens: this.builtinTokens,
currencyCode: currencyCode ?? this.currencyInfo.currencyCode,
customTokens
)
})

let address = ''

Expand Down
38 changes: 37 additions & 1 deletion src/declare-modules.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,46 @@
declare module 'eosjs-ecc'
declare module 'ethereumjs-abi'
declare module 'ethereumjs-util'
declare module 'ethereumjs-wallet'
declare module 'ethereumjs-wallet/hdkey'
declare module 'eztz.js'

declare module 'ethereumjs-util' {
export const isValidAddress: (address: string) => boolean
export const isValidChecksumAddress: (address: string) => boolean
export const isValidPrivate: (privateKeyBuffer: Buffer) => boolean
export const toChecksumAddress: (address: string) => string
export const pubToAddress: (
pubKey: Buffer | Uint8Array,
sanitize?: boolean
) => Buffer | Uint8Array
export const privateToAddress: (
privateKey: Buffer | Uint8Array
) => Buffer | Uint8Array

export const hashPersonalMessage: (
message: Buffer | Uint8Array | any[]
) => Buffer | Uint8Array
export const ecsign: (
msgHash: Buffer | Uint8Array,
privateKey: Buffer | Uint8Array
) => { [k: string]: any }
export const toRpcSig: (
v: number,
r: Buffer | Uint8Array,
s: Buffer | Uint8Array
) => string
export const keccak256: (
a: Buffer | Uint8Array | any[] | string | number
) => Buffer | Uint8Array
export const bufferToHex: (buf: Buffer | Uint8Array) => string
export const setLengthLeft: (
msg: Buffer | Uint8Array,
length: number,
right?: boolean
) => Buffer | Uint8Array
export const toBuffer: (v: any) => Buffer | Uint8Array
}

declare module 'react-native' {
export const NativeModules: {
EdgeCurrencyAccountbasedModule: {
Expand Down
10 changes: 5 additions & 5 deletions src/eos/EosTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,14 @@ export class EosTools implements EdgeCurrencyTools {
}

async parseUri(uri: string): Promise<EdgeParsedUri> {
const { edgeParsedUri } = parseUriCommon(
this.currencyInfo,
const { edgeParsedUri } = await parseUriCommon({
currencyInfo: this.currencyInfo,
uri,
{
networks: {
[this.networkInfo.uriProtocol]: true
},
this.builtinTokens
)
builtinTokens: this.builtinTokens
})

if (!checkAddress(edgeParsedUri.publicAddress ?? '')) {
throw new Error('InvalidPublicAddressError')
Expand Down
4 changes: 3 additions & 1 deletion src/ethereum/EthereumEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export class EthereumEngine extends CurrencyEngine<
SafeEthWalletInfo
> {
otherData!: EthereumWalletOtherData
lightMode: boolean
initOptions: EthereumInitOptions
networkInfo: EthereumNetworkInfo
ethNetwork: EthereumNetwork
Expand All @@ -113,6 +114,7 @@ export class EthereumEngine extends CurrencyEngine<
currencyInfo: EdgeCurrencyInfo
) {
super(env, tools, walletInfo, opts)
this.lightMode = opts.lightMode ?? false
this.initOptions = initOptions
this.networkInfo = env.networkInfo
this.ethNetwork = new EthereumNetwork(this)
Expand Down Expand Up @@ -991,7 +993,7 @@ export class EthereumEngine extends CurrencyEngine<
if (publicAddress == null)
throw new Error('makeSpend Missing publicAddress')
if (nativeAmount == null) throw new NoAmountSpecifiedError()
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions

if (!EthereumUtil.isValidAddress(publicAddress)) {
throw new TypeError(`Invalid ${this.currencyInfo.pluginId} address`)
}
Expand Down
26 changes: 19 additions & 7 deletions src/ethereum/EthereumNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,14 +377,26 @@ export class EthereumNetwork {

// Send an empty tokenTxs network update if no network adapters
// qualify for 'fetchTxs':
if (this.qualifyNetworkAdapters('fetchTxs').length === 0) {
if (
this.qualifyNetworkAdapters('fetchTxs').length === 0 ||
this.ethEngine.lightMode
) {
const tokenTxs: {
[currencyCode: string]: EdgeTransactionsBlockHeightTuple
} = {
[this.ethEngine.currencyInfo.currencyCode]: {
blockHeight: params.startBlock,
edgeTransactions: []
}
}
for (const token of Object.values(this.ethEngine.allTokensMap)) {
tokenTxs[token.currencyCode] = {
blockHeight: params.startBlock,
edgeTransactions: []
}
}
return {
tokenTxs: {
[this.ethEngine.currencyInfo.currencyCode]: {
blockHeight: params.startBlock,
edgeTransactions: []
}
},
tokenTxs,
server: 'none'
}
}
Expand Down
Loading
Loading