-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: revamp rpc
starkNet_extractPrivateKey
(#319)
* refactor: rename RPC method starkNet_extractPrivateKey to starkNet_displayPrivateKey * fix: use snap dialog helper * chore: revamp sign transaction * chore: add comment * chore: revamp display private key * fix: apply suggestions from code review Co-authored-by: Stanley Yuen <[email protected]> * chore: update sign detail * chore: update test * chore: remove unnecessary mock * chore: simplify test * refactor: revamp `starkNet_displayPrivateKey` * chore: update lint and unit test * chore: update display private key rpc unit test --------- Co-authored-by: Stanley Yuen <[email protected]>
- Loading branch information
1 parent
d2e92c4
commit e6eeb04
Showing
11 changed files
with
226 additions
and
311 deletions.
There are no files selected for viewing
This file contains 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 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 was deleted.
Oops, something went wrong.
This file contains 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 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
116 changes: 116 additions & 0 deletions
116
packages/starknet-snap/src/rpcs/displayPrivateKey.test.ts
This file contains 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,116 @@ | ||
import { | ||
InvalidParamsError, | ||
UserRejectedRequestError, | ||
} from '@metamask/snaps-sdk'; | ||
import { constants } from 'starknet'; | ||
|
||
import type { SnapState } from '../types/snapState'; | ||
import { STARKNET_SEPOLIA_TESTNET_NETWORK } from '../utils/constants'; | ||
import { | ||
mockAccount, | ||
prepareAlertDialog, | ||
prepareMockAccount, | ||
prepareConfirmDialog, | ||
} from './__tests__/helper'; | ||
import { displayPrivateKey } from './displayPrivateKey'; | ||
import type { DisplayPrivateKeyParams } from './displayPrivateKey'; | ||
|
||
describe('displayPrivateKey', () => { | ||
const state: SnapState = { | ||
accContracts: [], | ||
erc20Tokens: [], | ||
networks: [STARKNET_SEPOLIA_TESTNET_NETWORK], | ||
transactions: [], | ||
}; | ||
|
||
const createRequestParam = ( | ||
chainId: constants.StarknetChainId, | ||
address: string, | ||
): DisplayPrivateKeyParams => { | ||
const request: DisplayPrivateKeyParams = { | ||
chainId, | ||
address, | ||
}; | ||
return request; | ||
}; | ||
|
||
it('displays private key correctly', async () => { | ||
const chainId = constants.StarknetChainId.SN_SEPOLIA; | ||
const account = await mockAccount(chainId); | ||
prepareMockAccount(account, state); | ||
prepareConfirmDialog(); | ||
const { alertDialogSpy } = prepareAlertDialog(); | ||
|
||
const request = createRequestParam(chainId, account.address); | ||
|
||
await displayPrivateKey.execute(request); | ||
|
||
expect(alertDialogSpy).toHaveBeenCalledTimes(1); | ||
|
||
const calls = alertDialogSpy.mock.calls[0][0]; | ||
|
||
expect(calls).toStrictEqual([ | ||
{ type: 'text', value: 'Starknet Account Private Key' }, | ||
{ type: 'copyable', value: account.privateKey }, | ||
]); | ||
}); | ||
|
||
it('renders confirmation dialog', async () => { | ||
const chainId = constants.StarknetChainId.SN_SEPOLIA; | ||
const account = await mockAccount(chainId); | ||
prepareMockAccount(account, state); | ||
const { confirmDialogSpy } = prepareConfirmDialog(); | ||
prepareAlertDialog(); | ||
|
||
const request = createRequestParam(chainId, account.address); | ||
|
||
await displayPrivateKey.execute(request); | ||
|
||
expect(confirmDialogSpy).toHaveBeenCalledTimes(1); | ||
|
||
const calls = confirmDialogSpy.mock.calls[0][0]; | ||
|
||
expect(calls).toStrictEqual([ | ||
{ type: 'text', value: 'Do you want to export your private key?' }, | ||
]); | ||
}); | ||
|
||
it('throws `UserRejectedRequestError` if user denies the operation', async () => { | ||
const chainId = constants.StarknetChainId.SN_SEPOLIA; | ||
const account = await mockAccount(chainId); | ||
prepareMockAccount(account, state); | ||
const { confirmDialogSpy } = prepareConfirmDialog(); | ||
prepareAlertDialog(); | ||
|
||
confirmDialogSpy.mockResolvedValue(false); | ||
|
||
const request = createRequestParam(chainId, account.address); | ||
|
||
await expect(displayPrivateKey.execute(request)).rejects.toThrow( | ||
UserRejectedRequestError, | ||
); | ||
}); | ||
|
||
it.each([ | ||
{ | ||
case: 'user address is omitted', | ||
request: { | ||
chainId: constants.StarknetChainId.SN_SEPOLIA, | ||
}, | ||
}, | ||
{ | ||
case: 'user address is invalid', | ||
request: { | ||
chainId: constants.StarknetChainId.SN_SEPOLIA, | ||
address: 'invalid_address', | ||
}, | ||
}, | ||
])( | ||
'throws `InvalidParamsError` when $case', | ||
async ({ request }: { request: unknown }) => { | ||
await expect( | ||
displayPrivateKey.execute(request as DisplayPrivateKeyParams), | ||
).rejects.toThrow(InvalidParamsError); | ||
}, | ||
); | ||
}); |
This file contains 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,77 @@ | ||
import { copyable, text, UserRejectedRequestError } from '@metamask/snaps-sdk'; | ||
import { type Infer, object, literal, assign } from 'superstruct'; | ||
|
||
import { | ||
AccountRpcController, | ||
AddressStruct, | ||
confirmDialog, | ||
alertDialog, | ||
BaseRequestStruct, | ||
} from '../utils'; | ||
|
||
export const DisplayPrivateKeyRequestStruct = assign( | ||
object({ | ||
address: AddressStruct, | ||
}), | ||
BaseRequestStruct, | ||
); | ||
|
||
export const DisplayPrivateKeyResponseStruct = literal(null); | ||
|
||
export type DisplayPrivateKeyParams = Infer< | ||
typeof DisplayPrivateKeyRequestStruct | ||
>; | ||
|
||
export type DisplayPrivateKeyResponse = Infer< | ||
typeof DisplayPrivateKeyResponseStruct | ||
>; | ||
|
||
/** | ||
* The RPC handler to display a private key. | ||
*/ | ||
export class DisplayPrivateKeyRpc extends AccountRpcController< | ||
DisplayPrivateKeyParams, | ||
DisplayPrivateKeyResponse | ||
> { | ||
protected requestStruct = DisplayPrivateKeyRequestStruct; | ||
|
||
protected responseStruct = DisplayPrivateKeyResponseStruct; | ||
|
||
/** | ||
* Execute the display private key request handler. | ||
* The private key will be display via a confirmation dialog. | ||
* | ||
* @param params - The parameters of the request. | ||
* @param params.address - The account address. | ||
* @param params.chainId - The chain id of the network. | ||
*/ | ||
async execute( | ||
params: DisplayPrivateKeyParams, | ||
): Promise<DisplayPrivateKeyResponse> { | ||
return super.execute(params); | ||
} | ||
|
||
protected async handleRequest( | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
params: DisplayPrivateKeyParams, | ||
): Promise<DisplayPrivateKeyResponse> { | ||
const confirmComponents = [text('Do you want to export your private key?')]; | ||
|
||
if (!(await confirmDialog(confirmComponents))) { | ||
throw new UserRejectedRequestError() as unknown as Error; | ||
} | ||
|
||
const alertComponents = [ | ||
text('Starknet Account Private Key'), | ||
copyable(this.account.privateKey), | ||
]; | ||
|
||
await alertDialog(alertComponents); | ||
|
||
return null; | ||
} | ||
} | ||
|
||
export const displayPrivateKey = new DisplayPrivateKeyRpc({ | ||
showInvalidAccountAlert: false, | ||
}); |
This file contains 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './signMessage'; | ||
export * from './displayPrivateKey'; | ||
export * from './signTransaction'; |
Oops, something went wrong.