-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathchannel.test.ts
More file actions
178 lines (153 loc) · 5.76 KB
/
channel.test.ts
File metadata and controls
178 lines (153 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import {
SequenceClient,
ProxyMessageProvider,
WalletRequestHandler,
ProxyMessageChannel,
ProxyMessageHandler,
prefixEIP191Message,
MemoryItemStore
} from '@0xsequence/provider'
import { ethers } from 'ethers'
import { test, assert } from '../../utils/assert'
import { LocalRelayer } from '@0xsequence/relayer'
import { configureLogger, encodeMessageDigest } from '@0xsequence/utils'
import { testAccounts, getEOAWallet } from '../testutils'
import { Account } from '@0xsequence/account'
import * as utils from '@0xsequence/tests'
import { Orchestrator } from '@0xsequence/signhub'
import { trackers } from '@0xsequence/sessions'
import { commons } from '@0xsequence/core'
import { ChainId } from '@0xsequence/network'
configureLogger({ logLevel: 'DEBUG', silence: false })
export const tests = async () => {
// ProxyMessageChannel object is to be instantiated by the app coordinating
// the channel, ie. such as the mobile application itself.
//
// `ch.app` (port) will be injected into the app, and `ch.wallet` (port) will be injected into the wallet.
//
// Sending messages to the app port will go through channel and get received by the wallet.
// Sending messages to the wallet port will go through channel and get received by the app.
const ch = new ProxyMessageChannel()
ch.app.on('open', openInfo => {
console.log('app, wallet opened.', openInfo)
})
ch.app.on('close', () => {
console.log('app, wallet closed.')
})
ch.app.on('connect', () => {
console.log('app, wallet connected.')
})
ch.app.on('disconnect', () => {
console.log('app, wallet disconnected.')
})
// ch.wallet.on('open', () => {
// console.log('wallet, wallet opened.')
// })
// ch.wallet.on('close', () => {
// console.log('wallet, wallet closed.')
// })
// ch.wallet.on('connect', () => {
// console.log('wallet, wallet connected.')
// })
// ch.wallet.on('disconnect', () => {
// console.log('wallet, wallet disconnected.')
// })
//
// Wallet Handler
//
// owner account address: 0x4e37E14f5d5AAC4DF1151C6E8DF78B7541680853
const owner = getEOAWallet(testAccounts[0].privateKey)
// relayer account is same as owner here
const relayer = new LocalRelayer(owner)
const rpcProvider = new ethers.providers.JsonRpcProvider('http://localhost:8545')
const contexts = await utils.context.deploySequenceContexts(rpcProvider.getSigner())
const networks = [
{
name: 'hardhat',
chainId: 31337 as ChainId,
rpcUrl: rpcProvider.connection.url,
provider: rpcProvider,
relayer: relayer,
isDefaultChain: true,
nativeToken: {
symbol: 'ETH',
name: 'Ether',
decimals: 18
}
}
]
// wallet account address: 0x91A858FbBa42E7EE200b4303b1A8B2F0BD139663 based on the chainId
const account = await Account.new({
config: {
threshold: 1,
checkpoint: 1674142220,
signers: [
{
address: owner.address,
weight: 1
}
]
},
networks,
contexts,
orchestrator: new Orchestrator([owner]),
tracker: new trackers.local.LocalConfigTracker(rpcProvider)
})
// the rpc signer via the wallet
const walletRequestHandler = new WalletRequestHandler(undefined, null, networks)
// register wallet message handler, in this case using the ProxyMessage transport.
const proxyHandler = new ProxyMessageHandler(walletRequestHandler, ch.wallet)
proxyHandler.register()
//
// App Provider
//
const walletProvider = new ProxyMessageProvider(ch.app)
walletProvider.register()
// setup web3 provider
const client = new SequenceClient(walletProvider, new MemoryItemStore(), { defaultChainId: 31337 })
const connectPromise = client.connect({ app: 'proxy-transport-channel test', keepWalletOpened: true })
// fake/force an async wallet initialization for the wallet-request handler. This is the behaviour
// of the wallet-webapp, so lets ensure the mock wallet does the same thing too.
walletRequestHandler.signIn(account, { connect: true })
await connectPromise
const address = client.getAddress()
await test('verifying getAddress result', async () => {
assert.equal(address, ethers.utils.getAddress('0x91A858FbBa42E7EE200b4303b1A8B2F0BD139663'), 'wallet address')
})
await test('sending a json-rpc request', async () => {
await walletProvider.sendAsync({ jsonrpc: '2.0', id: 88, method: 'eth_accounts', params: [] }, (err, resp) => {
assert.true(!err, 'error is empty')
assert.true(!!resp, 'response successful')
assert.true(resp!.result == address, 'response address check')
})
})
await test('get chain id', async () => {
const chainIdClient = client.getChainId()
assert.equal(chainIdClient, 31337, 'chain id match')
const netVersion = await client.send({ method: 'net_version' })
assert.equal(netVersion, '31337', 'net_version check')
const chainId = await client.send({ method: 'eth_chainId' })
assert.equal(chainId, '0x7a69', 'eth_chainId check')
})
await test('sign a message and validate/recover', async () => {
const message = ethers.utils.toUtf8Bytes('hihi')
//
// Sign the message
//
const sig = await client.signMessage(message)
assert.equal(
sig,
'0x000163c9620c0001045ea593a25d0053816f2cfb0239eb04c30cc08fd26193927bf6cf68f7f31a8239ecbcbd1365f18a6bf2bf3b13d544c91d85e35503696a28fcb96a4078a7556a1c02',
'signature match'
)
const reader = new commons.reader.OnChainReader(rpcProvider)
//
// Verify the message signature
//
await account.doBootstrap(31337)
const messageDigest = encodeMessageDigest(prefixEIP191Message(message))
const isValid = await reader.isValidSignature(address, messageDigest, sig)
assert.true(isValid, 'signature is valid - 1')
})
walletProvider.closeWallet()
}