import { ethers } from 'ethers'
const rawTransactionWithoutSignature = {
to: 'the destination wallet address',
value: ethers.parseEther('0.01'),
chainId: 'the chain id',
gasLimit: 210000,
gasPrice: ethers.parseUnits('10.0', 'gwei'),
}
let nonce = 0
const custodialSignerUrl = 'the custodial signer service url'
async function signTransaction() {
if (typeof window === 'undefined') {
return
}
const fromAccount = 'your own wallet address'
const transactionCount = await provider.getTransactionCount(fromAccount)
nonce = transactionCount + 1
const serializedUnsignedTransaction = ethers.Transaction.from({
...rawTransactionWithoutSignature,
nonce,
}).unsignedSerialized
const signTransactionPayload = {
account: fromAccount,
transaction: serializedUnsignedTransaction,
}
// If using native clients (games) payload can include callbackUrl to which signature will be sent
// const signTransactionPayload = {
// account: fromAccount,
// transaction: serializedUnsignedTransaction,
// callbackUrl: 'http://localhost:3000/signature-callback' // <- your game callback URL here
// };
const id = 'client:2' // must be formatted as `client:${ an identifier number }`
const tag = 'fv/sign-tx' // do not change this
const encodedPayload = {
id,
tag,
payload: signTransactionPayload,
}
window.open(
`${custodialSignerUrl}?request=${base64UrlEncode(
JSON.stringify(encodedPayload),
)}`,
'futureverse_wallet', // don't change this
'popup,right=0,width=290,height=286,menubar=no,toolbar=no,location=no,status=0',
)
window.addEventListener('message', (ev) => {
if (ev.origin === custodialSignerUrl) {
const dataR = signMessageType.decode(ev.data)
if (E.isRight(dataR)) {
transactionSignature = dataR.right.payload.response.signature
}
}
})
}