Skip to content
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

fix: support byte array messages with viem signer #460

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/utils/viem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
}
}
44 changes: 44 additions & 0 deletions test/Message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, {
Expand Down
Loading