@exodus/key-utils
Advanced tools
Comparing version 3.0.0 to 3.1.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [3.1.0](https://github.com/ExodusMovement/exodus-core/compare/@exodus/key-utils@3.0.0...@exodus/key-utils@3.1.0) (2023-08-23) | ||
### Features | ||
* **key-utils:** add xpub & multi address support ([#936](https://github.com/ExodusMovement/exodus-core/issues/936)) ([b06bf9a](https://github.com/ExodusMovement/exodus-core/commit/b06bf9afa2c2aaf9b261893d34c4ddaf243d5db0)) | ||
## [3.0.0](https://github.com/ExodusMovement/exodus-core/compare/@exodus/key-utils@2.0.0...@exodus/key-utils@3.0.0) (2023-08-11) | ||
@@ -8,0 +19,0 @@ |
{ | ||
"name": "@exodus/key-utils", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"author": "Exodus Movement Inc", | ||
@@ -35,3 +35,3 @@ "description": "Utilities to manage derivation paths and key identifier aspects.", | ||
}, | ||
"gitHead": "046be7f382c2c97adb72ba304643341a5a2776dc" | ||
"gitHead": "1bb0c52adc466c8a042e3c0a63fa95d30dcde253" | ||
} |
@@ -1,4 +0,8 @@ | ||
const GENERIC_PURPOSES = [44] | ||
export const DEFAULT_PURPOSE = 44 | ||
const GENERIC_PURPOSES = [DEFAULT_PURPOSE] | ||
const BITCOIN_PURPOSES = [49, 84, 86] | ||
const CARDANO_SHELLEY_PURPOSE = [1852] | ||
export const BIP32_PURPOSES = [...GENERIC_PURPOSES, ...BITCOIN_PURPOSES, ...CARDANO_SHELLEY_PURPOSE] | ||
export const DEFAULT_CHAIN_INDEX = 0 | ||
export const DEFAULT_ADDRESS_INDEX = 0 |
@@ -30,3 +30,3 @@ import BIPPath from 'bip32-path' | ||
indexesWithoutM.every((index) => typeof index === 'string' && /^\d+(')?$/.test(index)), | ||
`derivationPath must contains number and optionally a hardening character '` | ||
`derivationPath must contain only a number and optionally a hardening character '` | ||
) | ||
@@ -44,2 +44,3 @@ | ||
} | ||
hardeningStillAllowed = hardeningStillAllowed && cur.endsWith(`'`) | ||
@@ -46,0 +47,0 @@ return { hardeningStillAllowed, violatedRule } |
import assert from 'minimalistic-assert' | ||
import { buildDerivationPath } from './derivation-path' | ||
import { DEFAULT_PURPOSE, DEFAULT_CHAIN_INDEX, DEFAULT_ADDRESS_INDEX } from './constants' | ||
@@ -19,6 +20,16 @@ export const isSafeObject = (value) => { | ||
const { allowedPurposes = [44], allowedChainIndices = [0] } = rules | ||
const { | ||
allowedPurposes = [DEFAULT_PURPOSE], | ||
allowedChainIndices = [DEFAULT_CHAIN_INDEX], | ||
allowMultipleAddresses = false, | ||
allowXPUB = false, | ||
} = rules | ||
assert(Array.isArray(allowedPurposes), 'rules.allowedPurposes has to be an array') | ||
assert(Array.isArray(allowedChainIndices), 'rules.allowedChainIndices has to be an array') | ||
assert( | ||
typeof allowMultipleAddresses === 'boolean', | ||
'rules.allowMultipleAddresses has to be a boolean' | ||
) | ||
assert(typeof allowXPUB === 'boolean', 'rules.allowXPUB has to be a boolean') | ||
@@ -34,11 +45,29 @@ const { purpose, accountIndex, chainIndex, addressIndex, compatibilityMode } = params | ||
assert(isSafeNonNegativeInteger(accountIndex), 'accountIndex must be a positive integer') | ||
assert(isSafeNonNegativeInteger(chainIndex), 'chainIndex must be a positive integer') | ||
// Account-based assets have a single address, no concept of change addresses exist there. | ||
// BIP44 specifies that chainIndex 1 should be used for change addresses. | ||
// Therefore, reject the creation of KeyIdentifiers with non-zero chainIndex values. | ||
assert( | ||
allowedChainIndices.includes(chainIndex), | ||
`setting chainIndex to value other than ${allowedChainIndices.join(', ')} is not supported` | ||
) | ||
assert(isSafeNonNegativeInteger(addressIndex), 'addressIndex must be a positive integer') | ||
if (chainIndex !== undefined) { | ||
assert(isSafeNonNegativeInteger(chainIndex), 'chainIndex must be a positive integer') | ||
assert( | ||
allowedChainIndices.includes(chainIndex), | ||
`setting chainIndex to value other than ${allowedChainIndices.join(', ')} is not supported` | ||
) | ||
} | ||
if (addressIndex !== undefined) { | ||
assert(isSafeNonNegativeInteger(addressIndex), 'addressIndex must be a positive integer') | ||
if (!allowMultipleAddresses) { | ||
assert( | ||
addressIndex === DEFAULT_ADDRESS_INDEX, | ||
'addressIndex must be zero or undefined if multiple address mode is disabled' | ||
) | ||
} | ||
} | ||
if (!allowXPUB) { | ||
assert( | ||
chainIndex !== undefined && addressIndex !== undefined, | ||
'chainIndex & addressIndex must be defined if XPUB derivation is not allowed' | ||
) | ||
} | ||
if (compatibilityMode) { | ||
@@ -58,3 +87,5 @@ assert(typeof compatibilityMode === 'string', `compatibilityMode, if defined, must be a string`) | ||
* @param [validationRules.allowedPurposes=[44]] {number[]} | ||
* @param [validationRules.allowedChainIndices=[0]] {number[]} | ||
* @param [validationRules.allowedChainIndices=[0]] {number[]} optional | ||
* @param [validationRules.allowMultipleAddresses=false] {boolean} optional | ||
* @param [validationRules.allowXPUB=false] {boolean} optional | ||
*/ | ||
@@ -71,3 +102,3 @@ export function createGetKeyIdentifier({ | ||
const { accountIndex, addressIndex, chainIndex, purpose } = params | ||
const { accountIndex, addressIndex, chainIndex, purpose = DEFAULT_PURPOSE } = params | ||
@@ -74,0 +105,0 @@ const derivationPath = buildDerivationPath({ |
11411
209