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

Ts/evm wallet changes #74

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [4.14.1](https://github.com/nash-io/nash-protocol/compare/v4.13.7...v4.14.1) (2024-12-16)

### [4.13.7](https://github.com/nash-io/nash-protocol/compare/v4.13.5...v4.13.7) (2024-09-23)

### [4.13.5](https://github.com/nash-io/nash-protocol/compare/v4.13.3...v4.13.5) (2024-08-02)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@neon-exchange/nash-protocol",
"version": "4.13.7",
"version": "4.14.1",
"description": "TypeScript implementation of Nash crypto routines",
"main": "build/main/index.js",
"typings": "build/main/index.d.ts",
Expand Down
14 changes: 14 additions & 0 deletions src/__tests__/testVectors.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@
"address": "cA7E5135e04371D048A78C4592BA3b61A984B563"
}
],
"evm": [
{
"index": 0,
"privateKey": "0b7561f1c775ef4715accd21aac8629bcda2e6eb4e434c0d55c917bd7c297ac6",
"publicKey": "04be641c583207c310739a23973fb7cb7336d2b835517ede791e9fa53fa5b0fc46390ebb4dab62e8b01352f37308dbff1512615856bffd3c752db95737d3bc93a4",
"address": "55D16CA38dFB219141Ab6617B2872B978aF84702"
},
{
"index": 1,
"privateKey": "e537b63096749e62e509ef4e849f739bff129baeeb100d131a681682422392cf",
"publicKey": "0475f8a0f4eda35b7194d265df33720cf80164f196765980fae29795f713b340d93a0fa0632f56b265450c8d9d3a631c9a79089a57f40316bb3b66a09c51ba9fcb",
"address": "cA7E5135e04371D048A78C4592BA3b61A984B563"
}
],
"btc": [
{
"index": 0,
Expand Down
33 changes: 33 additions & 0 deletions src/generateWallet/generateWallet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,39 @@ test('generates deterministic BIP44 ETH keys', async () => {
}
})

test('generates deterministic BIP44 EVM keys', async () => {
for (const vector of testVectors) {
const masterSeed = Buffer.from(vector.masterSeed, 'hex')

const walletZeroIndex = vector.wallets.evm[0]
const walletOneIndex = vector.wallets.evm[1]

// with a legacy index, ETH should use wallet with index 1
let wallet = generateWallet(masterSeed, CoinType.ETH, 1)
expect(wallet.address).toBe(walletOneIndex.address.toLowerCase())

// with non legacy index, ETH should use wallet with index 0
wallet = generateWallet(masterSeed, CoinType.ETH, 0)
expect(wallet.address).toBe(walletZeroIndex.address.toLowerCase())

// with a legacy index, MATIC should not be the same as ETH wallet
wallet = generateWallet(masterSeed, CoinType.POLYGON, 1)
expect(wallet.address).not.toBe(walletOneIndex.address.toLowerCase())

// with non legacy index, MATIC should be the same as the eth wallet
wallet = generateWallet(masterSeed, CoinType.POLYGON, 0)
expect(wallet.address).toBe(walletZeroIndex.address.toLowerCase())

// same with Arbitrum, mantle, etc
wallet = generateWallet(masterSeed, CoinType.MANTLE, 0)
expect(wallet.address).toBe(walletZeroIndex.address.toLowerCase())
wallet = generateWallet(masterSeed, CoinType.ABRITRUM, 0)
expect(wallet.address).toBe(walletZeroIndex.address.toLowerCase())
wallet = generateWallet(masterSeed, CoinType.OPTIMISM, 0)
expect(wallet.address).toBe(walletZeroIndex.address.toLowerCase())
}
})

test('generates deterministic BIP44 NEO keys', async () => {
for (const vector of testVectors) {
const masterSeed = Buffer.from(vector.masterSeed, 'hex')
Expand Down
31 changes: 30 additions & 1 deletion src/generateWallet/generateWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,19 @@ export function generateWallet(
net?: string,
blockchain?: Blockchain
): Wallet {
const key = derivePath(masterSeed, bip44Purpose, coinType, 0, 0)
let coinTypeToUseForWalletPathDerivation: CoinType = coinType
// legacy wallets used 1 as an index, which while not incorrect is not what most BIP44 implementations do
// going forward we will use 0 instead.
// however, the value of 0 can give us an idea that we want to use the
// same address for all EVM wallets on non-legacy accounts
// So if the index is 0, we will derive the path to always be the ETH coin type wallet for EVM wallets
// otherwise use non eth ( MATIC, ARBITRUM, etc ) coin type

if (index === 0 && isEVM(coinType)) {
coinTypeToUseForWalletPathDerivation = CoinType.ETH
}

const key = derivePath(masterSeed, bip44Purpose, coinTypeToUseForWalletPathDerivation, 0, 0)
const derivedChainKey = deriveIndex(key, index)

return generateWalletForCoinType(derivedChainKey, coinType, index, net, blockchain)
Expand Down Expand Up @@ -292,6 +304,23 @@ const bitcoinAddressFromPublicKey = (publicKey: Buffer, type: CoinType, net: str
}).address as string
}

const isEVM = (coinType: CoinType): boolean => {
switch (coinType) {
case CoinType.ETH:
case CoinType.ETC:
case CoinType.AVAXC:
case CoinType.POLYGON:
case CoinType.ABRITRUM:
case CoinType.NEO_X:
case CoinType.BNB:
case CoinType.BASE:
case CoinType.OPTIMISM:
case CoinType.MANTLE:
return true
}
return false
}

const bitcoinNetworkFromString = (type: CoinType, net: string | undefined): Bitcoin.Network => {
switch (type) {
case CoinType.BTC:
Expand Down
Loading