0

I have this seeds

seeds = [b"game".as_ref(),authority.key().as_ref(),player_one_account.games_count.to_be_bytes().as_ref()]

The games_count is a u16.

I want to create a PDA with these seeds :

            let gamesCount = fPlayer.gamesCount.toString();
            gamePDA] = await PublicKey
            .findProgramAddress(
                [
                    anchor.utils.bytes.utf8.encode("game"),
                    user.publicKey.toBuffer(),
                    anchor.utils.bytes.utf8.encode(gamesCount)
                ],
                program.programId
            );

But i am getting Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: Cross-program invocation with unauthorized signer or writable account

3 Answers 3

4

The seeds shoud be:

const [gamePDA] = await PublicKey.findProgramAddress(
  [
    Buffer.from("game"),
    user.publicKey.toBuffer(),
    new anchor.BN(fPlayer.gameCount).toArrayLike(Buffer, "be", "2"),
  ],
  program.programId
);
6
0

After searching for while I found this the issue was anchor.utils.bytes.utf8.encode(gamesCount) and the solution was to write that as new anchor.BN(fPlayer.gamesCount).toBuffer("be",2).

0

For anyone who the other solutions don't work for, you can try this:

Buffer.from(fPlayer.gamesCount.toString(16).padStart(8, '0'), 'hex')

In case you have .to_le_bytes() instead of .to_be_bytes() as the question indicates, you can try this:

Buffer.from(fPlayer.gamesCount.toString(16).padStart(8, '0'), 'hex').reverse()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.