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

Format Cleanup #280

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
4 changes: 3 additions & 1 deletion src/common/plugin/makeCurrencyTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { parsePathname } from '../utxobased/engine/utils'
import {
asNumbWalletInfo,
asPrivateKey,
getPrimaryFormat,
PrivateKey
} from '../utxobased/keymanager/cleaners'
import { EncodeUriMetadata, ExtendedParseUri, PluginInfo } from './types'
Expand All @@ -42,10 +43,11 @@ export function makeCurrencyTools(
opts?: JsonObject
): Promise<JsonObject> {
const mnemonic = bip39.entropyToMnemonic(Buffer.from(io.random(32)))
const currencyFormats = engineInfo.formats ?? (['bip44'] as ['bip44'])

const privateKey: PrivateKey = {
seed: mnemonic,
format: opts?.format ?? engineInfo.formats?.[0] ?? 'bip44',
format: getPrimaryFormat(currencyFormats, currencyFormats),
coinType: opts?.coinType ?? coinInfo.coinType ?? 0
}

Expand Down
4 changes: 2 additions & 2 deletions src/common/utxobased/engine/makeUtxoEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ export async function makeUtxoEngine(
},

getDisplayPublicSeed(): string | null {
const xpubs = publicKey.publicKeys
return Object.values(xpubs).join('\n')
const xpubs = Object.values(publicKey.publicKeys)
return xpubs.length > 0 ? xpubs.join('\n') : null
},

async getEnabledTokens(): Promise<string[]> {
Expand Down
41 changes: 31 additions & 10 deletions src/common/utxobased/keymanager/cleaners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,31 @@ export const getSupportedFormats = (
}
}

/**
* An algorithm that defines the primary format for a wallet given all of the
* wallet's formats and the currency formats (from plugin info).
*
* The purpose for this algorithm is to deterministically return a single
* primary format regardless of the order of the wallet formats given.
* The algorithm effectively takes the first format in the formats from plugin
* info that matches one of the formats in the given wallet formats.
*
* If no wallet format matches any of the formats in the plugin info, then
* the primary format is the first format in the wallet format after sorting
* in ascending order lexicographically.
*/
export const getPrimaryFormat = (
currencyFormats: CurrencyFormat[],
walletFormats: CurrencyFormat[]
): CurrencyFormat => {
return (
(currencyFormats.length > 0
? currencyFormats.find(format => walletFormats.includes(format))
: undefined) ??
walletFormats.sort((a, b) => (a === b ? 0 : a > b ? 1 : -1))[0]
)
}

/**
* A cleaner that desensitizes the walletInfo object, excluding sensitive
* keys (seed/mnemonic, sync key, data key, etc). By using this object type
Expand Down Expand Up @@ -147,16 +172,10 @@ export const asNumbWalletInfo = (
if (walletFormats.length === 0) {
throw new Error('Missing wallet public keys')
}

// Search the engineInfo's formats array for the first format that exists
// in the publicKey data.
// If there are no defined formats in the engineInfo, then fallback to the
// first format in the publicKey after sorting alphabetically.
const primaryFormat =
(engineInfo.formats != null && engineInfo.formats.length > 0
? engineInfo.formats.find(format => walletFormats.includes(format))
: undefined) ??
walletFormats.sort((a, b) => (a === b ? 0 : a > b ? 1 : -1))[0]
const primaryFormat = getPrimaryFormat(
engineInfo.formats ?? [],
walletFormats
)

return {
id,
Expand All @@ -181,6 +200,8 @@ export const asNumbWalletInfo = (
id,
type,
keys: {
// Private key format is the primary format because it was determined
// during `createPrivateKey` phase.
primaryFormat: privateKey.format,
walletFormats,
publicKey: { publicKeys: publicKey }
Expand Down