From 2decefab3445e8daff68947dae1bd733a5297609 Mon Sep 17 00:00:00 2001 From: Ry Racherbaumer Date: Tue, 26 Sep 2023 11:07:33 -0500 Subject: [PATCH 1/2] fix: support byte array messages with viem signer --- src/utils/viem.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/utils/viem.ts b/src/utils/viem.ts index a1540728d..dcda08a7e 100644 --- a/src/utils/viem.ts +++ b/src/utils/viem.ts @@ -28,9 +28,10 @@ export function convertWalletClientToSigner( return { getAddress: async () => account.address, - signMessage: async (message: string) => { - const signature = await walletClient.signMessage({ message, account }) - return signature - }, + signMessage: async (message: string | Uint8Array) => + walletClient.signMessage({ + message: typeof message === 'string' ? message : { raw: message }, + account, + }), } } From cd8e49a248b7f3043f7f7239fdbcedb936494e6e Mon Sep 17 00:00:00 2001 From: Ry Racherbaumer Date: Tue, 26 Sep 2023 14:19:58 -0500 Subject: [PATCH 2/2] test: add message sending test for viem wallets --- test/Message.test.ts | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/test/Message.test.ts b/test/Message.test.ts index 0f95b802b..8b91ca638 100644 --- a/test/Message.test.ts +++ b/test/Message.test.ts @@ -9,6 +9,9 @@ import { InMemoryKeystore, KeystoreError } from '../src/keystore' import { Client, ContentTypeText, InMemoryPersistence } from '../src' import { Wallet } from 'ethers' import { ContentTypeTestKey, TestKeyCodec } from './ContentTypeTestKey' +import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts' +import { createWalletClient, http } from 'viem' +import { mainnet } from 'viem/chains' describe('Message', function () { let aliceWallet: Wallet @@ -218,6 +221,47 @@ describe('Message', function () { expect(restoredDecodedMessage).toEqual(sentMessage) }) + it('round trips V2 text messages with viem', async () => { + const aliceWalletClient = createWalletClient({ + account: privateKeyToAccount(generatePrivateKey()), + chain: mainnet, + transport: http(), + }) + + const aliceClient = await Client.create(aliceWalletClient, { + env: 'local', + privateKeyOverride: alice.encode(), + }) + + const bobWalletClient = createWalletClient({ + account: privateKeyToAccount(generatePrivateKey()), + chain: mainnet, + transport: http(), + }) + + const bobClient = await Client.create(bobWalletClient, { + env: 'local', + privateKeyOverride: bob.encode(), + }) + + const convo = await aliceClient.conversations.newConversation( + bobClient.address + ) + const text = 'hi bob' + const sentMessage = await convo.send(text) + + const sentMessageBytes = sentMessage.toBytes() + expect(sentMessageBytes).toBeDefined() + + const restoredDecodedMessage = await DecodedMessage.fromBytes( + sentMessageBytes, + aliceClient + ) + expect(restoredDecodedMessage.toBytes()).toEqual(sentMessageBytes) + expect(restoredDecodedMessage.content).toEqual(text) + expect(restoredDecodedMessage).toEqual(sentMessage) + }) + it('round trips messages with custom content types', async () => { // Alice has the custom codec and bob does not const aliceClient = await Client.create(aliceWallet, {