Skip to content

Commit

Permalink
feat: update to use snap_getBip44Entropy (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesho authored Oct 11, 2022
1 parent 7c37970 commit a619c66
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 32 deletions.
8 changes: 6 additions & 2 deletions packages/starknet-snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -19,7 +19,11 @@
"initialPermissions": {
"snap_confirm": {},
"endowment:network-access": {},
"snap_getBip44Entropy_9004": {},
"snap_getBip44Entropy": [
{
"coinType": 9004
}
],
"snap_manageState": {}
},
"manifestVersion": "0.1"
Expand Down
11 changes: 5 additions & 6 deletions packages/starknet-snap/src/utils/keyPair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions packages/starknet-snap/test/constants.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions packages/starknet-snap/test/src/createAccount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions packages/starknet-snap/test/src/estimateFee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
});

Expand Down
4 changes: 2 additions & 2 deletions packages/starknet-snap/test/src/extractPrivateKey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
});

Expand Down
4 changes: 2 additions & 2 deletions packages/starknet-snap/test/src/extractPublicKey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
});

Expand Down
4 changes: 2 additions & 2 deletions packages/starknet-snap/test/src/recoverAccounts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions packages/starknet-snap/test/src/sendTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions packages/starknet-snap/test/src/signMessage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/starknet-snap/test/src/verifySignedMessage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
});

Expand Down
6 changes: 3 additions & 3 deletions packages/starknet-snap/test/utils/keyPair.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand All @@ -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}`);
});

Expand All @@ -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}`);
});
});
14 changes: 9 additions & 5 deletions packages/starknet-snap/test/wallet.mock.test.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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(),
};
Expand All @@ -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);
}
Expand Down

0 comments on commit a619c66

Please sign in to comment.