-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #634 from 1ifeworld/jawn/syn-from-scratch
init
- Loading branch information
Showing
9 changed files
with
288 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,32 @@ | ||
import { novaPubClient } from '@/config/publicClient' | ||
import { Defender } from '@openzeppelin/defender-sdk' | ||
import { ethers } from 'ethers' | ||
import { | ||
syndicateClientPost, | ||
generatePostTxnInput, | ||
projectIdPost, | ||
} from '@/config/syndicateClient' | ||
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 { waitForHash } from '@syndicateio/syndicate-node/utils' | ||
|
||
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, | ||
} | ||
|
||
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 postTx = | ||
// biome-ignore lint: | ||
await syndicateClientPost!.transact.sendTransaction( | ||
generatePostTxnInput(post), | ||
) | ||
|
||
const tx = await postGateway.post(post) | ||
const successfulTxHash = await waitForHash(syndicateClientPost, { | ||
projectId: projectIdPost, | ||
transactionId: postTx.transactionId, | ||
}) | ||
|
||
await novaPubClient.waitForTransactionReceipt({ | ||
hash: tx.hash as Hex, | ||
}) | ||
console.log({ successfulTxHash }) | ||
|
||
return new Response(JSON.stringify({ success: true, hash: tx.hash }), { | ||
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 | ||
|
||
if (error instanceof Error) { | ||
errorMessage = error.message | ||
statusCode = | ||
// biome-ignore lint: `status` is not part of the standard Error interface | ||
typeof (error as any).status === 'number' ? (error as any).status : 500 | ||
} | ||
|
||
return new Response( | ||
JSON.stringify({ success: false, hash: null, error: errorMessage }), | ||
{ | ||
status: statusCode, | ||
headers: { 'Content-Type': 'application/json' }, | ||
}, | ||
) | ||
} | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,32 @@ | ||
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 { waitForHash } from '@syndicateio/syndicate-node/utils' | ||
|
||
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, | ||
} | ||
|
||
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 postTx = | ||
// biome-ignore lint: | ||
await syndicateClientPost!.transact.sendTransaction( | ||
generatePostBatchTxnInput(postsArray), | ||
) | ||
|
||
const tx = await postGateway.postBatch(postsArray) | ||
await novaPubClient.waitForTransactionReceipt({ | ||
hash: tx.hash as Hex, | ||
}) | ||
const successfulTxHash = await waitForHash(syndicateClientPost, { | ||
projectId: projectIdPost, | ||
transactionId: postTx.transactionId, | ||
}) | ||
|
||
console.log({ successfulTxHash }) | ||
|
||
return new Response(JSON.stringify({ success: true, hash: tx.hash }), { | ||
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 | ||
|
||
if (error instanceof Error) { | ||
errorMessage = error.message | ||
statusCode = | ||
// biome-ignore lint: `status` is not part of the standard Error interface | ||
typeof (error as any).status === 'number' ? (error as any).status : 500 | ||
} | ||
|
||
return new Response( | ||
JSON.stringify({ success: false, hash: null, error: errorMessage }), | ||
{ | ||
status: statusCode, | ||
headers: { 'Content-Type': 'application/json' }, | ||
}, | ||
) | ||
} | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,52 @@ | ||
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' | ||
|
||
export const maxDuration = 30 // This function can run for a maximum of 30 seconds | ||
import { decodeAbiParameters } from 'viem' | ||
import { | ||
syndicateClientIdRegistry, | ||
generateIdRegistryInput, | ||
projectIdRegistry, | ||
} from '@/config/syndicateClient' | ||
import { waitForHash } from '@syndicateio/syndicate-node/utils' | ||
import { optimismPubClient } from '@/config/publicClient' | ||
import type { Hex } from 'viem' | ||
|
||
export async function POST(req: NextRequest) { | ||
const user = await req.json() | ||
console.log({ user }) | ||
|
||
const { username, ...userWithoutUsername } = user | ||
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, | ||
) | ||
|
||
const registerTxn = await idRegistry.registerFor( | ||
to, | ||
recovery, | ||
deadline, | ||
sig, | ||
const registerTx = | ||
// biome-ignore lint: | ||
await syndicateClientIdRegistry!.transact.sendTransaction( | ||
generateIdRegistryInput({ to, recovery, deadline, sig }), | ||
) | ||
|
||
const txnReceipt = await optimismPubClient.waitForTransactionReceipt({ | ||
hash: registerTxn.hash as Hex, | ||
}) | ||
|
||
const [rid, recoveryAddress] = decodeAbiParameters( | ||
[ | ||
{ name: 'rid', type: 'uint256' }, | ||
{ name: 'recoveryAddress', type: 'address' }, | ||
], | ||
txnReceipt.logs[0].data, | ||
) | ||
|
||
console.log('rid: ', rid) | ||
console.log('transaction receipt: ', registerTxn) | ||
|
||
return new Response( | ||
JSON.stringify({ | ||
success: true, | ||
hash: registerTxn.hash, | ||
rid: rid.toString(), | ||
}), | ||
{ | ||
status: 200, | ||
headers: { 'Content-Type': 'application/json' }, | ||
}, | ||
) | ||
} catch (error) { | ||
console.error(error) | ||
let errorMessage = 'Unknown error' | ||
let statusCode = 500 | ||
|
||
if (error instanceof Error) { | ||
errorMessage = error.message | ||
statusCode = | ||
// biome-ignore lint: `status` is not part of the standard Error interface | ||
typeof (error as any).status === 'number' ? (error as any).status : 500 | ||
} | ||
|
||
return new Response( | ||
JSON.stringify({ success: false, error: errorMessage }), | ||
{ | ||
status: statusCode, | ||
headers: { 'Content-Type': 'application/json' }, | ||
}, | ||
) | ||
} | ||
const successfulTxHash = await waitForHash(syndicateClientIdRegistry, { | ||
projectId: projectIdRegistry, | ||
transactionId: registerTx.transactionId, | ||
}) | ||
|
||
const txnReceipt = await optimismPubClient.waitForTransactionReceipt({ | ||
hash: successfulTxHash as Hex, | ||
}) | ||
|
||
const [rid] = decodeAbiParameters( | ||
[ | ||
{ name: 'rid', type: 'uint256' }, | ||
{ name: 'recoveryAddress', type: 'address' }, | ||
], | ||
|
||
txnReceipt.logs[0].data, | ||
) | ||
|
||
return new Response( | ||
JSON.stringify({ | ||
success: true, | ||
hash: successfulTxHash, | ||
rid: rid.toString(), | ||
}), | ||
{ | ||
status: 200, | ||
headers: { 'Content-Type': 'application/json' }, | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
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: 10, | ||
functionSignature: | ||
'registerFor(address to, address recovery, uint256 deadline, bytes sig)', | ||
args: { | ||
to: register.to, | ||
recovery: register.recovery, | ||
deadline: register.deadline, | ||
sig: register.sig, | ||
}, | ||
}) | ||
|
||
export const syndicateClientPost = new SyndicateClient({ token: process.env.SYNDICATE_POST_API_KEY as string }) | ||
export const syndicateClientIdRegistry = new SyndicateClient({ token: process.env.SYNDICATE_ID_API_KEY as string }) |
Oops, something went wrong.