Skip to content

Commit

Permalink
Fix dPaths (#27)
Browse files Browse the repository at this point in the history
* Fix dpath

* Fix Ledger testnet dpath + add fix testnet dpaths + add dpath to dcent + document and test dpaths

* Fix comments

* Lint

Co-authored-by: Diego Grasso <[email protected]>
Co-authored-by: Ilan <[email protected]>
Co-authored-by: Ilan <[email protected]>
  • Loading branch information
4 people authored Oct 11, 2021
1 parent 0e28fd6 commit e3506f9
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 21,287 deletions.
14,310 changes: 38 additions & 14,272 deletions package-lock.json

Large diffs are not rendered by default.

3,345 changes: 20 additions & 3,325 deletions packages/rlogin-dcent-provider/package-lock.json

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions packages/rlogin-dcent-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"files": [
"dist/"
],
"scripts": {
"build": "npx webpack"
},
"scripts": {
"build": "npx webpack"
},
"repository": {
"type": "git",
"url": "git+https://github.com/rsksmart/rLogin-providers.git"
Expand All @@ -28,17 +28,17 @@
},
"homepage": "https://github.com/rsksmart/rLogin-providers#readme",
"dependencies": {
"@rsksmart/rlogin-eip1193-proxy-subprovider": "^1.0.0",
"@rsksmart/rlogin-transactions": "^1.0.0",
"@rsksmart/rlogin-dpath": "^1.0.0",
"@rsksmart/dcent-provider": "^0.3.0-beta",
"@rsksmart/rlogin-dpath": "^1.0.0",
"@rsksmart/rlogin-eip1193-proxy-subprovider": "^1.0.0",
"@rsksmart/rlogin-transactions": "^1.0.0",
"assert": "^2.0.0",
"buffer": "^6.0.3",
"dcent-provider": "^0.3.0-beta",
"url": "^0.11.0",
"util": "^0.12.4",
"stream-browserify": "^3.0.0"
},
"devDependencies": {
"@rsksmart/rlogin-eip1193-types": "^1.0.0-beta.7"
"stream-browserify": "^3.0.0",
"url": "^0.11.0",
"util": "^0.12.4"
},
"devDependencies": {
"@rsksmart/rlogin-eip1193-types": "^1.0.0-beta.7"
}
}
8 changes: 5 additions & 3 deletions packages/rlogin-dcent-provider/src/DCentProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import DCentRPCProvider from 'dcent-provider'
import DCentRPCProvider from '@rsksmart/dcent-provider'
import { getDPathByChainId } from '@rsksmart/rlogin-dpath'
import { RLoginEIP1193Provider, RLoginEIP1193ProviderOptions } from '@rsksmart/rlogin-eip1193-proxy-subprovider'
import { EthSendTransactionParams, PersonalSignParams } from '@rsksmart/rlogin-eip1193-types'
import { createTransaction } from '@rsksmart/rlogin-transactions'
Expand Down Expand Up @@ -26,12 +27,13 @@ export class DCentProvider extends RLoginEIP1193Provider {
super({ rpcUrl, chainId })

this.#debug = debug
this.path = dPath
this.path = dPath || getDPathByChainId(chainId)
this.rpcUrl = rpcUrl

this.dcentProvider = new DCentRPCProvider({
rpcUrl: this.rpcUrl,
chainId: this.chainId
chainId: this.chainId,
dpath: this.path
})
}

Expand Down
32 changes: 25 additions & 7 deletions packages/rlogin-dpath/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
export const getDPathByChainId = (chainId: number): string => {
/**
* Get the BIP-44 account derivation path for a given network and index. The network
* is the return value for RPC eth_chainId
* Standards:
* - BIP-44: https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
* - SLIP-44: https://github.com/satoshilabs/slips/blob/master/slip-0044.md
* - RSKIP-57: https://github.com/rsksmart/RSKIPs/blob/master/IPs/RSKIP57.md
* - EIP-155: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md
* - EIP-695: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-695.md
* @param chainId EIP-155 chain Id
* @param index derive different accounts based on SLIP-44
* @param isLedger use it for RSK Testnet and Ledger
* @returns the first account of standard BIP-44 derviation path forthe given network
*/

export const getDPathByChainId = (chainId: number, index: number = 0, isLedger = false): string => {
if (isLedger && chainId === 31) return `m/44'/1'/0'/0/${index}` // Ledger + RSK Testnet - based on slip-44 - 37310 does not work
switch (chainId) {
case 30: return "44'/137'/0'/0/0"
case 31:
case 1:
case 3:
case 4:
case 5: return "m/44'/60'/0'/0/0"
case 30: return `m/44'/137'/0'/0/${index}` // RSK Mainnet - based on rskip-57
case 31: return `m/44'/37310'/0'/0/${index}` // RSK Testnet - based on rskip-57
case 1: return `m/44'/60'/0'/0/${index}` // Ethereum Mainnet - based on eip-155 and slip-44
case 3:// Ropsten
case 4:// Rinkeby
case 5:// Goerli
case 42: // Kovan
return `m/44'/1'/0'/0/${index}` // Ethereum testnets - based on eip-155 and slip-44
default: throw new Error('Network not supported please specify the derivation path')
}
}
31 changes: 28 additions & 3 deletions packages/rlogin-dpath/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
describe('test', () => {
test('test', () => {
expect(true).toBeTruthy()
import { getDPathByChainId } from '../src/index'

const testCases = [
{ title: 'rsk mainnet', chainId: 30, 0: 'm/44\'/137\'/0\'/0/0', 2: 'm/44\'/137\'/0\'/0/2' },
{ title: 'rsk testnet', chainId: 31, 0: 'm/44\'/37310\'/0\'/0/0', 2: 'm/44\'/37310\'/0\'/0/2', forLedeger: 'm/44\'/1\'/0\'/0/0' },
{ title: 'ethereum', chainId: 1, 0: 'm/44\'/60\'/0\'/0/0', 2: 'm/44\'/60\'/0\'/0/2' },
...[
{ title: 'ropsten', chainId: 3 },
{ title: 'rinkeby', chainId: 4 },
{ title: 'goreli', chainId: 5 },
{ title: 'kovan', chainId: 42 }
].map(({ title, chainId }) => ({
title, chainId, 0: 'm/44\'/1\'/0\'/0/0', 2: 'm/44\'/1\'/0\'/0/2'
}))
]
describe('dpath for network id', () => {
for (const testCase of testCases) {
test(testCase.title, () => {
expect(getDPathByChainId(testCase.chainId)).toEqual(testCase['0'])
expect(getDPathByChainId(testCase.chainId, 2)).toEqual(testCase['2'])
expect(getDPathByChainId(testCase.chainId, 0, true)).toEqual(testCase.forLedeger || testCase['0'])
})
}

test('throws for other coins', () => {
expect(() => getDPathByChainId(200)).toThrow()
expect(() => getDPathByChainId(200, 2)).toThrow()
expect(() => getDPathByChainId(200, 2, true)).toThrow()
})
})
248 changes: 2 additions & 246 deletions packages/rlogin-eip1193-proxy-subprovider/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e3506f9

Please sign in to comment.