Skip to content

Commit

Permalink
Wallet Add Address Spec Changes (coinbase#1513)
Browse files Browse the repository at this point in the history
* add address types

* update example

* fix types
  • Loading branch information
cb-jake authored Feb 6, 2025
1 parent 7effdc1 commit 9bfcf7d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Box, Button } from '@chakra-ui/react';
import { createCoinbaseWalletSDK } from '@coinbase/wallet-sdk';
import { createCoinbaseWalletSDK, getCryptoKeyAccount } from '@coinbase/wallet-sdk';
import React, { useCallback, useState } from 'react';

export function AddAddress({ sdk }: { sdk: ReturnType<typeof createCoinbaseWalletSDK> }) {
Expand All @@ -10,17 +10,22 @@ export function AddAddress({ sdk }: { sdk: ReturnType<typeof createCoinbaseWalle
return;
}
const provider = sdk.getProvider();
const account = await getCryptoKeyAccount();
const response = (await provider.request({
method: 'wallet_addAddress',
params: [
{
chainId: 84532,
capabilities: {
createAccount: {
signer: account.publicKey,
},
},
},
],
})) as { address: string };

console.info('customlogs: response', response);

setSubAccount(response.address);
}, [sdk]);

Expand Down
30 changes: 30 additions & 0 deletions packages/wallet-sdk/src/core/rpc/wallet_addAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Address, Hex } from 'viem';

export type WalletAddAddressCapabilities = {
createAccount?: {
signer: Hex;
};
};

export type WalletAddAddressRequest = {
method: 'wallet_addAddress';
params: [
{
version: '1';
chainId: string;
address?: string;
capabilities?: WalletAddAddressCapabilities;
},
];
};

export type WalletAddAddressResponse = {
address: Address;
chainId: number;
owners: Address[];
root: Address;
initCode: {
factory: Address;
factoryCalldata: Hex;
};
};
14 changes: 4 additions & 10 deletions packages/wallet-sdk/src/sign/scw/SCWSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ensureIntNumber, hexStringFromNumber } from ':core/type/util.js';
import { createClients, SDKChain } from ':stores/chain-clients/utils.js';
import { subaccounts } from ':stores/sub-accounts/store.js';
import { assertSubAccountInfo } from ':stores/sub-accounts/utils.js';
import { assertArrayPresence, assertPresence } from ':util/assertPresence.js';
import { assertPresence } from ':util/assertPresence.js';
import {
decryptContent,
encryptContent,
Expand Down Expand Up @@ -334,17 +334,11 @@ export class SCWSigner implements Signer {
}

await this.communicator.waitForPopupLoaded?.();
let signer = get(request, 'params[0].signer') as string;
assertPresence(state.getSigner, standardErrors.rpc.invalidParams('signer is required'));

const account = await state.getSigner();
if (!signer && account) {
signer = account.publicKey;
const address = get(request, 'params[0].address') as string;
if (address) {
throw standardErrors.rpc.invalidParams('importing an address is not yet supported');
}

assertArrayPresence(request.params);
request.params[0] = { ...request.params[0], signer };

const response = await this.sendRequestToPopup(request);
assertSubAccountInfo(response);
// Only store the sub account information after the popup has been closed and the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const params: SubAccountInfo = {
address: '0x',
chainId: 84532,
owners: [],
ownerIndex: 0,
root: '0x',
initCode: {
factory: '0x',
Expand Down
1 change: 0 additions & 1 deletion packages/wallet-sdk/src/stores/sub-accounts/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export type SubAccountInfo = {
address: Address;
chainId: number;
owners: Address[];
ownerIndex: number;
root: Address;
initCode: {
factory: Address;
Expand Down

0 comments on commit 9bfcf7d

Please sign in to comment.