Skip to content

Commit

Permalink
feat: added specific address index in starkNet_createAccount and fixe…
Browse files Browse the repository at this point in the history
…d Dapp to always use 0 (#7)
  • Loading branch information
jonesho authored Sep 20, 2022
1 parent 08da915 commit 0607626
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 19 deletions.
4 changes: 4 additions & 0 deletions packages/starknet-snap/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ <h1>Hello, Snaps!</h1>
<form id="deployUserAccContract">
<fieldset>
<legend>Deploy User Account Contract</legend>
<label for="addressIndex">Specific Address Index (optional)</label>
<input type="text" id="addressIndex" name="addressIndex"><br>
<input type="submit" id="deployUserAccContract" value="Deploy User Account SC">
</fieldset>
</form>
Expand Down Expand Up @@ -496,6 +498,7 @@ <h1>Hello, Snaps!</h1>
async function deployUserAccContract(e) {
e.preventDefault() // to prevent default form behavior

const addressIndex = document.getElementById('addressIndex').value
const chainId = document.getElementById('targetChainId').value;
const isDev = document.getElementById('isDev').checked;

Expand All @@ -505,6 +508,7 @@ <h1>Hello, Snaps!</h1>
params: [snapId, {
method: 'starkNet_createAccount',
params: {
addressIndex,
chainId,
isDev,
},
Expand Down
10 changes: 10 additions & 0 deletions packages/starknet-snap/openrpc/starknet_snap_api_openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@
"summary": "Deploys an account contract",
"paramStructure": "by-name",
"params": [
{
"name": "addressIndex",
"summary": "Specific address index of the derived key in BIP-44",
"description": "Specific address index of the derived key in BIP-44",
"required": false,
"schema": {
"type": "integer",
"minimum": 0
}
},
{
"name": "chainId",
"summary": "Id of the target StarkNet network",
Expand Down
2 changes: 1 addition & 1 deletion 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": "tXHWgLjhxC1D60s15bVt4begaQGdCO6nJwUHvpPoqzc=",
"shasum": "9k8ZDr5ZNJOsYza0mhp3KMiKBThzs9CXAKFq7f1yR9I=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
19 changes: 11 additions & 8 deletions packages/starknet-snap/src/createAccount.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getKeysFromAddressIndex, getAccContractAddressAndCallData, deployContract } from './utils/starknetUtils';
import { getNetworkFromChainId, upsertAccount, upsertTransaction } from './utils/snapUtils';
import { getNetworkFromChainId, getValidNumber, upsertAccount, upsertTransaction } from './utils/snapUtils';
import { AccContract, VoyagerTransactionType, Transaction, TransactionStatus } from './types/snapState';
import { ApiParams, CreateAccountRequestParams } from './types/snapApi';
import { PROXY_CONTRACT_STR } from './utils/constants';
Expand All @@ -9,18 +9,21 @@ export async function createAccount(params: ApiParams) {
const { state, wallet, saveMutex, keyDeriver, requestParams } = params;
const requestParamsObj = requestParams as CreateAccountRequestParams;

const addressIndex = getValidNumber(requestParamsObj.addressIndex, -1, 0);
const network = getNetworkFromChainId(state, requestParamsObj.chainId);

const { publicKey, addressIndex, derivationPath } = await getKeysFromAddressIndex(
keyDeriver,
network.chainId,
state,
);
const {
publicKey,
addressIndex: addressIndexInUsed,
derivationPath,
} = await getKeysFromAddressIndex(keyDeriver, network.chainId, state, addressIndex);
const { address: contractAddress, callData: contractCallData } = getAccContractAddressAndCallData(
network.accountClassHash,
publicKey,
);
console.log(`createAccount:\ncontractAddress = ${contractAddress}\npublicKey = ${publicKey}`);
console.log(
`createAccount:\ncontractAddress = ${contractAddress}\npublicKey = ${publicKey}\naddressIndex = ${addressIndexInUsed}`,
);

const deployResp = await deployContract(network, PROXY_CONTRACT_STR, contractCallData, publicKey);

Expand All @@ -29,7 +32,7 @@ export async function createAccount(params: ApiParams) {
addressSalt: publicKey,
publicKey,
address: deployResp.contract_address,
addressIndex,
addressIndex: addressIndexInUsed,
derivationPath,
deployTxnHash: deployResp.transaction_hash,
chainId: network.chainId,
Expand Down
4 changes: 3 additions & 1 deletion packages/starknet-snap/src/types/snapApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export interface BaseRequestParams {
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface CreateAccountRequestParams extends BaseRequestParams {}
export interface CreateAccountRequestParams extends BaseRequestParams {
addressIndex?: string | number;
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface GetStoredUserAccountsRequestParams extends BaseRequestParams {}
Expand Down
7 changes: 6 additions & 1 deletion packages/starknet-snap/test/constants.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ export const bip44Entropy: JsonBIP44CoinTypeNode = {

export const createAccountProxyMainnetResp = {
transaction_hash: '0x3b690b4c9dd639881a46f6a344ee90254562175ed7a7f5a028f69b8c32ccb47',
contract_address: '0x464a46982528b8b038b4a81af5be9a72b05e3bb42c449adbb222c4d8a168645',
contract_address: '0x57c2c9609934e5e2a23ecc5027c65731065d255fd8ce4a7234626b9b35e8e70',
};

export const createAccountProxyMainnetResp2 = {
transaction_hash: '0x60d85f7411349c0b4bc94cf1a6659dccb945f82865592ae7aaa494fa62b6965',
contract_address: '0x7aca804cc7541b6e57f2d7d22284f41ef7b445f4560526a2c6a48398e55cf86',
};

export const createAccountProxyResp = {
Expand Down
44 changes: 36 additions & 8 deletions packages/starknet-snap/test/src/createAccount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
createAccountProxyResp,
createAccountProxyMainnetResp,
createAccountFailedProxyResp,
createAccountProxyMainnetResp2,
} from '../constants.test';
import { getAddressKeyDeriver } from '../../src/utils/keyPair';
import { Mutex } from 'async-mutex';
Expand Down Expand Up @@ -75,6 +76,33 @@ describe('Test function: createAccount', function () {
expect(state.transactions.length).to.be.eq(1);
});

it('should create and store an user account of specific address index with proxy in state correctly in mainnet', async function () {
sandbox.stub(utils, 'deployContract').callsFake(async () => {
return createAccountProxyMainnetResp2;
});
const requestObject: CreateAccountRequestParams = {
chainId: STARKNET_MAINNET_NETWORK.chainId,
addressIndex: 1,
};
apiParams.requestParams = requestObject;
const result = await createAccount(apiParams);
const { publicKey: expectedPublicKey } = await utils.getKeysFromAddress(
apiParams.keyDeriver,
STARKNET_MAINNET_NETWORK.chainId,
state,
createAccountProxyMainnetResp2.contract_address,
);
expect(walletStub.rpcStubs.snap_manageState).to.have.been.callCount(4);
expect(result.address).to.be.eq(createAccountProxyMainnetResp2.contract_address);
expect(result.transaction_hash).to.be.eq(createAccountProxyMainnetResp2.transaction_hash);
expect(state.accContracts.length).to.be.eq(2);
expect(state.accContracts[1].address).to.be.eq(createAccountProxyMainnetResp2.contract_address);
expect(state.accContracts[1].deployTxnHash).to.be.eq(createAccountProxyMainnetResp2.transaction_hash);
expect(state.accContracts[1].publicKey).to.be.eq(expectedPublicKey);
expect(state.accContracts[1].addressSalt).to.be.eq(expectedPublicKey);
expect(state.transactions.length).to.be.eq(2);
});

it('should create and store an user account with proxy in state correctly in testnet', async function () {
sandbox.stub(utils, 'deployContract').callsFake(async () => {
return createAccountProxyResp;
Expand All @@ -91,12 +119,12 @@ describe('Test function: createAccount', function () {
expect(walletStub.rpcStubs.snap_manageState).to.have.been.callCount(4);
expect(result.address).to.be.eq(createAccountProxyResp.contract_address);
expect(result.transaction_hash).to.be.eq(createAccountProxyResp.transaction_hash);
expect(state.accContracts.length).to.be.eq(2);
expect(state.accContracts[1].address).to.be.eq(createAccountProxyResp.contract_address);
expect(state.accContracts[1].deployTxnHash).to.be.eq(createAccountProxyResp.transaction_hash);
expect(state.accContracts[1].publicKey).to.be.eq(expectedPublicKey);
expect(state.accContracts[1].addressSalt).to.be.eq(expectedPublicKey);
expect(state.transactions.length).to.be.eq(2);
expect(state.accContracts.length).to.be.eq(3);
expect(state.accContracts[2].address).to.be.eq(createAccountProxyResp.contract_address);
expect(state.accContracts[2].deployTxnHash).to.be.eq(createAccountProxyResp.transaction_hash);
expect(state.accContracts[2].publicKey).to.be.eq(expectedPublicKey);
expect(state.accContracts[2].addressSalt).to.be.eq(expectedPublicKey);
expect(state.transactions.length).to.be.eq(3);
});

it('should skip upsert account and transaction if deployTxn response code has no transaction_hash in testnet', async function () {
Expand All @@ -109,8 +137,8 @@ describe('Test function: createAccount', function () {
expect(walletStub.rpcStubs.snap_manageState).to.have.been.callCount(0);
expect(result.address).to.be.eq(createAccountFailedProxyResp.contract_address);
expect(result.transaction_hash).to.be.eq(createAccountFailedProxyResp.transaction_hash);
expect(state.accContracts.length).to.be.eq(2);
expect(state.transactions.length).to.be.eq(2);
expect(state.accContracts.length).to.be.eq(3);
expect(state.transactions.length).to.be.eq(3);
});

it('should throw error if upsertAccount failed', async function () {
Expand Down
1 change: 1 addition & 0 deletions packages/wallet-ui/src/services/useStarkNetSnap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export const useStarkNetSnap = () => {
{
method: 'starkNet_createAccount',
params: {
addressIndex: 0,
chainId,
},
},
Expand Down

0 comments on commit 0607626

Please sign in to comment.