Skip to content

Commit

Permalink
fix: small fixes for WalletConnect alpha.32 (#7507)
Browse files Browse the repository at this point in the history
### Description

There has been a few breaking changes as part of the WalletConnect alpha tumult.

### Other changes

Added the scripts `run-in-memory-client` and `run-in-memory-wallet` as the most bare bones sanity test you can run. You run the client, copy the URL and then run the wallet. 

Also nice for testing automated flows, 

- I can run a wallet (Valora) and then startup a client that automatically does a bunch of operations
- I can run a client (https://use-contractkit.vercel.app) and then run a wallet that automatically approves operations

### Tested

```
cd packages/sdk/wallets/wallet-walletconnect
WALLET_CONNECT_BRIGDE=wss://relay.walletconnect.org yarn test
WALLET_CONNECT_BRIGDE=wss://walletconnect.celo-networks-dev.org yarn test
```
  • Loading branch information
Alex authored Mar 25, 2021
1 parent eec6357 commit fe6e010
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 68 deletions.
2 changes: 1 addition & 1 deletion packages/sdk/wallets/wallet-walletconnect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@celo/utils": "1.1.1-dev",
"@celo/wallet-base": "1.1.1-dev",
"@celo/wallet-remote": "1.1.1-dev",
"@walletconnect/client": "2.0.0-alpha.26",
"@walletconnect/client": "2.0.0-alpha.32",
"debug": "^4.1.1",
"ethereumjs-util": "^7.0.8"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { newKit } from '@celo/contractkit'
import { WalletConnectWallet } from '../src'

async function main() {
const wallet = new WalletConnectWallet({
connect: {
metadata: {
name: 'use-contractkit demo',
description: 'A showcase of use-contractkit',
url: 'https://use-contractkit.vercel.app',
icons: [],
},
},
init: {
relayProvider: 'wss://relay.walletconnect.org',
logger: 'error',
},
})
const uri = await wallet.getUri()
console.log(`=== START OUT OF BAND URI ===
${uri.toString()}
=== END OUT OF BAND URI ===`)
await wallet.init()

const [from] = await wallet.getAccounts()
const kit = newKit('https://alfajores-forno.celo-testnet.org', wallet)

/**
* Uncomment to send a test transaction
*/
// const gold = await kit.contracts.getGoldToken()
// await gold
// .transfer('0x4132F04EaCfdE9E2b707667A13CB69DbC5BABb68', '1')
// .sendAndWaitForReceipt({ from })
// console.log('Transaction sent!')

/**
* Uncomment to sign a test payload
*/
// await kit.connection.sign(ensureLeading0x(Buffer.from('hello').toString()), from)
// console.log('Payload signed!')

/**
* Uncomment to hold connection open
*/
// await new Promise((resolve) => setTimeout(resolve, 1000 * 60 * 60 * 24))

await wallet.close()
}

main()
.then(() => process.exit())
.catch((e) => {
console.error(e)
process.exit(1)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getTestWallet } from '../src/test/in-memory-wallet'
;(async function main() {
const uri = process.argv[2]
if (!uri) {
return
}

const wallet = getTestWallet()
await wallet.init(uri)
})()
20 changes: 10 additions & 10 deletions packages/sdk/wallets/wallet-walletconnect/src/test/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import { toChecksumAddress } from 'ethereumjs-util'

// personal_sign is the one RPC that has [payload, from] rather
// than [from, payload]
export function parsePersonalSign(req: any): { from: string; payload: string } {
const [payload, from] = req.payload.params
export function parsePersonalSign(params: any): { from: string; payload: string } {
const [payload, from] = params
return { from, payload }
}
export function parseSignTypedData(req: any): { from: string; payload: EIP712TypedData } {
const [from, payload] = req.payload.params
export function parseSignTypedData(params: any): { from: string; payload: EIP712TypedData } {
const [from, payload] = params
return { from, payload: JSON.parse(payload) }
}
export function parseSignTransaction(req: any): CeloTx {
return req.payload.params
export function parseSignTransaction(params: any): CeloTx {
return params
}
export function parseComputeSharedSecret(req: any): { from: Address; publicKey: string } {
const [from, publicKey] = req.payload.params
export function parseComputeSharedSecret(params: any): { from: Address; publicKey: string } {
const [from, publicKey] = params
return { from, publicKey }
}
export function parseDecrypt(req: any): { from: string; payload: Buffer } {
const [from, payload] = req.payload.params
export function parseDecrypt(params: any): { from: string; payload: Buffer } {
const [from, payload] = params
return { from, payload: Buffer.from(payload, 'hex') }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,34 @@ export function getTestWallet() {
debug('onPairingDeleted', pairing)
}

async function onSessionPayload(event: SessionTypes.PayloadEvent) {
async function onSessionRequest(event: SessionTypes.RequestParams) {
const {
topic,
// @ts-ignore todo: ask Pedro why this isn't typed
payload: { id, method },
request: {
// @ts-ignore
id,
method,
// @ts-ignore
params,
},
} = event

let result: any

if (method === SupportedMethods.personalSign) {
const { payload, from } = parsePersonalSign(event)
const { payload, from } = parsePersonalSign(params)
result = await wallet.signPersonalMessage(from, payload)
} else if (method === SupportedMethods.signTypedData) {
const { from, payload } = parseSignTypedData(event)
const { from, payload } = parseSignTypedData(params)
result = await wallet.signTypedData(from, payload)
} else if (method === SupportedMethods.signTransaction) {
const tx = parseSignTransaction(event)
const tx = parseSignTransaction(params)
result = await wallet.signTransaction(tx)
} else if (method === SupportedMethods.computeSharedSecret) {
const { from, publicKey } = parseComputeSharedSecret(event)
const { from, publicKey } = parseComputeSharedSecret(params)
result = (await wallet.computeSharedSecret(from, publicKey)).toString('hex')
} else if (method === SupportedMethods.decrypt) {
const { from, payload } = parseDecrypt(event)
const { from, payload } = parseDecrypt(params)
result = (await wallet.decrypt(from, payload)).toString('hex')
} else {
// client.reject({})
Expand All @@ -109,13 +114,15 @@ export function getTestWallet() {
return {
init: async (uri: string) => {
client = await WalletConnect.init({
relayProvider: process.env.WALLET_CONNECT_BRIGDE,
relayProvider: process.env.WALLET_CONNECT_BRIDGE,
controller: true,
})

client.on(CLIENT_EVENTS.session.proposal, onSessionProposal)
client.on(CLIENT_EVENTS.session.created, onSessionCreated)
client.on(CLIENT_EVENTS.session.updated, onSessionUpdated)
client.on(CLIENT_EVENTS.session.deleted, onSessionDeleted)
client.on(CLIENT_EVENTS.session.payload, onSessionPayload)
client.on(CLIENT_EVENTS.session.request, onSessionRequest)

client.on(CLIENT_EVENTS.pairing.proposal, onPairingProposal)
client.on(CLIENT_EVENTS.pairing.created, onPairingCreated)
Expand Down
18 changes: 7 additions & 11 deletions packages/sdk/wallets/wallet-walletconnect/src/test/mock-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,29 @@ export class MockWalletConnectClient extends EventEmitter {
})
}

async request(event: SessionTypes.PayloadEvent) {
async request(event: SessionTypes.RequestEvent) {
const {
// @ts-ignore
request: { method },
request: { method, params },
} = event

// the request gets transformed between the client
// and wallet, here we reassign to use our decoding
// methods in ./common.ts.
// @ts-ignore
const request = { payload: { params: event.request.params } }

let result = null
if (method === SupportedMethods.personalSign) {
const { payload, from } = parsePersonalSign(request)
const { payload, from } = parsePersonalSign(params)
result = await testWallet.signPersonalMessage(from, payload)
} else if (method === SupportedMethods.signTypedData) {
const { from, payload } = parseSignTypedData(request)
const { from, payload } = parseSignTypedData(params)
result = await testWallet.signTypedData(from, payload)
} else if (method === SupportedMethods.signTransaction) {
const tx = parseSignTransaction(request)
const tx = parseSignTransaction(params)
result = await testWallet.signTransaction(tx)
} else if (method === SupportedMethods.computeSharedSecret) {
const { from, publicKey } = parseComputeSharedSecret(request)
const { from, publicKey } = parseComputeSharedSecret(params)
result = (await testWallet.computeSharedSecret(from, publicKey)).toString('hex')
} else if (method === SupportedMethods.decrypt) {
const { from, payload } = parseDecrypt(request)
const { from, payload } = parseDecrypt(params)
result = (await testWallet.decrypt(from, payload)).toString('hex')
} else {
return
Expand Down
1 change: 0 additions & 1 deletion packages/sdk/wallets/wallet-walletconnect/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export enum SupportedMethods {
accounts = 'eth_accounts',
sendTransaction = 'eth_sendTransaction',
signTransaction = 'eth_signTransaction',
personalSign = 'personal_sign',
signTypedData = 'eth_signTypedData',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const testTx = {
}
const decryptMessage = 'Hello'

const walletConnectBridge = process.env.WALLET_CONNECT_BRIGDE
const walletConnectBridge = process.env.WALLET_CONNECT_BRIDGE
const E2E = !!walletConnectBridge

describe('WalletConnectWallet tests', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/wallets/wallet-walletconnect/src/wc-signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class WalletConnectSigner implements Signer {
private request(method: SupportedMethods, params: any) {
return this.client.request({
topic: this.session.topic,
chainId: this.chainId,
chainId: `celo:${this.chainId}`,
request: {
method,
params,
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/wallets/wallet-walletconnect/src/wc-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async function waitForTruthy(getValue: () => any, attempts: number = 10) {
throw new Error('Unable to get pairing session, did you lose internet connection?')
}

const defaultInitOptions: ClientOptions = { relayProvider: 'wss://bridge.walletconnect.org' }
const defaultInitOptions: ClientOptions = { relayProvider: 'wss://relay.walletconnect.org' }
const defaultConnectOptions: ClientTypes.ConnectParams = {
metadata: {
name: 'ContractKit',
Expand Down Expand Up @@ -132,7 +132,7 @@ export class WalletConnectWallet extends RemoteWallet<WalletConnectSigner> {
debug('Attempted to update non existant pairing', pairing)
return
}
this.pairing.peer.metadata = pairing.peer.metadata
this.pairing.state.metadata = pairing.state.metadata
}
onPairingDeleted = () => {
this.pairing = undefined
Expand Down
80 changes: 49 additions & 31 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2825,7 +2825,7 @@
"@types/yargs" "^15.0.0"
chalk "^4.0.0"

"@json-rpc-tools/provider@^1.4.0", "@json-rpc-tools/provider@^1.6.3":
"@json-rpc-tools/provider@^1.4.0":
version "1.6.3"
resolved "https://registry.yarnpkg.com/@json-rpc-tools/provider/-/provider-1.6.3.tgz#c2ed6906e1b5c86dc17defcd2b0f5ba774c58d46"
integrity sha512-fVOmqJYAeCFVxSUUmmoxtLu/VtA1sbVsIrFsW84oSQv73C5A0+vby1fVIkxT2RXbjoAsufXTGin3twRDWusK/Q==
Expand All @@ -2835,20 +2835,44 @@
safe-json-utils "^1.1.1"
ws "^7.4.0"

"@json-rpc-tools/provider@^1.6.4":
version "1.6.4"
resolved "https://registry.yarnpkg.com/@json-rpc-tools/provider/-/provider-1.6.4.tgz#647f8d3d3e3b45db2b474671f20b63a8f3daf864"
integrity sha512-IXCPd26TonTKNPtc5dp/GyHJ5imCh5wcg+XICyAnMHdCinufjGbqBUIibGvPB/P2UmnfDUn6VlLKJShSa9x8Og==
dependencies:
"@json-rpc-tools/utils" "^1.6.4"
axios "^0.21.0"
safe-json-utils "^1.1.1"
ws "^7.4.0"

"@json-rpc-tools/types@^1.6.3":
version "1.6.3"
resolved "https://registry.yarnpkg.com/@json-rpc-tools/types/-/types-1.6.3.tgz#1c8231abf65f27a507cf4468bee86997d9b5a8e4"
integrity sha512-J9yIEcp6dkKrcLw/ojuui3AFO4J8MHyNAQ3Zi7W0oSRFtK/yBUAOJiuVG5AE6nOKVNGijZmbOdml7HZijeoIXA==
dependencies:
keyvaluestorage-interface "^1.0.0"

"@json-rpc-tools/types@^1.6.4":
version "1.6.4"
resolved "https://registry.yarnpkg.com/@json-rpc-tools/types/-/types-1.6.4.tgz#8cfda48bcc19d4f643ff21ff56bdc1dce730a1f6"
integrity sha512-DHtnvlIFN8YUun38Sy9SaRdV/BsUMFM5bAABDsb/iPGLfPHOMKoAyuPOwEqQ2vgtc9ayTcQ2546OPTQ92IzJ/g==
dependencies:
keyvaluestorage-interface "^1.0.0"

"@json-rpc-tools/utils@^1.4.0", "@json-rpc-tools/utils@^1.6.3":
version "1.6.3"
resolved "https://registry.yarnpkg.com/@json-rpc-tools/utils/-/utils-1.6.3.tgz#4a0568c9a20a97aee445affe7747778554d08ec1"
integrity sha512-k06R+D6cbud8APwVF3x4KiACUaZDPah1CQOvYdEpojNTIw2MlwVo8EELpldD9c/9j0B+PrCMQpIh14CRF6NtAA==
dependencies:
"@json-rpc-tools/types" "^1.6.3"

"@json-rpc-tools/utils@^1.6.4":
version "1.6.4"
resolved "https://registry.yarnpkg.com/@json-rpc-tools/utils/-/utils-1.6.4.tgz#92d1d12944326150c0ab26d248c710ea3bb2c36e"
integrity sha512-4XA8lXUyOjFs9hcJQJwbpc+Yt+AenCttsAq6HFRPd0nXwNbVsY6h+tVWVRyQj4oi/m3NkOfROOff0MN7VX1mfg==
dependencies:
"@json-rpc-tools/types" "^1.6.4"

"@ledgerhq/devices@^4.64.0":
version "4.64.0"
resolved "https://registry.yarnpkg.com/@ledgerhq/devices/-/devices-4.64.0.tgz#4412f81338f5495f179017e5101273c3f0edaddf"
Expand Down Expand Up @@ -5370,41 +5394,40 @@
version "1.0.0"
resolved "https://github.com/umpirsky/country-list#05fda51cd97b3294e8175ffed06104c44b3c71d7"

"@walletconnect/client@2.0.0-alpha.26":
version "2.0.0-alpha.26"
resolved "https://registry.yarnpkg.com/@walletconnect/client/-/client-2.0.0-alpha.26.tgz#072116519656f7d3dc035ada589b9ecb5c695b72"
integrity sha512-IQxmaNjRbcJ12SXZpBs5IRv1Q7MMcP/Kl5ST4sdLKmZEiTOaEYMPPhqhSZ/sxOs8uAQhEjZ/25NWUrjZZnTSOg==
"@walletconnect/client@2.0.0-alpha.32":
version "2.0.0-alpha.32"
resolved "https://registry.yarnpkg.com/@walletconnect/client/-/client-2.0.0-alpha.32.tgz#9645dd9b6f204468e25ebd85f50c308093cc52e3"
integrity sha512-lf3o/rM/B73a6mJ5z3RQOvJN5MckpClts1F4UlpS3xU0C6iKlzVFI3bp9wWdzT+fMUv9Ls3EtEga7Y8PmFaecQ==
dependencies:
"@json-rpc-tools/provider" "^1.6.3"
"@json-rpc-tools/utils" "^1.6.3"
"@json-rpc-tools/provider" "^1.6.4"
"@json-rpc-tools/utils" "^1.6.4"
"@pedrouid/pino-utils" "^1.0.1"
"@walletconnect/types" "^2.0.0-alpha.26"
"@walletconnect/utils" "^2.0.0-alpha.26"
"@walletconnect/types" "^2.0.0-alpha.32"
"@walletconnect/utils" "^2.0.0-alpha.32"
enc-utils "^3.0.0"
keyvaluestorage "^0.7.1"
pino "^6.7.0"
pino-pretty "^4.3.0"
relay-provider "^1.2.0"
relay-provider "^1.2.1"
safe-json-utils "^1.1.1"

"@walletconnect/types@^2.0.0-alpha.26":
version "2.0.0-alpha.26"
resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-2.0.0-alpha.26.tgz#83eee3e94bebfeabba95d1b910a93302d03a8a68"
integrity sha512-yoPVloExCgqbOUD3DbMhNnTfQPNY+cdDVrn6U3fCMRQ1PSNZyfuLNh769kQNodHfTEkfGyQyX9oJ9t3p2xJcUA==
"@walletconnect/types@^2.0.0-alpha.32":
version "2.0.0-alpha.32"
resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-2.0.0-alpha.32.tgz#05f6f641f369abc2db099f5560f958be5e047901"
integrity sha512-m8lsJIJEVbpGYbnjC6qkseB0HsN2go4dJIfaDw2jjTtKIGEvBlkKFEPWcUbmUdAjUWhdyFgHCotq4hmBCcDIbA==
dependencies:
"@json-rpc-tools/types" "^1.6.3"
"@json-rpc-tools/types" "^1.6.4"
keyvaluestorage "^0.7.1"
pino "^6.7.0"
pino-pretty "^4.3.0"

"@walletconnect/utils@^2.0.0-alpha.26":
version "2.0.0-alpha.26"
resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.0.0-alpha.26.tgz#88429c5e9c0557a36604e5311a138e28b54ecb16"
integrity sha512-K5OjZiFspHmGt8JXf07ZGaxwrJzI+AJ5//bt7KGVA6bmFAAs9syGmbaT/hX7eMK8JVCb+Fbjn0JalSKRQyEtlQ==
"@walletconnect/utils@^2.0.0-alpha.32":
version "2.0.0-alpha.32"
resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.0.0-alpha.32.tgz#feceffd467f24530208938c5f8b0b24004fbe1ad"
integrity sha512-6P/qsd4X3kXn+Yrq8Pk05xBHXv4zSI3tpK3KaXjJizzYUT+k/VgwLCXikyuOoVhcBOPBREAMurVzCsmtbP9DRw==
dependencies:
"@json-rpc-tools/utils" "^1.6.3"
"@walletconnect/types" "^2.0.0-alpha.26"
detect-browser "^5.1.0"
"@json-rpc-tools/utils" "^1.6.4"
"@walletconnect/types" "^2.0.0-alpha.32"
ecies-25519 "^1.3.0"
enc-utils "^3.0.0"
query-string "^6.13.5"
Expand Down Expand Up @@ -9959,11 +9982,6 @@ destroy@^1.0.4, destroy@~1.0.4:
resolved "https://registry.yarnpkg.com/destroy/-/destroy-1.0.4.tgz#978857442c44749e4206613e37946205826abd80"
integrity sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=

detect-browser@^5.1.0:
version "5.2.0"
resolved "https://registry.yarnpkg.com/detect-browser/-/detect-browser-5.2.0.tgz#c9cd5afa96a6a19fda0bbe9e9be48a6b6e1e9c97"
integrity sha512-tr7XntDAu50BVENgQfajMLzacmSe34D+qZc4zjnniz0ZVuw/TZcLcyxHQjYpJTM36sGEkZZlYLnIM1hH7alTMA==

detect-file@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/detect-file/-/detect-file-0.1.0.tgz#4935dedfd9488648e006b0129566e9386711ea63"
Expand Down Expand Up @@ -21977,10 +21995,10 @@ relative@^3.0.1:
dependencies:
isobject "^2.0.0"

relay-provider@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/relay-provider/-/relay-provider-1.2.0.tgz#9de740ab48f1deba29fa26f73559adb14b95da70"
integrity sha512-BCtRgrPO6Am5hbMXEcdltq1+DTtvNjvG7HjGVT4Fd1qDU96bctgXf2PAkGq1Yn1OGlcgOa6jaxAQzB9J043CtQ==
relay-provider@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/relay-provider/-/relay-provider-1.2.1.tgz#b4191899dbb871b06a74e7d8995d7749e1f7dc09"
integrity sha512-N3p05r57sHk76qfiNiSflDY83LigmbfaAnFEno9smCz30CgA61IJLfoqLr1+qQSdIEqvy1Jflcs2lLmpzBOYKA==
dependencies:
"@json-rpc-tools/provider" "^1.4.0"
"@json-rpc-tools/utils" "^1.4.0"
Expand Down

0 comments on commit fe6e010

Please sign in to comment.