Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offline signing. Sometimes signature fails #22391

Closed
apeTim opened this issue Jan 8, 2022 · 1 comment
Closed

Offline signing. Sometimes signature fails #22391

apeTim opened this issue Jan 8, 2022 · 1 comment

Comments

@apeTim
Copy link

apeTim commented Jan 8, 2022

Problem

Hello everyone. I am doing offline transaction signing with solana web3.js. The Problem is that sometimes this functions executes normally and txhash prints to the stdout, but sometimes it throws an error:
Error: Signature verification failed, however, im not changing the code, just running it in the different time.

image

For me, it is really weird. Would appreciate any help on this issue 🙏

Code to reproduce

import { clusterApiUrl, Connection, Keypair, LAMPORTS_PER_SOL, Message, NonceAccount, NONCE_ACCOUNT_LENGTH, SystemProgram, Transaction } from "@solana/web3.js"
import * as k1 from './k1.json'
import * as k2 from './k2.json'
import * as Base58 from 'base-58'

const connection = new Connection(clusterApiUrl('devnet'))

const sender = Keypair.fromSecretKey(Uint8Array.from(k1))
const receiver = Keypair.fromSecretKey(Uint8Array.from(k2))

const amount = 0.01

const INSTRUCTION = SystemProgram.transfer({
    fromPubkey: sender.publicKey,
    toPubkey: receiver.publicKey,
    lamports: amount * LAMPORTS_PER_SOL
})

const getTxSignature = async (signer: Keypair, instruction: SystemProgram, blockhash: string) => {
    const tx = new Transaction()
    tx.add(INSTRUCTION)

    tx.recentBlockhash = blockhash
    tx.feePayer = receiver.publicKey
    tx.sign(signer)

    const signature = tx.signatures.find(signature => signature.publicKey.toBase58() === signer.publicKey.toBase58())

    return signature
}

const send = async () => {
    // get recent blockhash
    const blockhash = (await connection.getRecentBlockhash()).blockhash
    // getting signatures
    const senderSignature = await getTxSignature(sender, INSTRUCTION, blockhash)
    const receiverSignature = await getTxSignature(receiver, INSTRUCTION, blockhash)

    const tx = new Transaction().add(INSTRUCTION)
    tx.recentBlockhash = (await connection.getRecentBlockhash()).blockhash
    tx.feePayer = receiver.publicKey

    tx.addSignature(senderSignature.publicKey, senderSignature.signature)
    tx.addSignature(receiverSignature.publicKey, receiverSignature.signature)

    console.log(`txhash: ${await connection.sendRawTransaction(tx.serialize())}`);
}

send()
@t-nelson
Copy link
Contributor

Hi! We don't use Github for support. Please join us in #developer-support on https://solana.com/discord

@solana-labs solana-labs locked as off-topic and limited conversation to collaborators Jan 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants