okx / js-wallet-sdk

Multi-chain typescript signature sdk, supports bitcoin, ethereum, solana, cosmos, etc.
https://okx.github.io/js-wallet-sdk/#/
MIT License
234 stars 87 forks source link

add psbtSignImplIndex #112

Closed xzone911 closed 5 months ago

xzone911 commented 5 months ago

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch @okxweb3/coin-bitcoin@1.0.19 for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/@okxweb3/coin-bitcoin/.DS_Store b/node_modules/@okxweb3/coin-bitcoin/.DS_Store
new file mode 100644
index 0000000..c88a062
Binary files /dev/null and b/node_modules/@okxweb3/coin-bitcoin/.DS_Store differ
diff --git a/node_modules/@okxweb3/coin-bitcoin/dist/psbtSign.d.ts b/node_modules/@okxweb3/coin-bitcoin/dist/psbtSign.d.ts
index f73513a..751c872 100644
--- a/node_modules/@okxweb3/coin-bitcoin/dist/psbtSign.d.ts
+++ b/node_modules/@okxweb3/coin-bitcoin/dist/psbtSign.d.ts
@@ -12,6 +12,7 @@ export declare function signPsbtWithKeyPathAndScriptPathBatch(psbtHexs: string[]
 export declare function signPsbtWithKeyPathAndScriptPath(psbtStr: string, privateKey: string, network?: Network, opts?: signPsbtOptions): string;
 export declare function signPsbtWithKeyPathAndScriptPathImpl(psbt: Psbt, privateKey: string, network?: Network, autoFinalized?: boolean, signInputs?: toSignInput[]): void;
 export declare function psbtSignImpl(psbt: Psbt, privateKey: string, network?: Network): void;
+export declare function psbtSignImplIndex(psbt: Psbt, privateKey: string,index:number, network?: Network): void;
 export declare function extractPsbtTransaction(txHex: string, network?: Network, maximumFeeRate?: number): string;
 export declare function generateUnsignedListingPsbt(listingData: ListingData, network?: Network, publicKey?: string): string;
 export declare function generateSignedListingPsbt(listingData: ListingData, privateKey: string, network?: Network): string;
diff --git a/node_modules/@okxweb3/coin-bitcoin/dist/psbtSign.js b/node_modules/@okxweb3/coin-bitcoin/dist/psbtSign.js
index adecfb9..6704764 100644
--- a/node_modules/@okxweb3/coin-bitcoin/dist/psbtSign.js
+++ b/node_modules/@okxweb3/coin-bitcoin/dist/psbtSign.js
@@ -314,6 +314,42 @@ function psbtSignImpl(psbt, privateKey, network) {
     }
 }
 exports.psbtSignImpl = psbtSignImpl;
+function psbtSignImplIndex(psbt, privateKey, index,network) {
+    network = network || bitcoinjs_lib_1.networks.bitcoin;
+    const privKeyHex = (0, txBuild_1.privateKeyFromWIF)(privateKey, network);
+    const signer = {
+        publicKey: Buffer.alloc(0),
+        sign(hash) {
+            return (0, txBuild_1.sign)(hash, privKeyHex);
+        },
+        signSchnorr(hash) {
+            const tweakedPrivKey = taproot.taprootTweakPrivKey(crypto_lib_1.base.fromHex(privKeyHex));
+            return Buffer.from(schnorr.sign(hash, tweakedPrivKey, crypto_lib_1.base.randomBytes(32)));
+        },
+    };
+    const allowedSighashTypes = [
+        bitcoinjs_lib_1.Transaction.SIGHASH_SINGLE | bitcoinjs_lib_1.Transaction.SIGHASH_ANYONECANPAY,
+        bitcoinjs_lib_1.Transaction.SIGHASH_ALL | bitcoinjs_lib_1.Transaction.SIGHASH_ANYONECANPAY,
+        bitcoinjs_lib_1.Transaction.SIGHASH_ALL,
+        bitcoinjs_lib_1.Transaction.SIGHASH_DEFAULT
+    ];
+        if ((0, bip371_1.isTaprootInput)(psbt.data.inputs[index])) {
+            const input = psbt.data.inputs[index];
+            if (!input.tapInternalKey) {
+                input.tapInternalKey = (0, bip371_1.toXOnly)((0, txBuild_1.wif2Public)(privateKey, network));
+            }
+            signer.publicKey = Buffer.from(taproot.taprootTweakPubkey((0, bip371_1.toXOnly)((0, txBuild_1.wif2Public)(privateKey, network)))[0]);
+        }
+        else {
+            signer.publicKey = (0, txBuild_1.wif2Public)(privateKey, network);
+        }
+        try {
+            psbt.signInput(index, signer, allowedSighashTypes);
+        }
+        catch (e) {
+        }
+}
+exports.psbtSignImplIndex = psbtSignImplIndex;
 function extractPsbtTransaction(txHex, network, maximumFeeRate) {
     const psbt = psbt_1.Psbt.fromHex(txHex, {
         network,

This issue body was partially generated by patch-package.

luyishisi commented 5 months ago

Hi, thank you for your contribution and sharing. We will consider optimizing it in the next version~