Skip to content

Commit

Permalink
Enable signing_url for the PMM protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminLu committed Mar 13, 2023
1 parent ca2ee18 commit eb07084
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,
rfqAddr: pmm,
})
}

const signedOrder = {
...o,
Expand Down

0 comments on commit eb07084

Please sign in to comment.