Skip to content

Commit

Permalink
pk_root in permit2 witness
Browse files Browse the repository at this point in the history
  • Loading branch information
sehyunc committed Apr 5, 2024
1 parent e1d341e commit ec6dd84
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const MAX_INT = BigInt(

export default function DepositButton() {
const { baseTicker, baseTokenAmount } = useDeposit()
const { accountId } = useRenegade()
const { accountId, pkRoot } = useRenegade()
const {
isOpen: signInIsOpen,
onOpen: onOpenSignIn,
Expand Down Expand Up @@ -108,6 +108,7 @@ export default function DepositButton() {
permit2Address: env.NEXT_PUBLIC_PERMIT2_CONTRACT as `0x${string}`,
token,
walletClient,
pkRoot,
})

// Deposit
Expand Down
6 changes: 6 additions & 0 deletions trade.renegade.fi/contexts/Renegade/renegade-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ function RenegadeProvider({ children }: React.PropsWithChildren) {
const [accountId, setAccountId] = React.useState<AccountId>()
const [balances, setBalances] = React.useState<Record<BalanceId, Balance>>({})
const [orders, setOrders] = React.useState<Record<OrderId, Order>>({})
// Used in Permit2 witness
const [pkRoot, setPkRoot] = React.useState<bigint[]>([])

// Create task states.
const [taskId, setTaskId] = React.useState<TaskId>()
Expand Down Expand Up @@ -157,6 +159,9 @@ function RenegadeProvider({ children }: React.PropsWithChildren) {
.then(() => {
attemptedAutoSignin.current = accountId
setAccountId(accountId)
const pkRoot = keychain.getPkRoot()
console.log("Setting account, setting pkroot: ", pkRoot)
setPkRoot(pkRoot)
refreshAccount(accountId)

const funded = safeLocalStorageGetItem(`funded_${accountId}`)
Expand Down Expand Up @@ -249,6 +254,7 @@ function RenegadeProvider({ children }: React.PropsWithChildren) {
counterparties,
orderBook,
orders,
pkRoot,
refreshAccount,
setAccount,
setTask,
Expand Down
1 change: 1 addition & 0 deletions trade.renegade.fi/contexts/Renegade/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface RenegadeContextType {
counterparties: Record<PeerId, Counterparty>
orderBook: Record<OrderId, CounterpartyOrder>
orders: Record<OrderId, Order>
pkRoot: bigint[]
refreshAccount: (accountId?: AccountId) => void
setAccount: (oldAccountId?: AccountId, keychain?: Keychain) => Promise<void>
setTask: (newTaskId?: TaskId, taskType?: TaskType) => void
Expand Down
32 changes: 24 additions & 8 deletions trade.renegade.fi/lib/permit2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ const TOKEN_PERMISSIONS = [
{ name: "amount", type: "uint256" },
]

const PERMIT_TRANSFER_FROM_TYPES = {
PermitTransferFrom: [
const WITNESS = [{ name: "pk_root", type: "uint256[4]" }]

// const PERMIT_TRANSFER_FROM_TYPES = {
const PERMIT_WITNESS_TRANSFER_FROM_TYPES = {
// PermitTransferFrom: [
PermitWitnessTransferFrom: [
{ name: "permitted", type: "TokenPermissions" },
{ name: "spender", type: "address" },
{ name: "nonce", type: "uint256" },
{ name: "deadline", type: "uint256" },
{ name: "witness", type: "Witness" },
],
TokenPermissions: TOKEN_PERMISSIONS,
Witness: WITNESS,
}

/**
Expand All @@ -49,14 +55,17 @@ export async function signPermit2({
permit2Address,
token,
walletClient,
pkRoot,
}: {
amount: bigint
chainId: number
spender: Address
permit2Address: Address
token: Token
walletClient: WalletClient
pkRoot: bigint[]
}) {
console.log("Inside sign Permit2", pkRoot)
if (!walletClient.account)
throw new Error("Address not found on wallet client")

Expand All @@ -76,23 +85,28 @@ export async function signPermit2({
spender,
nonce: BigInt(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)),
deadline: BigInt(millisecondsToSeconds(Date.now() + 1000 * 60 * 30)),
witness: { pk_root: pkRoot },
} as const

// Generate signature
const signature = await walletClient.signTypedData({
account: walletClient.account.address,
domain,
types: PERMIT_TRANSFER_FROM_TYPES,
primaryType: "PermitTransferFrom",
// types: PERMIT_TRANSFER_FROM_TYPES,
types: PERMIT_WITNESS_TRANSFER_FROM_TYPES,
// primaryType: "PermitTransferFrom",
primaryType: "PermitWitnessTransferFrom",
message,
})

// Verify signature
const valid = await verifyTypedData({
address: walletClient.account.address,
domain,
types: PERMIT_TRANSFER_FROM_TYPES,
primaryType: "PermitTransferFrom",
// types: PERMIT_TRANSFER_FROM_TYPES,
types: PERMIT_WITNESS_TRANSFER_FROM_TYPES,
// primaryType: "PermitTransferFrom",
primaryType: "PermitWitnessTransferFrom",
message,
signature,
})
Expand All @@ -101,8 +115,10 @@ export async function signPermit2({
// Ensure correct public key is recovered
const hash = hashTypedData({
domain,
types: PERMIT_TRANSFER_FROM_TYPES,
primaryType: "PermitTransferFrom",
// types: PERMIT_TRANSFER_FROM_TYPES,
types: PERMIT_WITNESS_TRANSFER_FROM_TYPES,
// primaryType: "PermitTransferFrom",
primaryType: "PermitWitnessTransferFrom",
message,
})
const recoveredPubKey = publicKeyToAddress(
Expand Down

0 comments on commit ec6dd84

Please sign in to comment.