Skip to content

Commit

Permalink
Merge pull request #55 from consenlabs/feature/pmm_signing_url
Browse files Browse the repository at this point in the history
Enable signing_url for the PMM protocol
  • Loading branch information
BenjaminLu authored Mar 13, 2023
2 parents ca2ee18 + 5631063 commit 56169c4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/handler/newOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,12 @@ export const newOrder = async (ctx) => {
signer,
order,
userAddr.toLowerCase(),
config.addressBookV5.PMM
chainID,
config.addressBookV5.PMM,
{
signingUrl,
salt,
}
)
break
case Protocol.RFQV1:
Expand Down
46 changes: 41 additions & 5 deletions src/signer/pmmv5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '0x-v2-order-utils'
import * as ethUtils from 'ethereumjs-util'
import { utils, Wallet } from 'ethers'
import axios from 'axios'
import { BigNumber, orderBNToString } from '../utils'

const EIP712_ORDER_SCHEMA = {
Expand Down Expand Up @@ -113,8 +114,29 @@ async function signByMMPSigner(
return signatureUtils.convertToSignatureWithType(walletSign, SignatureType.Wallet)
}

export const forwardUnsignedOrder = async (signingUrl: string, orderInfo: any): Promise<string> => {
const resp = await axios.post(signingUrl, orderInfo)
const body = resp.data
if (body.signature) {
return body.signature
} else {
throw new Error('Invalid signature')
}
}

// Move fee factor to salt field
export const buildSignedOrder = async (signer: Wallet, order, userAddr, pmm): Promise<any> => {
export const buildSignedOrder = async (
signer: Wallet,
order,
userAddr,
chainId,
pmm,
options?: {
signingUrl?: string
salt?: string
}
): Promise<any> => {
const signingUrl = options ? options.signingUrl : undefined
const feeFactor = order.feeFactor
order.takerAddress = pmm.toLowerCase()
order.senderAddress = pmm.toLowerCase()
Expand All @@ -130,10 +152,24 @@ export const buildSignedOrder = async (signer: Wallet, order, userAddr, pmm): Pr
console.log(`orderHash: ${orderHash}`)
const orderSignDigest = orderHashUtils.getOrderHashHex(o)
console.log(`orderSignDigest: ${orderSignDigest}`)
const makerWalletSignature =
signer.address.toLowerCase() == o.makerAddress.toLowerCase()
? await signByEOA(orderSignDigest, signer)
: await signByMMPSigner(orderSignDigest, userAddr, feeFactor, signer)
let makerWalletSignature
if (!signingUrl) {
makerWalletSignature =
signer.address.toLowerCase() == o.makerAddress.toLowerCase()
? await signByEOA(orderSignDigest, signer)
: await signByMMPSigner(orderSignDigest, userAddr, feeFactor, signer)
} else {
makerWalletSignature = await forwardUnsignedOrder(signingUrl, {
pmmOrder: o,
feeFactor: feeFactor,
orderHash: orderHash,
orderSignDigest: orderSignDigest,
userAddr: userAddr,
signer: signer.address,
chainId: chainId,
pmmAddr: pmm,
})
}

const signedOrder = {
...o,
Expand Down

0 comments on commit 56169c4

Please sign in to comment.