-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: bitcoin sign transaction (#52)
Co-authored-by: Suraj Tiwari <surajtiwari020@gmail.com> Co-authored-by: Ujjwal Kumar <31813384+Ujjwal0501@users.noreply.github.com> Co-authored-by: Md Irshad Ansari <irshadjsr21@gmail.com>
- Loading branch information
1 parent
7f1403f
commit 4375dba
Showing
18 changed files
with
486 additions
and
405 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@cypherock/sdk-app-btc': patch | ||
--- | ||
|
||
sign txn proto, test cases and flow update |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { http } from '../utils/http'; | ||
|
||
const baseURL = '/v2/transaction'; | ||
|
||
export const getRawTxnHash = async (params: { | ||
coinType: string; | ||
hash: string; | ||
}): Promise<string> => { | ||
const res = await http.post(`${baseURL}/hex`, params); | ||
return res.data.data; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
declare module 'bip66' { | ||
export const decode: any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import axios from 'axios'; | ||
import { config } from '@cypherock/sdk-utils'; | ||
|
||
export const http = axios.create({ baseURL: config.API_CYPHEROCK }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import * as bip66 from 'bip66'; | ||
import type { Signer } from 'bitcoinjs-lib'; | ||
|
||
import { assert } from '@cypherock/sdk-utils'; | ||
import { getBitcoinJsLib } from './bitcoinjs-lib'; | ||
import { getNetworkFromPath } from './networks'; | ||
|
||
import { ISignTxnInputData, ISignTxnOutputData } from '../operations/types'; | ||
|
||
export const addressToScriptPubKey = ( | ||
address: string, | ||
derivationPath: number[], | ||
) => { | ||
const network = getNetworkFromPath(derivationPath); | ||
|
||
const key = getBitcoinJsLib() | ||
.address.toOutputScript(address, network) | ||
.toString('hex'); | ||
|
||
return key; | ||
}; | ||
|
||
export function isScriptSegwit(script: string) { | ||
return script.startsWith('0014'); | ||
} | ||
|
||
export const createSignedTransaction = (params: { | ||
inputs: ISignTxnInputData[]; | ||
outputs: ISignTxnOutputData[]; | ||
signatures: string[]; | ||
derivationPath: number[]; | ||
}) => { | ||
const { inputs, outputs, signatures, derivationPath } = params; | ||
|
||
const bitcoinjs = getBitcoinJsLib(); | ||
const transaction = new bitcoinjs.Psbt(); | ||
|
||
for (const input of inputs) { | ||
const script = addressToScriptPubKey(input.address, derivationPath); | ||
|
||
const isSegwit = isScriptSegwit(script); | ||
|
||
const txnInput: any = { | ||
hash: input.prevTxnId, | ||
index: input.prevIndex, | ||
}; | ||
|
||
if (isSegwit) { | ||
txnInput.witnessUtxo = { | ||
script: Buffer.from(script, 'hex'), | ||
value: parseInt(input.value, 10), | ||
}; | ||
} else { | ||
assert(input.prevTxn, 'prevTxn is required in input'); | ||
txnInput.nonWitnessUtxo = Buffer.from(input.prevTxn, 'hex'); | ||
} | ||
|
||
transaction.addInput(txnInput); | ||
} | ||
|
||
for (const output of outputs) { | ||
transaction.addOutput({ | ||
address: output.address, | ||
value: parseInt(output.value, 10), | ||
}); | ||
} | ||
|
||
for (let i = 0; i < inputs.length; i += 1) { | ||
const signature = signatures[i]; | ||
|
||
const derLength = parseInt(signature.slice(4, 6), 16) * 2; | ||
const derEncoded = signature.slice(2, derLength + 6); | ||
const decoded = bip66.decode(Buffer.from(derEncoded, 'hex')); | ||
|
||
const signer: Signer = { | ||
publicKey: Buffer.from(signature.slice(signature.length - 66), 'hex'), | ||
sign: () => | ||
Buffer.concat([ | ||
decoded.r.subarray(decoded.r.length - 32), | ||
decoded.s.subarray(decoded.s.length - 32), | ||
]), | ||
}; | ||
|
||
transaction.signInput(i, signer); | ||
} | ||
|
||
transaction.finalizeAllInputs(); | ||
|
||
const hex = transaction.extractTransaction().toHex(); | ||
|
||
return hex; | ||
}; |
Oops, something went wrong.