From ad020207483c464a5c358691fdfb973845d72967 Mon Sep 17 00:00:00 2001 From: ruijialin Date: Thu, 14 Nov 2024 09:53:57 -0500 Subject: [PATCH 1/4] chore: add sig to all credentials --- src/vms/common/unsignedTx.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/vms/common/unsignedTx.ts b/src/vms/common/unsignedTx.ts index 49be51240..df009876b 100644 --- a/src/vms/common/unsignedTx.ts +++ b/src/vms/common/unsignedTx.ts @@ -173,10 +173,13 @@ export class UnsignedTx { private addSignatureForPubKey(sig: Uint8Array, publicKey: Uint8Array) { const coordinates = this.getSigIndicesForPubKey(publicKey); - if (coordinates) { - coordinates.forEach(([index, subIndex]) => { - this.addSignatureAt(sig, index, subIndex); - }); + for (let i = 0; i < this.credentials.length; i++) { + if (coordinates) { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + coordinates.forEach(([_, subIndex]) => { + this.addSignatureAt(sig, i, subIndex); + }); + } } } From bb2d17e79bbc863c514af12ddf9a1d709cde616d Mon Sep 17 00:00:00 2001 From: ruijialin Date: Thu, 14 Nov 2024 10:30:36 -0500 Subject: [PATCH 2/4] chore: update logic --- src/vms/common/unsignedTx.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/vms/common/unsignedTx.ts b/src/vms/common/unsignedTx.ts index df009876b..07df9da09 100644 --- a/src/vms/common/unsignedTx.ts +++ b/src/vms/common/unsignedTx.ts @@ -173,12 +173,13 @@ export class UnsignedTx { private addSignatureForPubKey(sig: Uint8Array, publicKey: Uint8Array) { const coordinates = this.getSigIndicesForPubKey(publicKey); - for (let i = 0; i < this.credentials.length; i++) { - if (coordinates) { - // eslint-disable-next-line @typescript-eslint/no-unused-vars - coordinates.forEach(([_, subIndex]) => { - this.addSignatureAt(sig, i, subIndex); + for (let i = 0; i < this.getCredentials().length; i++) { + if (coordinates[i]) { + coordinates.forEach(([index, subIndex]) => { + this.addSignatureAt(sig, index, subIndex); }); + } else { + this.addSignatureAt(sig, i, 0); } } } From 01c8574c719994e59050158bcc41e26d7f29c904 Mon Sep 17 00:00:00 2001 From: ruijialin Date: Thu, 14 Nov 2024 16:04:38 -0500 Subject: [PATCH 3/4] chore: add sig to all credentials --- examples/p-chain/etna/createChain.ts | 8 +++----- src/vms/common/unsignedTx.ts | 12 ++++-------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/examples/p-chain/etna/createChain.ts b/examples/p-chain/etna/createChain.ts index 86520e589..a0164888d 100644 --- a/examples/p-chain/etna/createChain.ts +++ b/examples/p-chain/etna/createChain.ts @@ -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. @@ -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()); }; diff --git a/src/vms/common/unsignedTx.ts b/src/vms/common/unsignedTx.ts index 07df9da09..49be51240 100644 --- a/src/vms/common/unsignedTx.ts +++ b/src/vms/common/unsignedTx.ts @@ -173,14 +173,10 @@ export class UnsignedTx { private addSignatureForPubKey(sig: Uint8Array, publicKey: Uint8Array) { const coordinates = this.getSigIndicesForPubKey(publicKey); - for (let i = 0; i < this.getCredentials().length; i++) { - if (coordinates[i]) { - coordinates.forEach(([index, subIndex]) => { - this.addSignatureAt(sig, index, subIndex); - }); - } else { - this.addSignatureAt(sig, i, 0); - } + if (coordinates) { + coordinates.forEach(([index, subIndex]) => { + this.addSignatureAt(sig, index, subIndex); + }); } } From cba36c9f10299347d909a96561fa50776f33b8a9 Mon Sep 17 00:00:00 2001 From: ruijialin Date: Thu, 14 Nov 2024 16:05:25 -0500 Subject: [PATCH 4/4] chore: add util --- .../etna/utils/addSignatureToAllCred.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 examples/p-chain/etna/utils/addSignatureToAllCred.ts diff --git a/examples/p-chain/etna/utils/addSignatureToAllCred.ts b/examples/p-chain/etna/utils/addSignatureToAllCred.ts new file mode 100644 index 000000000..d1ed72763 --- /dev/null +++ b/examples/p-chain/etna/utils/addSignatureToAllCred.ts @@ -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); + } +};