Skip to content

Commit 1e4dac3

Browse files
authored
test(sdk): Use ethers v6 utils (#2886)
Moved two Ethereum function selector utils from `FakeJsonRpcServer` to test utils. These are soon needed in #2883. Also refactored methods (and one `FakeJsonRpcServer` usage) to use the `id` utils from ethers (https://github.com/ethers-io/ethers.js/blob/9e7e7f3e2f2d51019aaa782e6290e079c38332fb/src.ts/hash/id.ts#L16). It does the same hash calculation as our implementation. Note that the return value of the functions now includes the `0x` prefix. It is therefore compatible e.g. with `ethers` `FunctionFragment.getSelector()` method (https://github.com/ethers-io/ethers.js/blob/9e7e7f3e2f2d51019aaa782e6290e079c38332fb/src.ts/abi/fragments.ts#L1449).
1 parent 2a40642 commit 1e4dac3

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

packages/sdk/test/test-utils/FakeJsonRpcServer.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { AbiCoder, keccak256, toUtf8Bytes } from 'ethers'
1+
import { AbiCoder, id } from 'ethers'
22
import { once } from 'events'
33
import express, { Request, Response } from 'express'
44
import { Server } from 'http'
55
import { intersection, isArray } from 'lodash'
66
import { AddressInfo } from 'net'
77
import { promisify } from 'util'
8+
import { formEthereumFunctionSelector, parseEthereumFunctionSelectorFromCallData } from './utils'
89

910
export const CHAIN_ID = 5555
1011
const BLOCK_NUMBER = 123
@@ -14,12 +15,6 @@ const toHex = (val: number) => {
1415
return '0x' + val.toString(16)
1516
}
1617

17-
const getLabelHash = (methodSignature: string) => keccak256(toUtf8Bytes(methodSignature))
18-
19-
const getContractMethodHash = (methodSignature: string) => getLabelHash(methodSignature).substring(2, 10)
20-
21-
const getEventTopicHash = (eventSignature: string) => getLabelHash(eventSignature)
22-
2318
export interface JsonRpcRequest {
2419
id: string
2520
method: string
@@ -88,17 +83,17 @@ export class FakeJsonRpcServer {
8883
return toHex(BLOCK_NUMBER)
8984
} else if (request.method === 'eth_call') {
9085
const data: string = request.params[0].data
91-
const contractMethodHash = data.substring(2, 10)
92-
if (contractMethodHash === getContractMethodHash('getPermissionsForUserId(string,bytes)')) {
86+
const functionSelector = parseEthereumFunctionSelectorFromCallData(data)
87+
if (functionSelector === formEthereumFunctionSelector('getPermissionsForUserId(string,bytes)')) {
9388
// PermissionStructOutput: { canEdit: false, canDelete: false, publishExpiration: 0n, subscribeExpiration: 0n, canGrant: false }
9489
return '0x' + '0'.repeat(320)
9590
} else {
96-
throw new Error(`Unknown contract method: ${contractMethodHash}, request: ${JSON.stringify(request)}`)
91+
throw new Error(`Unknown contract method: ${functionSelector}, request: ${JSON.stringify(request)}`)
9792
}
9893
} else if (request.method === 'eth_getLogs') {
9994
const topics = request.params[0].topics
100-
const topicHash = getEventTopicHash('StreamCreated(string,string)')
101-
if ((topics.length !== 1) || (topics[0] !== topicHash)) {
95+
const topicId = id('StreamCreated(string,string)')
96+
if ((topics.length !== 1) || (topics[0] !== topicId)) {
10297
throw new Error('Not implemented')
10398
}
10499
if (request.params[0].toBlock !== 'latest') {
@@ -109,7 +104,7 @@ export class FakeJsonRpcServer {
109104
const data = new AbiCoder().encode(['string', 'string'], [EVENT_STREAM_ID, JSON.stringify({ partitions: 1 })])
110105
return [{
111106
address: request.params[0].address,
112-
topics: [topicHash],
107+
topics: [topicId],
113108
data,
114109
blockNumber: toHex(BLOCK_NUMBER),
115110
transactionHash: '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef',

packages/sdk/test/test-utils/utils.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import {
55
DEFAULT_PARTITION_COUNT,
66
Logger,
77
MAX_PARTITION_COUNT,
8+
merge,
89
StreamPartID,
910
StreamPartIDUtils,
11+
until,
1012
UserID,
11-
merge,
1213
utf8ToBinary,
13-
wait,
14-
until
14+
wait
1515
} from '@streamr/utils'
1616
import crypto from 'crypto'
17-
import { Wallet } from 'ethers'
17+
import { id, Wallet } from 'ethers'
1818
import { once } from 'events'
1919
import express, { Request, Response } from 'express'
2020
import { mock } from 'jest-mock-extended'
@@ -318,3 +318,13 @@ export const readUtf8ExampleIndirectly = async (): Promise<string> => {
318318
})
319319
})
320320
}
321+
322+
const ETHEREUM_FUNCTION_SELECTOR_LENGTH = 10 // 0x + 4 bytes
323+
324+
export const formEthereumFunctionSelector = (methodSignature: string): string => {
325+
return id(methodSignature).substring(0, ETHEREUM_FUNCTION_SELECTOR_LENGTH)
326+
}
327+
328+
export const parseEthereumFunctionSelectorFromCallData = (data: string): string => {
329+
return data.substring(0, ETHEREUM_FUNCTION_SELECTOR_LENGTH)
330+
}

0 commit comments

Comments
 (0)