-
-
Notifications
You must be signed in to change notification settings - Fork 95
feat: support EIP-5792 methods #359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b0aaf31
Add wallet_sendCalls
matthewwalsh0 f3be963
Add wallet_getCallsStatus
matthewwalsh0 fec5a17
Add validation unit tests
matthewwalsh0 f8010ec
Add get calls status unit tests
matthewwalsh0 7e705dc
Add wallet_getCapabilities
matthewwalsh0 9e77943
Export types
matthewwalsh0 7f607ad
Fix unit tests
matthewwalsh0 b8f410d
Allow capabilities
matthewwalsh0 64c48e2
Require version
matthewwalsh0 0077d6e
Update capabilities result type
matthewwalsh0 33d241b
Merge branch 'main' into feat/eip-5792
matthewwalsh0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,145 @@ | ||
import type { JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils'; | ||
import { klona } from 'klona'; | ||
|
||
import type { | ||
GetCallsStatusParams, | ||
GetCallsStatusResult, | ||
GetTransactionReceiptsByBatchIdHook, | ||
} from './wallet-get-calls-status'; | ||
import { walletGetCallsStatus } from './wallet-get-calls-status'; | ||
|
||
const ID_MOCK = '1234-5678'; | ||
|
||
const RECEIPT_MOCK = { | ||
logs: [ | ||
{ | ||
address: '0x123abc123abc123abc123abc123abc123abc123a', | ||
data: '0x123abc', | ||
topics: ['0x123abc'], | ||
}, | ||
], | ||
status: '0x1', | ||
chainId: '0x1', | ||
blockHash: '0x123abc', | ||
blockNumber: '0x1', | ||
gasUsed: '0x1', | ||
transactionHash: '0x123abc', | ||
}; | ||
|
||
const REQUEST_MOCK = { | ||
params: [ID_MOCK], | ||
} as unknown as JsonRpcRequest<GetCallsStatusParams>; | ||
|
||
describe('wallet_getCallsStatus', () => { | ||
let request: JsonRpcRequest<GetCallsStatusParams>; | ||
let params: GetCallsStatusParams; | ||
let response: PendingJsonRpcResponse<GetCallsStatusResult>; | ||
let getTransactionReceiptsByBatchIdMock: jest.MockedFunction<GetTransactionReceiptsByBatchIdHook>; | ||
|
||
async function callMethod() { | ||
return walletGetCallsStatus(request, response, { | ||
getTransactionReceiptsByBatchId: getTransactionReceiptsByBatchIdMock, | ||
}); | ||
} | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
|
||
request = klona(REQUEST_MOCK); | ||
params = request.params as GetCallsStatusParams; | ||
response = {} as PendingJsonRpcResponse<GetCallsStatusResult>; | ||
|
||
getTransactionReceiptsByBatchIdMock = jest | ||
.fn() | ||
.mockResolvedValue([RECEIPT_MOCK, RECEIPT_MOCK]); | ||
}); | ||
|
||
it('calls hook', async () => { | ||
await callMethod(); | ||
expect(getTransactionReceiptsByBatchIdMock).toHaveBeenCalledWith( | ||
params[0], | ||
request, | ||
); | ||
}); | ||
|
||
it('returns confirmed status if all receipts available', async () => { | ||
await callMethod(); | ||
expect(response.result?.status).toBe('CONFIRMED'); | ||
}); | ||
|
||
it('returns pending status if missing receipts', async () => { | ||
getTransactionReceiptsByBatchIdMock = jest | ||
.fn() | ||
.mockResolvedValue([RECEIPT_MOCK, undefined]); | ||
|
||
await callMethod(); | ||
expect(response.result?.status).toBe('PENDING'); | ||
expect(response.result?.receipts).toBeNull(); | ||
}); | ||
|
||
it('returns receipts', async () => { | ||
await callMethod(); | ||
|
||
expect(response.result?.receipts).toStrictEqual([ | ||
RECEIPT_MOCK, | ||
RECEIPT_MOCK, | ||
]); | ||
}); | ||
|
||
it('returns null if no receipts', async () => { | ||
getTransactionReceiptsByBatchIdMock = jest.fn().mockResolvedValue([]); | ||
|
||
await callMethod(); | ||
expect(response.result).toBeNull(); | ||
}); | ||
|
||
jiexi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
it('throws if no hook', async () => { | ||
await expect( | ||
walletGetCallsStatus(request, response, {}), | ||
).rejects.toMatchInlineSnapshot(`[Error: Method not supported.]`); | ||
}); | ||
|
||
it('throws if no params', async () => { | ||
request.params = undefined; | ||
|
||
await expect(callMethod()).rejects.toMatchInlineSnapshot(` | ||
[Error: Invalid params | ||
|
||
Expected an array, but received: undefined] | ||
`); | ||
}); | ||
|
||
it('throws if wrong type', async () => { | ||
params[0] = 123 as never; | ||
|
||
await expect(callMethod()).rejects.toMatchInlineSnapshot(` | ||
[Error: Invalid params | ||
|
||
0 - Expected a string, but received: 123] | ||
`); | ||
}); | ||
|
||
it('throws if empty', async () => { | ||
params[0] = ''; | ||
|
||
await expect(callMethod()).rejects.toMatchInlineSnapshot(` | ||
[Error: Invalid params | ||
|
||
0 - Expected a nonempty string but received an empty one] | ||
`); | ||
}); | ||
|
||
it('removes excess properties from receipts', async () => { | ||
getTransactionReceiptsByBatchIdMock.mockResolvedValue([ | ||
{ | ||
...RECEIPT_MOCK, | ||
extra: 'value1', | ||
logs: [{ ...RECEIPT_MOCK.logs[0], extra2: 'value2' }], | ||
} as never, | ||
]); | ||
|
||
await callMethod(); | ||
|
||
expect(response.result?.receipts).toStrictEqual([RECEIPT_MOCK]); | ||
}); | ||
}); |
This file contains hidden or 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,85 @@ | ||
import { rpcErrors } from '@metamask/rpc-errors'; | ||
import type { Infer } from '@metamask/superstruct'; | ||
import { | ||
nonempty, | ||
optional, | ||
mask, | ||
string, | ||
array, | ||
object, | ||
tuple, | ||
} from '@metamask/superstruct'; | ||
import type { | ||
Json, | ||
JsonRpcRequest, | ||
PendingJsonRpcResponse, | ||
} from '@metamask/utils'; | ||
import { HexChecksumAddressStruct, StrictHexStruct } from '@metamask/utils'; | ||
|
||
import { validateParams } from '../utils/validation'; | ||
|
||
const GetCallsStatusStruct = tuple([nonempty(string())]); | ||
|
||
const GetCallsStatusReceiptStruct = object({ | ||
logs: optional( | ||
array( | ||
object({ | ||
address: optional(HexChecksumAddressStruct), | ||
data: optional(StrictHexStruct), | ||
topics: optional(array(StrictHexStruct)), | ||
}), | ||
), | ||
), | ||
status: optional(StrictHexStruct), | ||
chainId: optional(StrictHexStruct), | ||
jiexi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
blockHash: optional(StrictHexStruct), | ||
blockNumber: optional(StrictHexStruct), | ||
gasUsed: optional(StrictHexStruct), | ||
transactionHash: optional(StrictHexStruct), | ||
}); | ||
|
||
export type GetCallsStatusParams = Infer<typeof GetCallsStatusStruct>; | ||
export type GetCallsStatusReceipt = Infer<typeof GetCallsStatusReceiptStruct>; | ||
|
||
export type GetCallsStatusResult = { | ||
status: 'PENDING' | 'CONFIRMED'; | ||
receipts?: GetCallsStatusReceipt[]; | ||
}; | ||
|
||
export type GetTransactionReceiptsByBatchIdHook = ( | ||
batchId: string, | ||
req: JsonRpcRequest, | ||
jiexi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) => Promise<GetCallsStatusReceipt[]>; | ||
|
||
export async function walletGetCallsStatus( | ||
req: JsonRpcRequest, | ||
res: PendingJsonRpcResponse<Json>, | ||
{ | ||
getTransactionReceiptsByBatchId, | ||
}: { | ||
getTransactionReceiptsByBatchId?: GetTransactionReceiptsByBatchIdHook; | ||
}, | ||
): Promise<void> { | ||
if (!getTransactionReceiptsByBatchId) { | ||
throw rpcErrors.methodNotSupported(); | ||
} | ||
|
||
validateParams(req.params, GetCallsStatusStruct); | ||
|
||
const batchId = req.params[0]; | ||
const rawReceipts = await getTransactionReceiptsByBatchId(batchId, req); | ||
|
||
if (!rawReceipts.length) { | ||
res.result = null; | ||
return; | ||
} | ||
|
||
const isComplete = rawReceipts.every((receipt) => Boolean(receipt)); | ||
const status = isComplete ? 'CONFIRMED' : 'PENDING'; | ||
|
||
const receipts = isComplete | ||
? rawReceipts.map((receipt) => mask(receipt, GetCallsStatusReceiptStruct)) | ||
: null; | ||
|
||
res.result = { status, receipts }; | ||
} |
This file contains hidden or 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,101 @@ | ||
import type { JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils'; | ||
import { klona } from 'klona'; | ||
|
||
import type { | ||
GetCapabilitiesHook, | ||
GetCapabilitiesParams, | ||
GetCapabilitiesResult, | ||
} from './wallet-get-capabilities'; | ||
import { walletGetCapabilities } from './wallet-get-capabilities'; | ||
|
||
const ADDRESS_MOCK = '0x123abc123abc123abc123abc123abc123abc123a'; | ||
|
||
const RESULT_MOCK = { | ||
testCapability: { | ||
testKey: 'testValue', | ||
}, | ||
}; | ||
|
||
const REQUEST_MOCK = { | ||
params: [ADDRESS_MOCK], | ||
} as unknown as JsonRpcRequest<GetCapabilitiesParams>; | ||
|
||
describe('wallet_getCapabilities', () => { | ||
let request: JsonRpcRequest<GetCapabilitiesParams>; | ||
let params: GetCapabilitiesParams; | ||
let response: PendingJsonRpcResponse<GetCapabilitiesResult>; | ||
let getCapabilitiesMock: jest.MockedFunction<GetCapabilitiesHook>; | ||
|
||
async function callMethod() { | ||
return walletGetCapabilities(request, response, { | ||
getCapabilities: getCapabilitiesMock, | ||
}); | ||
} | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
|
||
request = klona(REQUEST_MOCK); | ||
params = request.params as GetCapabilitiesParams; | ||
response = {} as PendingJsonRpcResponse<GetCapabilitiesResult>; | ||
|
||
getCapabilitiesMock = jest.fn().mockResolvedValue(RESULT_MOCK); | ||
}); | ||
|
||
it('calls hook', async () => { | ||
await callMethod(); | ||
expect(getCapabilitiesMock).toHaveBeenCalledWith(params[0], request); | ||
}); | ||
|
||
it('returns capabilities from hook', async () => { | ||
await callMethod(); | ||
|
||
expect(response.result).toStrictEqual(RESULT_MOCK); | ||
}); | ||
|
||
it('throws if no hook', async () => { | ||
await expect( | ||
walletGetCapabilities(request, response, {}), | ||
).rejects.toMatchInlineSnapshot(`[Error: Method not supported.]`); | ||
}); | ||
|
||
it('throws if no params', async () => { | ||
request.params = undefined; | ||
|
||
await expect(callMethod()).rejects.toMatchInlineSnapshot(` | ||
[Error: Invalid params | ||
|
||
Expected an array, but received: undefined] | ||
`); | ||
}); | ||
|
||
it('throws if wrong type', async () => { | ||
params[0] = 123 as never; | ||
|
||
await expect(callMethod()).rejects.toMatchInlineSnapshot(` | ||
[Error: Invalid params | ||
|
||
0 - Expected a string, but received: 123] | ||
`); | ||
}); | ||
|
||
it('throws if not hex', async () => { | ||
params[0] = 'test' as never; | ||
|
||
await expect(callMethod()).rejects.toMatchInlineSnapshot(` | ||
[Error: Invalid params | ||
|
||
0 - Expected a string matching \`/^0x[0-9a-fA-F]{40}$/\` but received "test"] | ||
`); | ||
}); | ||
|
||
it('throws if wrong length', async () => { | ||
params[0] = '0x123' as never; | ||
|
||
await expect(callMethod()).rejects.toMatchInlineSnapshot(` | ||
[Error: Invalid params | ||
|
||
0 - Expected a string matching \`/^0x[0-9a-fA-F]{40}$/\` but received "0x123"] | ||
`); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.