Skip to content

Commit

Permalink
Merge pull request #913 from ava-labs/addSigToAllCred
Browse files Browse the repository at this point in the history
chore: add sig to all credentials
  • Loading branch information
ruijialin-avalabs authored Jan 6, 2025
2 parents ef80a85 + cba36c9 commit 06c8738
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
8 changes: 3 additions & 5 deletions examples/p-chain/etna/createChain.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { addTxSignatures, pvm, utils } from '../../../src';
import { pvm, utils } from '../../../src';
import { setupEtnaExample } from './utils/etna-helper';
import { testGenesisData } from '../../../src/fixtures/transactions';
import { getEnvVars } from '../../utils/getEnvVars';
import { addSigToAllCreds } from './utils/addSignatureToAllCred';

/**
* Create a new chain on the P-Chain.
Expand Down Expand Up @@ -38,10 +39,7 @@ const createChainTxExample = async () => {
context,
);

await addTxSignatures({
unsignedTx: tx,
privateKeys: [utils.hexToBuffer(PRIVATE_KEY)],
});
await addSigToAllCreds(tx, utils.hexToBuffer(PRIVATE_KEY));

return pvmApi.issueSignedTx(tx.getSignedTx());
};
Expand Down
18 changes: 18 additions & 0 deletions examples/p-chain/etna/utils/addSignatureToAllCred.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { secp256k1, type UnsignedTx } from '../../../../src';

export const addSigToAllCreds = async (
unsignedTx: UnsignedTx,
privateKey: Uint8Array,
) => {
const unsignedBytes = unsignedTx.toBytes();
const publicKey = secp256k1.getPublicKey(privateKey);

if (!unsignedTx.hasPubkey(publicKey)) {
return;
}
const signature = await secp256k1.sign(unsignedBytes, privateKey);

for (let i = 0; i < unsignedTx.getCredentials().length; i++) {
unsignedTx.addSignatureAt(signature, i, 0);
}
};

0 comments on commit 06c8738

Please sign in to comment.