Skip to content

Commit

Permalink
Update api-kit with new signMessage helper
Browse files Browse the repository at this point in the history
  • Loading branch information
yagopv committed Oct 30, 2023
1 parent 43e7720 commit c19a638
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
14 changes: 7 additions & 7 deletions packages/api-kit/tests/e2e/addMessage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const generateRandomUUID = (): string => {
const generateMessage = () => `${generateRandomUUID()}: I am the owner of the safe`
const safeAddress = '0x3296b3DD454B7c3912F7F477787B503918C50082'

describe.only('addMessage', () => {
describe('addMessage', () => {
before(async () => {
;({ safeApiKit, ethAdapter } = await getServiceClient(
'0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d'
Expand Down Expand Up @@ -55,27 +55,27 @@ describe.only('addMessage', () => {

it('should allow to create a new off-chain message signed with EIP-191', async () => {
const rawMessage = generateMessage()
const messageHash = await protocolKit.hashSafeMessage(rawMessage)
const safeMessageHash = await protocolKit.getSafeMessageHash(messageHash)
const signature = await protocolKit.signHash(safeMessageHash)
const safeMessage = protocolKit.createMessage(rawMessage)
const signedMessage = await protocolKit.signMessage(safeMessage, 'eth_sign')

await chai.expect(
safeApiKit.addMessage(safeAddress, {
message: rawMessage,
signature: signature.data
signature: signedMessage.encodedSignatures()
})
).to.be.fulfilled
})

it('should allow to create a new off-chain message signed with EIP-712', async () => {
const rawMessage = generateMessage()
const signature = await protocolKit.signTypedData(rawMessage)
const safeMessage = protocolKit.createMessage(rawMessage)
const signedMessage = await protocolKit.signMessage(safeMessage, 'eth_signTypedData_v4')

await chai.expect(
safeApiKit.addMessage(safeAddress, {
message: rawMessage,
safeAppId: 123,
signature: signature.data
signature: signedMessage.encodedSignatures()
})
).to.be.fulfilled
})
Expand Down
25 changes: 16 additions & 9 deletions packages/api-kit/tests/e2e/addMessageSignature.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Safe from '@safe-global/protocol-kit'
import { EthAdapter } from '@safe-global/safe-core-sdk-types'
import { EthAdapter, SafeMessage } from '@safe-global/safe-core-sdk-types'
import SafeApiKit from '@safe-global/api-kit/index'
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
Expand All @@ -24,7 +24,7 @@ const generateRandomUUID = (): string => {
const generateMessage = () => `${generateRandomUUID()}: I am the owner of the safe`
const safeAddress = '0x3296b3DD454B7c3912F7F477787B503918C50082'

describe('addMessageSignature', () => {
describe.only('addMessageSignature', () => {
before(async () => {
;({ safeApiKit: safeApiKit1, ethAdapter: ethAdapter1 } = await getServiceClient(
'0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d'
Expand Down Expand Up @@ -58,21 +58,28 @@ describe('addMessageSignature', () => {

it('should allow to add a confirmation signature using a mix of EIP-191 and EIP-712', async () => {
const rawMessage = generateMessage()
const messageHash = await protocolKit1.hashSafeMessage(rawMessage)
const safeMessageHash = await protocolKit1.getSafeMessageHash(messageHash)
const ethSign = await protocolKit1.signHash(safeMessageHash)
const safeMessage = protocolKit1.createMessage(rawMessage)
const signedMessage1: SafeMessage = await protocolKit1.signMessage(safeMessage, 'eth_sign')

await chai.expect(
safeApiKit1.addMessage(safeAddress, {
message: rawMessage,
signature: ethSign.data
signature: [...signedMessage1.signatures.values()][0].data
})
).to.be.fulfilled

const typedDataSign = await protocolKit2.signTypedData(rawMessage)
const signedMessage2 = await protocolKit2.signMessage(signedMessage1, 'eth_signTypedData_v4')

await chai.expect(safeApiKit1.addMessageSignature(safeMessageHash, typedDataSign.data)).to.be
.fulfilled
const safeMessageHash = await protocolKit1.getSafeMessageHash(
protocolKit1.hashSafeMessage(rawMessage)
)

await chai.expect(
safeApiKit1.addMessageSignature(
safeMessageHash,
[...signedMessage2.signatures.values()][1].data
)
).to.be.fulfilled

const confirmedMessage = await safeApiKit1.getMessage(safeMessageHash)

Expand Down
4 changes: 1 addition & 3 deletions packages/protocol-kit/src/Safe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,9 +595,7 @@ class Safe {
safeAddress: await this.getAddress(),
safeVersion: await this.getContractVersion(),
chainId: await this.getEthAdapter().getChainId(),
data: eip712Data.hasOwnProperty('signatures')
? (eip712Data as SafeTransaction).data
: (eip712Data as EIP712TypedData | string)
data: eip712Data.data
}

const signature = await generateEIP712Signature(this.#ethAdapter, safeEIP712Args, methodVersion)
Expand Down

0 comments on commit c19a638

Please sign in to comment.