-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactor verify sign message api (#332)
- Loading branch information
1 parent
b0d6151
commit 75237eb
Showing
9 changed files
with
198 additions
and
269 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 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,3 +1,4 @@ | ||
export * from './signMessage'; | ||
export * from './displayPrivateKey'; | ||
export * from './signTransaction'; | ||
export * from './verify-signature'; |
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,75 @@ | ||
import { InvalidParamsError } from '@metamask/snaps-sdk'; | ||
import { constants } from 'starknet'; | ||
|
||
import typedDataExample from '../__tests__/fixture/typedDataExample.json'; | ||
import type { SnapState } from '../types/snapState'; | ||
import { STARKNET_SEPOLIA_TESTNET_NETWORK } from '../utils/constants'; | ||
import * as starknetUtils from '../utils/starknetUtils'; | ||
import { mockAccount, prepareMockAccount } from './__tests__/helper'; | ||
import { verifySignature } from './verify-signature'; | ||
import type { VerifySignatureParams } from './verify-signature'; | ||
|
||
jest.mock('../utils/snap'); | ||
jest.mock('../utils/logger'); | ||
|
||
describe('verifySignature', () => { | ||
const state: SnapState = { | ||
accContracts: [], | ||
erc20Tokens: [], | ||
networks: [STARKNET_SEPOLIA_TESTNET_NETWORK], | ||
transactions: [], | ||
}; | ||
|
||
it('returns true if the signature is correct', async () => { | ||
const account = await mockAccount(constants.StarknetChainId.SN_SEPOLIA); | ||
prepareMockAccount(account, state); | ||
|
||
const signature = await starknetUtils.signMessage( | ||
account.privateKey, | ||
typedDataExample, | ||
account.address, | ||
); | ||
|
||
const request = { | ||
chainId: constants.StarknetChainId.SN_SEPOLIA, | ||
address: account.address, | ||
typedDataMessage: typedDataExample, | ||
signature, | ||
}; | ||
|
||
const result = await verifySignature.execute(request); | ||
|
||
expect(result).toBe(true); | ||
}); | ||
|
||
it('returns false if the signature is incorrect', async () => { | ||
const account = await mockAccount(constants.StarknetChainId.SN_SEPOLIA); | ||
const invalidSignatueAccount = await mockAccount( | ||
constants.StarknetChainId.SN_MAIN, | ||
); | ||
prepareMockAccount(account, state); | ||
|
||
const invalidSignatue = await starknetUtils.signMessage( | ||
invalidSignatueAccount.privateKey, | ||
typedDataExample, | ||
invalidSignatueAccount.address, | ||
); | ||
|
||
const request = { | ||
chainId: constants.StarknetChainId.SN_SEPOLIA, | ||
address: account.address, | ||
typedDataMessage: typedDataExample, | ||
signature: invalidSignatue, | ||
}; | ||
|
||
const result = await verifySignature.execute(request); | ||
|
||
expect(result).toBe(false); | ||
}); | ||
|
||
it('throws `InvalidParamsError` when request parameter is not correct', async () => { | ||
await expect( | ||
verifySignature.execute({} as unknown as VerifySignatureParams), | ||
).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,73 @@ | ||
import { HexStruct } from '@metamask/utils'; | ||
import type { Infer } from 'superstruct'; | ||
import { object, assign, boolean, array } from 'superstruct'; | ||
|
||
import { | ||
AddressStruct, | ||
TypeDataStruct, | ||
BaseRequestStruct, | ||
AccountRpcController, | ||
} from '../utils'; | ||
import { verifyTypedDataMessageSignature } from '../utils/starknetUtils'; | ||
|
||
export const VerifySignatureRequestStruct = assign( | ||
object({ | ||
address: AddressStruct, | ||
typedDataMessage: TypeDataStruct, | ||
signature: array(HexStruct), | ||
}), | ||
BaseRequestStruct, | ||
); | ||
|
||
export const VerifySignatureResponseStruct = boolean(); | ||
|
||
export type VerifySignatureParams = Infer<typeof VerifySignatureRequestStruct>; | ||
|
||
export type VerifySignatureResponse = Infer< | ||
typeof VerifySignatureResponseStruct | ||
>; | ||
|
||
/** | ||
* The RPC handler to verify a signature. | ||
*/ | ||
export class VerifySignatureRpc extends AccountRpcController< | ||
VerifySignatureParams, | ||
VerifySignatureResponse | ||
> { | ||
protected requestStruct = VerifySignatureRequestStruct; | ||
|
||
protected responseStruct = VerifySignatureResponseStruct; | ||
|
||
/** | ||
* Execute the verify signature request handler. | ||
* | ||
* @param params - The parameters of the request. | ||
* @param params.address - The address of the signer. | ||
* @param params.typedDataMessage - The Starknet type data message to sign. | ||
* @param params.signature - The signed signature in string. | ||
* @param params.chainId - The chain id of the network. | ||
* @returns the verification result in boolean. | ||
*/ | ||
async execute( | ||
params: VerifySignatureParams, | ||
): Promise<VerifySignatureResponse> { | ||
return super.execute(params); | ||
} | ||
|
||
protected async handleRequest( | ||
params: VerifySignatureParams, | ||
): Promise<VerifySignatureResponse> { | ||
const { typedDataMessage, address, signature } = params; | ||
|
||
return verifyTypedDataMessageSignature( | ||
address, | ||
this.account.privateKey, | ||
typedDataMessage, | ||
signature, | ||
); | ||
} | ||
} | ||
|
||
export const verifySignature = new VerifySignatureRpc({ | ||
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.