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 signTypedData #220

Merged
merged 6 commits into from
Jul 14, 2022
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
2 changes: 1 addition & 1 deletion packages/safe-core-sdk-types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gnosis.pm/safe-core-sdk-types",
"version": "1.2.0",
"version": "1.2.1",
"description": "Safe Core SDK types",
"main": "dist/src/index.js",
"typings": "dist/src/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/safe-core-sdk-types/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export interface Eip712MessageTypes {
export interface GenerateTypedData {
types: Eip712MessageTypes
domain: {
chainId: number | undefined
chainId?: number
verifyingContract: string
}
primaryType: string
Expand Down
4 changes: 2 additions & 2 deletions packages/safe-core-sdk-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gnosis.pm/safe-core-sdk-utils",
"version": "1.2.0",
"version": "1.2.1",
"description": "Safe Core SDK Utilities",
"main": "dist/src/index.js",
"typings": "dist/src/index.d.ts",
Expand Down Expand Up @@ -52,7 +52,7 @@
"typescript": "^4.6.3"
},
"dependencies": {
"@gnosis.pm/safe-core-sdk-types": "^1.2.0",
"@gnosis.pm/safe-core-sdk-types": "^1.2.1",
"semver": "^7.3.7",
"web3-utils": "^1.7.1"
}
Expand Down
10 changes: 6 additions & 4 deletions packages/safe-core-sdk-utils/src/eip-712/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import semverSatisfies from 'semver/functions/satisfies'

const EQ_OR_GT_1_3_0 = '>=1.3.0'

const EIP712_DOMAIN_BEFORE_V130 = [
export const EIP712_DOMAIN_BEFORE_V130 = [
{
type: 'address',
name: 'verifyingContract'
}
]

const EIP712_DOMAIN = [
export const EIP712_DOMAIN = [
{
type: 'uint256',
name: 'chainId'
Expand All @@ -23,7 +23,7 @@ const EIP712_DOMAIN = [
]

// This function returns the types structure for signing off-chain messages according to EIP-712
function getEip712MessageTypes(safeVersion: string): {
export function getEip712MessageTypes(safeVersion: string): {
EIP712Domain: typeof EIP712_DOMAIN | typeof EIP712_DOMAIN_BEFORE_V130
SafeTx: Array<{ type: string; name: string }>
} {
Expand Down Expand Up @@ -55,7 +55,6 @@ export function generateTypedData({
const typedData: GenerateTypedData = {
types: getEip712MessageTypes(safeVersion),
domain: {
chainId: eip712WithChainId ? chainId : undefined,
verifyingContract: safeAddress
},
primaryType: 'SafeTx',
Expand All @@ -68,5 +67,8 @@ export function generateTypedData({
nonce: BigNumber.from(safeTransactionData.nonce)
}
}
if (eip712WithChainId) {
typedData.domain.chainId = chainId
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the error was caused because ethers would try to encode undefined instead of ignoring it if the key is present?

Would it be possible to unit-test this? I feel like this is missed / refactored wrong easily.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that was exactly the issue ;)
I'll add some tests to cover that

}
return typedData
}
61 changes: 61 additions & 0 deletions packages/safe-core-sdk-utils/tests/eip-712.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { SafeTransactionData } from '@gnosis.pm/safe-core-sdk-types'
import chai from 'chai'
import {
EIP712_DOMAIN,
EIP712_DOMAIN_BEFORE_V130,
generateTypedData,
getEip712MessageTypes
} from '../src'

const safeAddress = '0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1'
const safeTransactionData: SafeTransactionData = {
to: '0x000',
value: '111',
data: '0x222',
operation: 333,
safeTxGas: 444,
baseGas: 555,
gasPrice: 666,
gasToken: '0x777',
refundReceiver: '0x888',
nonce: 999
}

describe('EIP-712 sign typed data', () => {
describe('getEip712MessageTypes', async () => {
it('should have the domain typed as EIP712_DOMAIN_BEFORE_V130 for Safes < v1.3.0', async () => {
const { EIP712Domain } = getEip712MessageTypes('1.2.0')
chai.expect(EIP712Domain).to.be.eq(EIP712_DOMAIN_BEFORE_V130)
})

it('should have the domain typed as EIP712_DOMAIN for Safes >= v1.3.0', async () => {
const { EIP712Domain } = getEip712MessageTypes('1.3.0')
chai.expect(EIP712Domain).to.be.eq(EIP712_DOMAIN)
})
})

describe('generateTypedData', async () => {
it('should generate the typed data for Safes < v1.3.0', async () => {
const { domain } = generateTypedData({
safeAddress,
safeVersion: '1.2.0',
chainId: 4,
safeTransactionData
})
chai.expect(domain.verifyingContract).to.be.eq(safeAddress)
chai.expect(domain.chainId).to.be.undefined
})

it('should generate the typed data for for Safes >= v1.3.0', async () => {
const chainId = 4
const { domain } = generateTypedData({
safeAddress,
safeVersion: '1.3.0',
chainId,
safeTransactionData
})
chai.expect(domain.verifyingContract).to.be.eq(safeAddress)
chai.expect(domain.chainId).to.be.eq(chainId)
})
})
})
8 changes: 4 additions & 4 deletions packages/safe-core-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gnosis.pm/safe-core-sdk",
"version": "2.2.0",
"version": "2.2.1",
"description": "Safe Core SDK",
"main": "dist/src/index.js",
"typings": "dist/src/index.d.ts",
Expand Down Expand Up @@ -41,8 +41,8 @@
"devDependencies": {
"@gnosis.pm/safe-contracts-v1.2.0": "npm:@gnosis.pm/[email protected]",
"@gnosis.pm/safe-contracts-v1.3.0": "npm:@gnosis.pm/[email protected]",
"@gnosis.pm/safe-ethers-lib": "^1.2.0",
"@gnosis.pm/safe-web3-lib": "^1.2.0",
"@gnosis.pm/safe-ethers-lib": "^1.2.1",
"@gnosis.pm/safe-web3-lib": "^1.2.1",
"@nomiclabs/hardhat-ethers": "^2.0.5",
"@nomiclabs/hardhat-waffle": "^2.0.3",
"@nomiclabs/hardhat-web3": "^2.0.0",
Expand Down Expand Up @@ -89,7 +89,7 @@
},
"dependencies": {
"@ethersproject/solidity": "^5.6.0",
"@gnosis.pm/safe-core-sdk-types": "^1.2.0",
"@gnosis.pm/safe-core-sdk-types": "^1.2.1",
"@gnosis.pm/safe-deployments": "1.15.0",
"ethereumjs-util": "^7.1.4",
"semver": "^7.3.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/safe-core-sdk/src/utils/signatures/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,6 @@ export async function generateEIP712Signature(
): Promise<EthSignSignature> {
const signerAddress = await ethAdapter.getSignerAddress()
let signature = await ethAdapter.signTypedData(safeTransactionEIP712Args, methodVersion)
signature = adjustVInSignature('eth_signTypedData', signature.replace('0x', ''))
signature = adjustVInSignature('eth_signTypedData', signature)
return new EthSignSignature(signerAddress, signature)
}
6 changes: 3 additions & 3 deletions packages/safe-ethers-lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gnosis.pm/safe-ethers-lib",
"version": "1.2.0",
"version": "1.2.1",
"description": "Ethers library adapter to be used by Safe Core SDK",
"main": "dist/src/index.js",
"typings": "dist/src/index.d.ts",
Expand Down Expand Up @@ -50,8 +50,8 @@
"typescript": "^4.6.3"
},
"dependencies": {
"@gnosis.pm/safe-core-sdk-types": "^1.2.0",
"@gnosis.pm/safe-core-sdk-utils": "^1.2.0"
"@gnosis.pm/safe-core-sdk-types": "^1.2.1",
"@gnosis.pm/safe-core-sdk-utils": "^1.2.1"
},
"peerDependencies": {
"ethers": "^5.6.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/safe-ethers-lib/src/EthersAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class EthersAdapter implements EthAdapter {
async signTypedData(safeTransactionEIP712Args: SafeTransactionEIP712Args): Promise<string> {
if (isTypedDataSigner(this.#signer)) {
const typedData = generateTypedData(safeTransactionEIP712Args)
const signature = this.#signer._signTypedData(
const signature = await this.#signer._signTypedData(
typedData.domain,
{ SafeTx: typedData.types.SafeTx },
typedData.message
Expand Down
6 changes: 3 additions & 3 deletions packages/safe-service-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
],
"homepage": "https://github.com/safe-global/safe-core-sdk#readme",
"devDependencies": {
"@gnosis.pm/safe-ethers-lib": "^1.2.0",
"@gnosis.pm/safe-web3-lib": "^1.2.0",
"@gnosis.pm/safe-ethers-lib": "^1.2.1",
"@gnosis.pm/safe-web3-lib": "^1.2.1",
"@nomiclabs/hardhat-ethers": "^2.0.5",
"@nomiclabs/hardhat-waffle": "^2.0.3",
"@nomiclabs/hardhat-web3": "^2.0.0",
Expand Down Expand Up @@ -75,7 +75,7 @@
},
"dependencies": {
"@ethersproject/abstract-signer": "^5.6.2",
"@gnosis.pm/safe-core-sdk-types": "^1.2.0",
"@gnosis.pm/safe-core-sdk-types": "^1.2.1",
"node-fetch": "^2.6.6"
}
}
6 changes: 3 additions & 3 deletions packages/safe-web3-lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gnosis.pm/safe-web3-lib",
"version": "1.2.0",
"version": "1.2.1",
"description": "Web3 library adapter to be used by Safe Core SDK",
"main": "dist/src/index.js",
"typings": "dist/src/index.d.ts",
Expand Down Expand Up @@ -50,8 +50,8 @@
"typescript": "^4.6.3"
},
"dependencies": {
"@gnosis.pm/safe-core-sdk-types": "^1.2.0",
"@gnosis.pm/safe-core-sdk-utils": "^1.2.0"
"@gnosis.pm/safe-core-sdk-types": "^1.2.1",
"@gnosis.pm/safe-core-sdk-utils": "^1.2.1"
},
"peerDependencies": {
"web3": "^1.7.0"
Expand Down