javaonflow-cadenceonflow-fcl

Flow account creation throws excepcion "duplicated signature for key"


I'm trying to get into FLOW and I just need to create an account and deploy a contract for creating a new token.

So, first I'm trying to create an account with this contract:

transaction(publicKey: String) {
    prepare(signer: AuthAccount) {
        let account = AuthAccount(payer: signer)
        account.addPublicKey(publicKey.decodeHex())
    }
}

Then, I try to create it with this java method that I got from here

public FlowAddress createAccount(FlowAddress payerAddress, String publicKeyHex) {
        FlowAccountKey payerAccountKey = this.getAccountKey(payerAddress, 0);
        FlowAccountKey newAccountPublicKey = new FlowAccountKey(
                0,
                new FlowPublicKey(publicKeyHex),
                SignatureAlgorithm.ECDSA_P256,
                HashAlgorithm.SHA2_256,
                1,
                0,
                false);

        FlowTransaction tx = new FlowTransaction(
                new FlowScript(loadScript("src/main/resources/cadence/transactions/flow/create_account.cdc")),
                Arrays.asList(new FlowArgument(new StringField(Hex.toHexString(newAccountPublicKey.getEncoded())))),
                this.getLatestBlockID(),
                100L,
                new FlowTransactionProposalKey(
                        payerAddress,
                        payerAccountKey.getId(),
                        payerAccountKey.getSequenceNumber()),
                payerAddress,
                Arrays.asList(payerAddress),
                new ArrayList<>(),
                new ArrayList<>());

        Signer signer = Crypto.getSigner(this.privateKey, payerAccountKey.getHashAlgo());
        tx = tx.addPayloadSignature(payerAddress, 0, signer);
        tx = tx.addEnvelopeSignature(payerAddress, 0, signer);

        FlowId txID = this.accessAPI.sendTransaction(tx);
        FlowTransactionResult txResult = this.waitForSeal(txID);

        return this.getAccountCreatedAddress(txResult);
    }

The params I send are a FlowAddress for the service account and the public key showed up on emulator startup.

Emulator caption

And the error I receive is:

io.grpc.StatusRuntimeException: INTERNAL: duplicated signature for key (address: f8d6e0586b0a20c7, index: 0)

I assume it's not a "coding" error but a conceptual error. Does anyone know how can I create the account?


Solution

  • It turned out that the example code of Flow for Java doesn't work. Deleted this line and worked: tx = tx.addPayloadSignature(payerAddress, 0, signer);