Skip to content
Open
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
87 changes: 85 additions & 2 deletions src/server/Route.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Hex, Value } from 'ox'
import { Key } from 'porto'
import { Hex, Secp256k1, Value } from 'ox'
import { Account, Key } from 'porto'
import { Route } from 'porto/server'
import { readContract, waitForCallsStatus } from 'viem/actions'
import { describe, expect, test } from 'vitest'
Expand Down Expand Up @@ -89,6 +89,89 @@ describe('merchant', () => {
expect(merchantBalance_post).toBeLessThan(merchantBalance_pre)
})

test('behavior: sponsor with root eoa key', async () => {
// Reproduces https://github.com/ithacaxyz/relay/issues/1516
// Merchant uses their root EOA private key directly in Route.merchant
// without registering it as a named key on their Porto account.
const merchantAdminKey = Key.createHeadlessWebAuthnP256()
const merchantPrivateKey = Secp256k1.randomPrivateKey()
const merchantAccount = Account.fromPrivateKey(merchantPrivateKey, {
keys: [merchantAdminKey],
})
await TestActions.setBalance(client, {
address: merchantAccount.address,
})
await RelayActions.upgradeAccount(client, {
account: merchantAccount,
authorizeKeys: [merchantAdminKey],
})
const { id: deployId } = await RelayActions.sendCalls(client, {
account: merchantAccount,
calls: [],
feeToken: contracts.exp1.address,
})
await waitForCallsStatus(client, { id: deployId })

const route = Route.merchant({
...porto.config,
address: merchantAccount.address,
key: merchantPrivateKey,
})

if (server) await server.closeAsync()
server = await Http.createServer(route.listener)

const userKey = Key.createHeadlessWebAuthnP256()
const userAccount = await TestActions.createAccount(client, {
keys: [userKey],
})

const userBalance_pre = await readContract(client, {
...contracts.exp1,
args: [userAccount.address],
functionName: 'balanceOf',
})
const merchantBalance_pre = await readContract(client, {
...contracts.exp1,
args: [merchantAccount.address],
functionName: 'balanceOf',
})

const result = await RelayActions.sendCalls(client, {
account: userAccount,
calls: [
{
abi: contracts.exp1.abi,
args: [userAccount.address, Value.fromEther('1')],
functionName: 'mint',
to: contracts.exp1.address,
},
],
merchantUrl: server.url,
})

await waitForCallsStatus(client, {
id: result.id,
})

const userBalance_post = await readContract(client, {
...contracts.exp1,
args: [userAccount.address],
functionName: 'balanceOf',
})
const merchantBalance_post = await readContract(client, {
...contracts.exp1,
args: [merchantAccount.address],
functionName: 'balanceOf',
})

// Check if user was credited with 1 EXP.
expect(userBalance_post).toBe(userBalance_pre + Value.fromEther('1'))

// Check if merchant was debited the fee payment.
expect(merchantBalance_post).toBeLessThan(merchantBalance_pre)
})

test('behavior: conditional sponsoring', async () => {
const { server, merchantAccount } = await setup({
sponsor: (request) =>
Expand Down
4 changes: 4 additions & 0 deletions src/server/Route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ export function merchant(options: merchant.Options) {
? await Key.sign(key, {
address: null,
payload: feePayerDigest,
wrap: !(
key.type === 'secp256k1' &&
key.publicKey.toLowerCase() === address.toLowerCase()
),
})
: undefined

Expand Down
Loading