From 63bf929d817ec0af1317818bb6f04578d79e81bd Mon Sep 17 00:00:00 2001 From: Alejo Acosta Date: Mon, 29 Apr 2024 18:15:06 -0300 Subject: [PATCH] refactor signTransaction to use musig --- src.ts/quais.ts | 5 ++++- src.ts/transaction/index.ts | 4 +++- src.ts/wallet/index.ts | 4 +++- src.ts/wallet/utxohdwallet.ts | 19 +++++++++++++------ 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src.ts/quais.ts b/src.ts/quais.ts index 98db6351..5460f015 100644 --- a/src.ts/quais.ts +++ b/src.ts/quais.ts @@ -84,7 +84,8 @@ export { export { accessListify, computeAddress, recoverAddress, - AbstractTransaction, FewestCoinSelector + AbstractTransaction, FewestCoinSelector, + QiTransaction } from "./transaction/index.js"; export { @@ -120,6 +121,8 @@ export { encryptKeystoreJson, encryptKeystoreJsonSync, quaiHDAccountPath, qiHDAccountPath, + + nobleCrypto, } from "./wallet/index.js"; export { diff --git a/src.ts/transaction/index.ts b/src.ts/transaction/index.ts index 598569f3..cd1a1760 100644 --- a/src.ts/transaction/index.ts +++ b/src.ts/transaction/index.ts @@ -32,4 +32,6 @@ export { FewestCoinSelector } from "./coinselector-fewest.js"; export type { TransactionLike } from "./abstract-transaction.js"; -export type {TxInput, TxOutput} from "./utxo.js"; \ No newline at end of file +export type {TxInput, TxOutput} from "./utxo.js"; + +export { QiTransaction } from "./qi-transaction.js"; \ No newline at end of file diff --git a/src.ts/wallet/index.ts b/src.ts/wallet/index.ts index b27a0f1e..121cd562 100644 --- a/src.ts/wallet/index.ts +++ b/src.ts/wallet/index.ts @@ -45,4 +45,6 @@ export type { KeystoreAccount, EncryptOptions } from "./json-keystore.js" -export { UTXOHDWallet } from "./utxohdwallet.js"; \ No newline at end of file +export { UTXOHDWallet } from "./utxohdwallet.js"; + +export { nobleCrypto } from "./musig-crypto.js"; \ No newline at end of file diff --git a/src.ts/wallet/utxohdwallet.ts b/src.ts/wallet/utxohdwallet.ts index 4177515d..5422eb2a 100644 --- a/src.ts/wallet/utxohdwallet.ts +++ b/src.ts/wallet/utxohdwallet.ts @@ -386,14 +386,22 @@ export class UTXOHDWallet extends BaseWallet { private createMuSigSignature(tx: QiTransaction, hash: Uint8Array): string { const musig = MuSigFactory(nobleCrypto); - const privKeys = tx.txInputs!.map(input => { + // Collect private keys corresponding to the addresses of the inputs + const privKeysSet = new Set(); + tx.txInputs!.forEach(input => { const address = computeAddress(hexlify(input.pubKey)); const utxoAddrObj = this.utxoAddresses.find(utxoAddr => utxoAddr.address === address); - return utxoAddrObj ? utxoAddrObj.privKey : null; - }).filter(privKey => privKey !== null); + if (!utxoAddrObj) { + throw new Error(`Private key not found for public key associated with address: ${address}`); + } + privKeysSet.add(utxoAddrObj.privKey); + }); + const privKeys = Array.from(privKeysSet); + // Create an array of public keys corresponding to the private keys for musig aggregation const pubKeys: Uint8Array[] = privKeys.map(privKey => nobleCrypto.getPublicKey(getBytes(privKey!), true)).filter(pubKey => pubKey !== null) as Uint8Array[]; + // Generate nonces for each public key const nonces = pubKeys.map(pk => musig.nonceGen({publicKey: getBytes(pk!)})); const aggNonce = musig.nonceAgg(nonces); @@ -403,7 +411,7 @@ export class UTXOHDWallet extends BaseWallet { pubKeys ); - //Each signer creates a partial signature + // Create partial signatures for each private key const partialSignatures = privKeys.map((sk, index) => musig.partialSign({ secretKey: getBytes(sk || ''), @@ -415,8 +423,7 @@ export class UTXOHDWallet extends BaseWallet { // Aggregate the partial signatures into a final aggregated signature const finalSignature = musig.signAgg(partialSignatures, signingSession); - - // const isValid = schnorr.verify(finalSignature, hash, aggPublicKey); + return hexlify(finalSignature); }