Skip to content

Commit

Permalink
Merge pull request #54 from consenlabs/feature/add_eip1271_eip712_sig…
Browse files Browse the repository at this point in the history
…ning_method

Fix signing
  • Loading branch information
BenjaminLu authored Feb 23, 2023
2 parents 9e909a4 + c25c878 commit ca2ee18
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
44 changes: 22 additions & 22 deletions src/signer/rfqv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ export const forwardUnsignedOrder = async (signingUrl: string, orderInfo: any):
}
}

export const signRFQTx = async (
export const signRFQOrder = async (
chainId: number,
rfqAddr: string,
signedOrder: any,
user: Wallet,
receiverAddr: string,
order: any,
maker: Wallet,
feeFactor = 30,
signatureType = SignatureType.EIP712
) => {
const domain = {
Expand All @@ -106,15 +106,15 @@ export const signRFQTx = async (
verifyingContract: rfqAddr,
}

// The named list of all type definitions
const types = {
fillWithPermit: [
Order: [
{ name: 'takerAddr', type: 'address' },
{ name: 'makerAddr', type: 'address' },
{ name: 'takerAssetAddr', type: 'address' },
{ name: 'makerAssetAddr', type: 'address' },
{ name: 'takerAssetAmount', type: 'uint256' },
{ name: 'makerAssetAmount', type: 'uint256' },
{ name: 'takerAddr', type: 'address' },
{ name: 'receiverAddr', type: 'address' },
{ name: 'salt', type: 'uint256' },
{ name: 'deadline', type: 'uint256' },
{ name: 'feeFactor', type: 'uint256' },
Expand All @@ -123,24 +123,24 @@ export const signRFQTx = async (

// The data to sign
const value = {
makerAddr: signedOrder.makerAddr,
takerAssetAddr: signedOrder.takerAssetAddr,
makerAssetAddr: signedOrder.makerAssetAddr,
takerAssetAmount: signedOrder.takerAssetAmount.toString(),
makerAssetAmount: signedOrder.makerAssetAmount.toString(),
takerAddr: signedOrder.takerAddr,
receiverAddr: receiverAddr,
salt: signedOrder.salt.toString(),
deadline: signedOrder.deadline.toString(),
feeFactor: signedOrder.feeFactor.toString(),
takerAddr: order.takerAddr,
makerAddr: order.makerAddr,
takerAssetAddr: order.takerAssetAddr,
makerAssetAddr: order.makerAssetAddr,
takerAssetAmount: order.takerAssetAmount.toString(),
makerAssetAmount: order.makerAssetAmount.toString(),
salt: order.salt.toString(),
deadline: order.deadline.toString(),
feeFactor: feeFactor.toString(),
}

const signatureTypedData = await user._signTypedData(domain, types, value)
const signatureTypedData = await maker._signTypedData(domain, types, value)
const signature = Buffer.concat([
ethUtils.toBuffer(signatureTypedData),
ethUtils.toBuffer(signatureType),
])
const eip712sig = '0x' + signature.toString('hex')

return eip712sig
}

Expand Down Expand Up @@ -172,22 +172,22 @@ export const buildSignedOrder = async (
let makerWalletSignature
if (!signingUrl) {
if (signer.address.toLowerCase() == order.makerAddress.toLowerCase()) {
makerWalletSignature = await signRFQTx(
makerWalletSignature = await signRFQOrder(
chainId,
rfqAddr,
rfqOrer,
signer,
rfqOrer.makerAddr,
rfqOrer.feeFactor,
SignatureType.EIP712
)
} else if (walletType === WalletType.ERC1271_EIP712) {
// standard ERC1271 + ERC712 signature
makerWalletSignature = await signRFQTx(
makerWalletSignature = await signRFQOrder(
chainId,
rfqAddr,
rfqOrer,
signer,
rfqOrer.makerAddr,
rfqOrer.feeFactor,
SignatureType.WalletBytes32
)
} else {
Expand Down
19 changes: 9 additions & 10 deletions test/new_order.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -707,32 +707,31 @@ describe('NewOrder', function () {
chainId: chainId,
verifyingContract: rfqAddr,
}
// The named list of all type definitions
const types = {
fillWithPermit: [
Order: [
{ name: 'takerAddr', type: 'address' },
{ name: 'makerAddr', type: 'address' },
{ name: 'takerAssetAddr', type: 'address' },
{ name: 'makerAssetAddr', type: 'address' },
{ name: 'takerAssetAmount', type: 'uint256' },
{ name: 'makerAssetAmount', type: 'uint256' },
{ name: 'takerAddr', type: 'address' },
{ name: 'receiverAddr', type: 'address' },
{ name: 'salt', type: 'uint256' },
{ name: 'deadline', type: 'uint256' },
{ name: 'feeFactor', type: 'uint256' },
],
}
// The data to sign
const value = {
takerAddr: signedOrder.takerAddr,
makerAddr: signedOrder.makerAddr,
takerAssetAddr: signedOrder.takerAssetAddr,
makerAssetAddr: signedOrder.makerAssetAddr,
takerAssetAmount: signedOrder.takerAssetAmount,
makerAssetAmount: signedOrder.makerAssetAmount,
takerAddr: signedOrder.takerAddr,
receiverAddr: signedOrder.makerAddr,
salt: signedOrder.salt,
deadline: signedOrder.deadline,
feeFactor: signedOrder.feeFactor,
takerAssetAmount: signedOrder.takerAssetAmount.toString(),
makerAssetAmount: signedOrder.makerAssetAmount.toString(),
salt: signedOrder.salt.toString(),
deadline: signedOrder.deadline.toString(),
feeFactor: signedOrder.feeFactor.toString(),
}
const recovered = ethers.utils.verifyTypedData(
domain,
Expand Down

0 comments on commit ca2ee18

Please sign in to comment.