Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

init #634

Merged
merged 23 commits into from
Jun 25, 2024
Merged

init #634

Show file tree
Hide file tree
Changes from 13 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
58 changes: 33 additions & 25 deletions apps/site/app/api/post/route.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,51 @@
import { novaPubClient } from '@/config/publicClient'
import { Defender } from '@openzeppelin/defender-sdk'
import {
syndicateClientPost,
generatePostTxnInput,
projectIdPost,
} from '@/config/syndicateClient'
import { ethers } from 'ethers'
import type { NextRequest } from 'next/server'
import { addresses, postGatewayABI } from 'scrypt'
import type { Hex } from 'viem'
import { waitUntilTx, authToken } from '@/lib'

export const maxDuration = 30 // This function can run for a maximum of 30 seconds

export async function POST(req: NextRequest) {
const post = await req.json()

const credentials = {
relayerApiKey: process.env.NONCE_API_UNO,
relayerApiSecret: process.env.NONCE_SECRET_UNO,
if (!syndicateClientPost) {
return new Response(
JSON.stringify({
success: false,
hash: null,
error: 'Syndicate client not initialized',
}),
{
status: 500,
headers: { 'Content-Type': 'application/json' },
},
)
}

try {
const defenderClient = new Defender(credentials)
const provider = defenderClient.relaySigner.getProvider()
const signer = defenderClient.relaySigner.getSigner(provider, {
speed: 'fast',
})

const postGateway = new ethers.Contract(
addresses.postGateway.nova,
postGatewayABI,
signer as unknown as ethers.Signer,
)

const tx = await postGateway.post(post)
const postTx =
await syndicateClientPost.officialActions.transact.sendTransaction(
generatePostTxnInput(post),
)

await novaPubClient.waitForTransactionReceipt({
hash: tx.hash as Hex,
const successfulTxHash = await waitUntilTx({
projectID: projectIdPost as string,
txID: postTx.transactionId,
authToken: authToken as string,
})

return new Response(JSON.stringify({ success: true, hash: tx.hash }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
})
return new Response(
JSON.stringify({ success: true, hash: successfulTxHash }),
{
status: 200,
headers: { 'Content-Type': 'application/json' },
},
)
} catch (error) {
let errorMessage = 'Unknown error'
let statusCode = 500
Expand Down
62 changes: 33 additions & 29 deletions apps/site/app/api/postBatch/route.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,47 @@
import { novaPubClient } from '@/config/publicClient'
import { Defender } from '@openzeppelin/defender-sdk'
import { ethers } from 'ethers'
import type { NextRequest } from 'next/server'
import { addresses, postGatewayABI } from 'scrypt'
import type { Hex } from 'viem'

export const maxDuration = 30 // This function can run for a maximum of 30 seconds
import {
syndicateClientPost,
generatePostBatchTxnInput,
projectIdPost,
} from '@/config/syndicateClient'
import { waitUntilTx, authToken } from '@/lib'

export async function POST(req: NextRequest) {
const postsArray = await req.json()
console.log({ postsArray })

const credentials = {
relayerApiKey: process.env.NONCE_API_UNO,
relayerApiSecret: process.env.NONCE_SECRET_UNO,
if (!syndicateClientPost) {
return new Response(
JSON.stringify({
success: false,
hash: null,
error: 'Syndicate client not initialized',
}),
{
status: 500,
headers: { 'Content-Type': 'application/json' },
},
)
}

try {
const defenderClient = new Defender(credentials)
const provider = defenderClient.relaySigner.getProvider()
const signer = defenderClient.relaySigner.getSigner(provider, {
speed: 'fast',
})
const postTx =
await syndicateClientPost.officialActions.transact.sendTransaction(
generatePostBatchTxnInput(postsArray),
)

const postGateway = new ethers.Contract(
addresses.postGateway.nova,
postGatewayABI,
signer as unknown as ethers.Signer,
)

const tx = await postGateway.postBatch(postsArray)
await novaPubClient.waitForTransactionReceipt({
hash: tx.hash as Hex,
const successfulTxHash = await waitUntilTx({
projectID: projectIdPost as string,
txID: postTx.transactionId,
authToken: authToken as string,
})

return new Response(JSON.stringify({ success: true, hash: tx.hash }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
})
return new Response(
JSON.stringify({ success: true, hash: successfulTxHash }),
{
status: 200,
headers: { 'Content-Type': 'application/json' },
},
)
} catch (error) {
let errorMessage = 'Unknown error'
let statusCode = 500
Expand Down
61 changes: 31 additions & 30 deletions apps/site/app/api/registerFor/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { optimismPubClient } from '@/config/publicClient'
import { Defender } from '@openzeppelin/defender-sdk'
import { ethers } from 'ethers'
import type { NextRequest } from 'next/server'
import { addresses, idRegistryABI } from 'scrypt'
import { type Hex, decodeAbiParameters } from 'viem'
import {
syndicateClientIdRegistry,
generateIdRegistryInput,
projectIdRegistry,
} from '@/config/syndicateClient'
import { waitUntilTx, authToken } from '@/lib'

export const maxDuration = 30 // This function can run for a maximum of 30 seconds

Expand All @@ -15,50 +18,48 @@ export async function POST(req: NextRequest) {
const { to, recovery, deadline, sig } = userWithoutUsername

console.log({ userWithoutUsername })
const credentials = {
relayerApiKey: process.env.IDREGISTRY_API_UNO,
relayerApiSecret: process.env.IDREGISTRY_SECRET_UNO,
}

try {
const defenderClient = new Defender(credentials)
const provider = defenderClient.relaySigner.getProvider()
const signer = defenderClient.relaySigner.getSigner(provider, {
speed: 'fast',
})

const idRegistry = new ethers.Contract(
addresses.idRegistry.optimism,
idRegistryABI,
signer as unknown as ethers.Signer,
if (!syndicateClientIdRegistry) {
return new Response(
JSON.stringify({
success: false,
hash: null,
error: 'Syndicate client not initialized',
}),
{
status: 500,
headers: { 'Content-Type': 'application/json' },
},
)
}

const registerTxn = await idRegistry.registerFor(
to,
recovery,
deadline,
sig,
)
try {
const registerTx =
await syndicateClientIdRegistry.officialActions.transact.sendTransaction(
generateIdRegistryInput({ to, recovery, deadline, sig }),
)

const txnReceipt = await optimismPubClient.waitForTransactionReceipt({
hash: registerTxn.hash as Hex,
const successfulTxHash = await waitUntilTx({
projectID: projectIdRegistry as string,
txID: registerTx.transactionId,
authToken: authToken as string,
})

const [rid, recoveryAddress] = decodeAbiParameters(
const [rid] = decodeAbiParameters(
[
{ name: 'rid', type: 'uint256' },
{ name: 'recoveryAddress', type: 'address' },
],
txnReceipt.logs[0].data,
successfulTxHash.logs[0].data,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jawndiego this is the bug that could potentially have been introduced by new code. the "logs" field is something that specifically part of "transactionReceipt" (official ethereum term) object that is produced by successful transactions. need to make sure that whatever it is we are called ".logs[0].data" on is a txn receipt

)

console.log('rid: ', rid)
console.log('transaction receipt: ', registerTxn)
console.log('transaction receipt: ', successfulTxHash)

return new Response(
JSON.stringify({
success: true,
hash: registerTxn.hash,
hash: successfulTxHash,
rid: rid.toString(),
}),
{
Expand Down
4 changes: 2 additions & 2 deletions apps/site/config/publicClient.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { http, createPublicClient } from 'viem'
import { optimism } from 'viem/chains'
import { optimism, optimismSepolia } from 'viem/chains'
import { arbitrumNova } from './customChainConfig'

export const optimismPubClient = createPublicClient({
chain: optimism,
chain: optimismSepolia,
transport: http(process.env.NEXT_PUBLIC_OPTIMISM_RPC_URL),
})

Expand Down
96 changes: 96 additions & 0 deletions apps/site/config/syndicateClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { SyndicateClient } from '@syndicateio/syndicate-node'
import { addresses } from 'scrypt'

type PostMessage = {
rid: bigint
timestamp: bigint
msgType: number
msgBody: string
}

type Post = {
signer: string
message: PostMessage
hashType: number
hash: string
sigType: number
sig: string
}

type Register = {
to: string
recovery: string
deadline: number
sig: string
}

type PostBatchFunction = {
posts: Post[]
}

export const projectIdPost =
process.env.SYNDICATE_PROJECT_ID_POSTGATEWAY ?? 'Error'
export const projectIdRegistry =
process.env.SYNDICATE_PROJECT_ID_IDREGISTRY ?? 'Error'

export const generatePostBatchTxnInput = (postsArray: PostBatchFunction) => ({
projectId: projectIdPost,
contractAddress: addresses.postGateway.nova,
chainId: 42170,
functionSignature:
'postBatch((address signer, (uint256 rid, uint256 timestamp, uint8 msgType, bytes msgBody) message, uint16 hashType, bytes32 hash, uint16 sigType, bytes sig)[] posts)',
args: {
posts: postsArray,
},
})

export const generatePostTxnInput = (post: Post) => ({
projectId: projectIdPost,
contractAddress: addresses.postGateway.nova,
chainId: 42170,
functionSignature:
'post((address signer, (uint256 rid, uint256 timestamp, uint8 msgType, bytes msgBody) message, uint16 hashType, bytes32 hash, uint16 sigType, bytes sig) post)',
args: {
post: post,
},
})

export const generateIdRegistryInput = (register: Register) => ({
projectId: projectIdRegistry,
contractAddress: addresses.idRegistry.optimism,
chainId: 11155420,
functionSignature:
'registerFor(address to, address recovery, uint256 deadline, bytes sig)',
args: {
to: register.to,
recovery: register.recovery,
deadline: register.deadline,
sig: register.sig,
},
})

const apiPostKey = process.env.SYNDICATE_POST_API_KEY
const apiIdKey = process.env.SYNDICATE_ID_API_KEY

export const syndicateClientPost =
projectIdPost !== 'Error' && apiPostKey
? {
officialActions: new SyndicateClient({
token: () => apiPostKey,
}),
projectId: projectIdPost,
generatePostTxnInput,
generatePostBatchTxnInput,
}
: null

export const syndicateClientIdRegistry =
projectIdRegistry !== 'Error' && apiIdKey
? {
officialActions: new SyndicateClient({
token: () => apiIdKey,
}),
projectId: projectIdRegistry,
generateIdRegistryInput,
}
: null
Loading