diff --git a/packages/starknet-snap/snap.manifest.json b/packages/starknet-snap/snap.manifest.json index 24300641..5a1cfced 100644 --- a/packages/starknet-snap/snap.manifest.json +++ b/packages/starknet-snap/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/ConsenSys/starknet-snap.git" }, "source": { - "shasum": "ZjVbz5xyzjNC8nS3bLJz3+C6VqHWxceozLOXUwk05Pk=", + "shasum": "yFBG+wOgTgEHVwEZgQ6MiZhPuyQa3vd7v/R6fKjSIVM=", "location": { "npm": { "filePath": "dist/bundle.js", @@ -19,7 +19,11 @@ "initialPermissions": { "snap_confirm": {}, "endowment:network-access": {}, - "snap_getBip44Entropy_9004": {}, + "snap_getBip44Entropy": [ + { + "coinType": 9004 + } + ], "snap_manageState": {} }, "manifestVersion": "0.1" diff --git a/packages/starknet-snap/src/utils/keyPair.ts b/packages/starknet-snap/src/utils/keyPair.ts index 14b4f8f6..b0c1b339 100644 --- a/packages/starknet-snap/src/utils/keyPair.ts +++ b/packages/starknet-snap/src/utils/keyPair.ts @@ -4,16 +4,15 @@ import { number } from 'starknet'; export async function getAddressKeyDeriver(wallet) { const bip44Node = await wallet.request({ - method: `snap_getBip44Entropy_9004`, - params: [], + method: 'snap_getBip44Entropy', + params: { + coinType: 9004, + }, }); //`m / purpose' / coin_type' / account' / change / address_index` //`m / 44' / 9004' / 0' / 0 / {index}` - return getBIP44AddressKeyDeriver(bip44Node, { - account: 0, - change: 0, - }); + return getBIP44AddressKeyDeriver(bip44Node); } export async function getAddressKey(keyDeriver, addressIndex = 0) { diff --git a/packages/starknet-snap/test/constants.test.ts b/packages/starknet-snap/test/constants.test.ts index 5b1fb70c..5b05cc67 100644 --- a/packages/starknet-snap/test/constants.test.ts +++ b/packages/starknet-snap/test/constants.test.ts @@ -99,6 +99,14 @@ export const bip44Entropy: JsonBIP44CoinTypeNode = { path: "m / bip32:44' / bip32:9004'", }; +export const getBip44EntropyStub = async (...args: unknown[]) => { + if (args?.[0]?.['coinType'] === 9004) { + return bip44Entropy; + } else { + return null; + } +}; + export const createAccountProxyMainnetResp = { transaction_hash: '0x3b690b4c9dd639881a46f6a344ee90254562175ed7a7f5a028f69b8c32ccb47', contract_address: '0x57c2c9609934e5e2a23ecc5027c65731065d255fd8ce4a7234626b9b35e8e70', diff --git a/packages/starknet-snap/test/src/createAccount.test.ts b/packages/starknet-snap/test/src/createAccount.test.ts index 601b2663..c7b6b039 100644 --- a/packages/starknet-snap/test/src/createAccount.test.ts +++ b/packages/starknet-snap/test/src/createAccount.test.ts @@ -8,12 +8,12 @@ import { createAccount } from '../../src/createAccount'; import { SnapState } from '../../src/types/snapState'; import { STARKNET_MAINNET_NETWORK, STARKNET_TESTNET_NETWORK } from '../../src/utils/constants'; import { - bip44Entropy, createAccountProxyTxn, createAccountProxyResp, createAccountProxyMainnetResp, createAccountFailedProxyResp, createAccountProxyMainnetResp2, + getBip44EntropyStub, } from '../constants.test'; import { getAddressKeyDeriver } from '../../src/utils/keyPair'; import { Mutex } from 'async-mutex'; @@ -39,7 +39,7 @@ describe('Test function: createAccount', function () { }; beforeEach(async function () { - walletStub.rpcStubs.snap_getBip44Entropy_9004.resolves(bip44Entropy); + walletStub.rpcStubs.snap_getBip44Entropy.callsFake(getBip44EntropyStub); apiParams.keyDeriver = await getAddressKeyDeriver(walletStub); sandbox.useFakeTimers(createAccountProxyTxn.timestamp); walletStub.rpcStubs.snap_manageState.resolves(state); diff --git a/packages/starknet-snap/test/src/estimateFee.test.ts b/packages/starknet-snap/test/src/estimateFee.test.ts index 15110f4e..2331163d 100644 --- a/packages/starknet-snap/test/src/estimateFee.test.ts +++ b/packages/starknet-snap/test/src/estimateFee.test.ts @@ -7,7 +7,7 @@ import { estimateFee } from '../../src/estimateFee'; import { SnapState } from '../../src/types/snapState'; import { STARKNET_TESTNET_NETWORK } from '../../src/utils/constants'; import { getAddressKeyDeriver } from '../../src/utils/keyPair'; -import { account2, bip44Entropy, estimateFeeResp, estimateFeeResp2 } from '../constants.test'; +import { account2, estimateFeeResp, estimateFeeResp2, getBip44EntropyStub } from '../constants.test'; import { Mutex } from 'async-mutex'; import { ApiParams, EstimateFeeRequestParams } from '../../src/types/snapApi'; @@ -37,7 +37,7 @@ describe('Test function: estimateFee', function () { }; beforeEach(async function () { - walletStub.rpcStubs.snap_getBip44Entropy_9004.resolves(bip44Entropy); + walletStub.rpcStubs.snap_getBip44Entropy.callsFake(getBip44EntropyStub); apiParams.keyDeriver = await getAddressKeyDeriver(walletStub); }); diff --git a/packages/starknet-snap/test/src/extractPrivateKey.test.ts b/packages/starknet-snap/test/src/extractPrivateKey.test.ts index fc1744b9..3dc737b4 100644 --- a/packages/starknet-snap/test/src/extractPrivateKey.test.ts +++ b/packages/starknet-snap/test/src/extractPrivateKey.test.ts @@ -5,7 +5,7 @@ import { WalletMock } from '../wallet.mock.test'; import { SnapState } from '../../src/types/snapState'; import { extractPrivateKey } from '../../src/extractPrivateKey'; import { STARKNET_TESTNET_NETWORK } from '../../src/utils/constants'; -import { account1, bip44Entropy, unfoundUserAddress } from '../constants.test'; +import { account1, getBip44EntropyStub, unfoundUserAddress } from '../constants.test'; import { getAddressKeyDeriver } from '../../src/utils/keyPair'; import * as utils from '../../src/utils/starknetUtils'; import { Mutex } from 'async-mutex'; @@ -30,7 +30,7 @@ describe('Test function: extractPrivateKey', function () { }; beforeEach(async function () { - walletStub.rpcStubs.snap_getBip44Entropy_9004.resolves(bip44Entropy); + walletStub.rpcStubs.snap_getBip44Entropy.callsFake(getBip44EntropyStub); apiParams.keyDeriver = await getAddressKeyDeriver(walletStub); }); diff --git a/packages/starknet-snap/test/src/extractPublicKey.test.ts b/packages/starknet-snap/test/src/extractPublicKey.test.ts index e45d1774..0039af89 100644 --- a/packages/starknet-snap/test/src/extractPublicKey.test.ts +++ b/packages/starknet-snap/test/src/extractPublicKey.test.ts @@ -5,7 +5,7 @@ import { WalletMock } from '../wallet.mock.test'; import { SnapState } from '../../src/types/snapState'; import { extractPublicKey } from '../../src/extractPublicKey'; import { STARKNET_TESTNET_NETWORK } from '../../src/utils/constants'; -import { account1, bip44Entropy, unfoundUserAddress, unfoundUserPublicKey } from '../constants.test'; +import { account1, getBip44EntropyStub, unfoundUserAddress, unfoundUserPublicKey } from '../constants.test'; import { getAddressKeyDeriver } from '../../src/utils/keyPair'; import * as utils from '../../src/utils/starknetUtils'; import { Mutex } from 'async-mutex'; @@ -30,7 +30,7 @@ describe('Test function: extractPublicKey', function () { }; beforeEach(async function () { - walletStub.rpcStubs.snap_getBip44Entropy_9004.resolves(bip44Entropy); + walletStub.rpcStubs.snap_getBip44Entropy.callsFake(getBip44EntropyStub); apiParams.keyDeriver = await getAddressKeyDeriver(walletStub); }); diff --git a/packages/starknet-snap/test/src/recoverAccounts.test.ts b/packages/starknet-snap/test/src/recoverAccounts.test.ts index 3098df32..ac8f6173 100644 --- a/packages/starknet-snap/test/src/recoverAccounts.test.ts +++ b/packages/starknet-snap/test/src/recoverAccounts.test.ts @@ -7,13 +7,13 @@ import * as snapUtils from '../../src/utils/snapUtils'; import { SnapState } from '../../src/types/snapState'; import { STARKNET_MAINNET_NETWORK, STARKNET_TESTNET_NETWORK } from '../../src/utils/constants'; import { - bip44Entropy, createAccountProxyTxn, testnetAccAddresses, testnetPublicKeys, mainnetPublicKeys, mainnetAccAddresses, invalidNetwork as INVALID_NETWORK, + getBip44EntropyStub, } from '../constants.test'; import { getAddressKeyDeriver } from '../../src/utils/keyPair'; import { recoverAccounts } from '../../src/recoverAccounts'; @@ -41,7 +41,7 @@ describe('Test function: recoverAccounts', function () { }; beforeEach(async function () { - walletStub.rpcStubs.snap_getBip44Entropy_9004.resolves(bip44Entropy); + walletStub.rpcStubs.snap_getBip44Entropy.callsFake(getBip44EntropyStub); apiParams.keyDeriver = await getAddressKeyDeriver(walletStub); sandbox.useFakeTimers(createAccountProxyTxn.timestamp); walletStub.rpcStubs.snap_manageState.resolves(state); diff --git a/packages/starknet-snap/test/src/sendTransaction.test.ts b/packages/starknet-snap/test/src/sendTransaction.test.ts index 3eef32cb..a8f6bf24 100644 --- a/packages/starknet-snap/test/src/sendTransaction.test.ts +++ b/packages/starknet-snap/test/src/sendTransaction.test.ts @@ -9,8 +9,8 @@ import { sendTransaction } from '../../src/sendTransaction'; import { STARKNET_TESTNET_NETWORK } from '../../src/utils/constants'; import { account1, - bip44Entropy, estimateFeeResp, + getBip44EntropyStub, sendTransactionFailedResp, sendTransactionResp, token2, @@ -42,7 +42,7 @@ describe('Test function: sendTransaction', function () { let executeTxnResp; beforeEach(async function () { - walletStub.rpcStubs.snap_getBip44Entropy_9004.resolves(bip44Entropy); + walletStub.rpcStubs.snap_getBip44Entropy.callsFake(getBip44EntropyStub); apiParams.keyDeriver = await getAddressKeyDeriver(walletStub); sandbox.stub(utils, 'estimateFee').callsFake(async () => { return estimateFeeResp; diff --git a/packages/starknet-snap/test/src/signMessage.test.ts b/packages/starknet-snap/test/src/signMessage.test.ts index 6b790163..4dd08cf2 100644 --- a/packages/starknet-snap/test/src/signMessage.test.ts +++ b/packages/starknet-snap/test/src/signMessage.test.ts @@ -6,7 +6,7 @@ import { SnapState } from '../../src/types/snapState'; import { signMessage } from '../../src/signMessage'; import typedDataExample from '../../src/typedData/typedDataExample.json'; import { STARKNET_TESTNET_NETWORK } from '../../src/utils/constants'; -import { account1, bip44Entropy, signature1, signature2, unfoundUserAddress } from '../constants.test'; +import { account1, getBip44EntropyStub, signature1, signature2, unfoundUserAddress } from '../constants.test'; import { getAddressKeyDeriver } from '../../src/utils/keyPair'; import * as utils from '../../src/utils/starknetUtils'; import { Mutex } from 'async-mutex'; @@ -32,7 +32,7 @@ describe('Test function: signMessage', function () { }; beforeEach(async function () { - walletStub.rpcStubs.snap_getBip44Entropy_9004.resolves(bip44Entropy); + walletStub.rpcStubs.snap_getBip44Entropy.callsFake(getBip44EntropyStub); apiParams.keyDeriver = await getAddressKeyDeriver(walletStub); walletStub.rpcStubs.snap_confirm.resolves(true); }); diff --git a/packages/starknet-snap/test/src/verifySignedMessage.test.ts b/packages/starknet-snap/test/src/verifySignedMessage.test.ts index 5bd0b943..6b1de10a 100644 --- a/packages/starknet-snap/test/src/verifySignedMessage.test.ts +++ b/packages/starknet-snap/test/src/verifySignedMessage.test.ts @@ -6,7 +6,7 @@ import { SnapState } from '../../src/types/snapState'; import typedDataExample from '../../src/typedData/typedDataExample.json'; import { verifySignedMessage } from '../../src/verifySignedMessage'; import { STARKNET_TESTNET_NETWORK } from '../../src/utils/constants'; -import { account1, bip44Entropy, signature1, signature2, unfoundUserAddress } from '../constants.test'; +import { account1, getBip44EntropyStub, signature1, signature2, unfoundUserAddress } from '../constants.test'; import { getAddressKeyDeriver } from '../../src/utils/keyPair'; import * as utils from '../../src/utils/starknetUtils'; import { Mutex } from 'async-mutex'; @@ -32,7 +32,7 @@ describe('Test function: verifySignedMessage', function () { }; beforeEach(async function () { - walletStub.rpcStubs.snap_getBip44Entropy_9004.resolves(bip44Entropy); + walletStub.rpcStubs.snap_getBip44Entropy.callsFake(getBip44EntropyStub); apiParams.keyDeriver = await getAddressKeyDeriver(walletStub); }); diff --git a/packages/starknet-snap/test/utils/keyPair.test.ts b/packages/starknet-snap/test/utils/keyPair.test.ts index 8c985d69..527b94b1 100644 --- a/packages/starknet-snap/test/utils/keyPair.test.ts +++ b/packages/starknet-snap/test/utils/keyPair.test.ts @@ -15,7 +15,7 @@ describe('Test function: getAddressKey', function () { let keyDeriver; beforeEach(async function () { - walletStub.rpcStubs.snap_getBip44Entropy_9004.resolves(bip44Entropy); + walletStub.rpcStubs.snap_getBip44Entropy.resolves(bip44Entropy); keyDeriver = await getAddressKeyDeriver(walletStub); }); @@ -42,7 +42,7 @@ describe('Test function: getAddressKey', function () { const addressKey = `0x${privateKey}${chainCode}`; const expectedResult = keyDerivation.grindKey(addressKey, ec.n); const result = await getAddressKey(keyDeriver); - expect(walletStub.rpcStubs.snap_getBip44Entropy_9004).to.have.been.calledOnce; + expect(walletStub.rpcStubs.snap_getBip44Entropy).to.have.been.calledOnce; expect(result.addressKey).to.be.eql(`0x${expectedResult}`); }); @@ -54,7 +54,7 @@ describe('Test function: getAddressKey', function () { const addressKey = `0x${privateKey}${chainCode}`; const expectedResult = keyDerivation.grindKey(addressKey, ec.n); const result = await getAddressKey(keyDeriver, addressIndex); - expect(walletStub.rpcStubs.snap_getBip44Entropy_9004).to.have.been.calledOnce; + expect(walletStub.rpcStubs.snap_getBip44Entropy).to.have.been.calledOnce; expect(result.addressKey).to.be.eql(`0x${expectedResult}`); }); }); diff --git a/packages/starknet-snap/test/wallet.mock.test.ts b/packages/starknet-snap/test/wallet.mock.test.ts index 8824e051..bd25d68e 100644 --- a/packages/starknet-snap/test/wallet.mock.test.ts +++ b/packages/starknet-snap/test/wallet.mock.test.ts @@ -1,7 +1,7 @@ import sinon from 'sinon'; export interface Wallet { registerRpcMessageHandler: (fn) => unknown; - request(options: { method: string; params?: unknown[] }): unknown; + request(options: { method: string; params?: { [key: string]: unknown } | unknown[] }): unknown; } export class WalletMock implements Wallet { @@ -10,7 +10,7 @@ export class WalletMock implements Wallet { public readonly requestStub = sinon.stub(); public readonly rpcStubs = { - snap_getBip44Entropy_9004: sinon.stub(), + snap_getBip44Entropy: sinon.stub(), snap_confirm: sinon.stub(), snap_manageState: sinon.stub(), }; @@ -19,10 +19,14 @@ export class WalletMock implements Wallet { * Calls this.requestStub or this.rpcStubs[req.method], if the method has * a dedicated stub. */ - public request(args: { method: string; params: unknown[] }): unknown { - const { method, params = [] } = args; + public request(args: { method: string; params: { [key: string]: unknown } | unknown[] }): unknown { + const { method, params } = args; if (Object.hasOwnProperty.call(this.rpcStubs, method)) { - return this.rpcStubs[method](...params); + if (Array.isArray(params)) { + return this.rpcStubs[method](...params); + } else { + return this.rpcStubs[method](params); + } } return this.requestStub(args); }