-
Notifications
You must be signed in to change notification settings - Fork 55
Derive Avalanche C-Chain addresses at coin type 60 #1057
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
Open
j0ntz
wants to merge
2
commits into
master
Choose a base branch
from
jon/fix-avax-evm-derivation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import { assert } from 'chai' | ||
| import { | ||
| EdgeCorePluginOptions, | ||
| EdgeCurrencyPlugin, | ||
| EdgeCurrencyTools, | ||
| JsonObject, | ||
| makeFakeIo | ||
| } from 'edge-core-js' | ||
| import { before, describe, it } from 'mocha' | ||
| import fetch from 'node-fetch' | ||
|
|
||
| import edgeCorePlugins from '../../src/index' | ||
| import { fakeLog } from '../fake/fakeLog' | ||
|
|
||
| // A 12-word seed and the receive address it yields on EVM wallets (Exodus, | ||
| // MetaMask, Trust) which all derive at coin type 60. Ethereum, Polygon and | ||
| // Avalanche C-Chain are all EVM, so importing this seed into Edge must produce | ||
| // the same address on each. Avalanche was the regression (it derived at coin | ||
| // type 9000); Ethereum and Polygon are the chains the report flagged as most | ||
| // important, so they are locked here too. | ||
| const MNEMONIC = | ||
| 'room soda device label bicycle hill fork nest lion knee purpose hen' | ||
| const EXPECTED_EVM_ADDRESS = '0x21D45Fd06e291C49AbFa135460DE827b6579Cef5' | ||
|
|
||
| // What the same seed yields at Avalanche's old coin type 9000. Wallets created | ||
| // before the correction hold a private key derived at that path, so they must | ||
| // keep resolving to this address. | ||
| const LEGACY_AVAX_ADDRESS = '0xc0Ee5411B61513Bea1853692463e28D6c32A0b50' | ||
|
|
||
| const makeOpts = (): EdgeCorePluginOptions => { | ||
| const fakeIo = makeFakeIo() | ||
| return { | ||
| infoPayload: {}, | ||
| initOptions: {}, | ||
| io: { ...fakeIo, fetch, fetchCors: fetch }, | ||
| log: fakeLog, | ||
| nativeIo: {}, | ||
| pluginDisklet: fakeIo.disklet | ||
| } | ||
| } | ||
|
|
||
| const makeTools = async ( | ||
| pluginId: keyof typeof edgeCorePlugins | ||
| ): Promise<EdgeCurrencyTools> => { | ||
| const factory = edgeCorePlugins[pluginId] | ||
| const plugin: EdgeCurrencyPlugin = factory(makeOpts()) | ||
| return await plugin.makeCurrencyTools() | ||
| } | ||
|
|
||
| /** `EdgeCurrencyTools.importPrivateKey` is optional, but EVM plugins have it. */ | ||
| const importPrivateKey = async ( | ||
| tools: EdgeCurrencyTools, | ||
| userInput: string | ||
| ): Promise<JsonObject> => { | ||
| if (tools.importPrivateKey == null) { | ||
| throw new Error('Plugin does not support importPrivateKey') | ||
| } | ||
| return await tools.importPrivateKey(userInput) | ||
| } | ||
|
|
||
| const CASES = [ | ||
| { pluginId: 'ethereum', mnemonicKey: 'ethereumMnemonic' }, | ||
| { pluginId: 'polygon', mnemonicKey: 'polygonMnemonic' }, | ||
| { pluginId: 'avalanche', mnemonicKey: 'avalancheMnemonic' } | ||
| ] as const | ||
|
|
||
| describe('EVM derivation parity (coin type 60)', function () { | ||
| for (const { pluginId, mnemonicKey } of CASES) { | ||
| describe(pluginId, function () { | ||
| let tools: EdgeCurrencyTools | ||
|
|
||
| before('Tools', async function () { | ||
| tools = await makeTools(pluginId) | ||
| }) | ||
|
|
||
| it('derives the Exodus-matching EVM address from an imported seed', async function () { | ||
| const importedKeys = await importPrivateKey(tools, MNEMONIC) | ||
| assert.equal(importedKeys[mnemonicKey], MNEMONIC) | ||
| assert.equal(importedKeys.derivationPath, "m/44'/60'/0'/0/0") | ||
|
|
||
| const keys = await tools.derivePublicKey({ | ||
| id: 'id', | ||
| keys: importedKeys, | ||
| type: `wallet:${pluginId}` | ||
| }) | ||
| assert.equal(keys.publicKey, EXPECTED_EVM_ADDRESS) | ||
| }) | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| describe('Avalanche legacy wallets', function () { | ||
| let tools: EdgeCurrencyTools | ||
|
|
||
| before('Tools', async function () { | ||
| tools = await makeTools('avalanche') | ||
| }) | ||
|
|
||
| it('keeps the coin type 9000 address when no path was saved', async function () { | ||
| // The shape of a wallet created before the plugin saved `derivationPath`: | ||
| // its stored private key was derived at coin type 9000, so re-deriving the | ||
| // public key on a new device has to land on the same address. | ||
| const keys = await tools.derivePublicKey({ | ||
| id: 'id', | ||
| keys: { avalancheMnemonic: MNEMONIC }, | ||
| type: 'wallet:avalanche' | ||
| }) | ||
| assert.equal(keys.publicKey, LEGACY_AVAX_ADDRESS) | ||
| }) | ||
|
|
||
| it('derives the private key at the saved path', async function () { | ||
| const importedKeys = await importPrivateKey(tools, MNEMONIC) | ||
| const publicKeys = await tools.derivePublicKey({ | ||
| id: 'id', | ||
| // Only the private key, as if the mnemonic were never saved: | ||
| keys: { avalancheKey: importedKeys.avalancheKey }, | ||
| type: 'wallet:avalanche' | ||
| }) | ||
| assert.equal( | ||
| publicKeys.publicKey.toLowerCase(), | ||
| EXPECTED_EVM_ADDRESS.toLowerCase() | ||
| ) | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.