-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from getsafle/test
Sign transaction and get fees update
- Loading branch information
Showing
7 changed files
with
58 additions
and
37 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
module.exports = { | ||
SOCHAIN_API_KEY: 'jsGr7ioLvkz1Hfo6uwH_CGYdOnIpWtrJ', | ||
SOCHAIN_BASE_URL: "https://sochain.com/api/v3/", | ||
BLOCKCYPHER_BASE_URL: "https://api.blockcypher.com/v1/btc/" | ||
} |
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 |
---|---|---|
@@ -1,37 +1,50 @@ | ||
const bitcore = require("bitcore-lib") | ||
const bitcoin = require("bitcoinjs-lib") | ||
|
||
const {getFeeAndInput} = require('./calculateFeeAndInput') | ||
|
||
async function signTransaction(from, to, amountToSend, URL, privateKey, satPerByte, headers) { | ||
|
||
const transaction = new bitcore.Transaction(); | ||
async function signTransaction(from, to, amountToSend, URL, privateKey, satPerByte, headers, network) { | ||
const psbt = new bitcoin.Psbt({ network: network }) | ||
|
||
const { totalAmountAvailable, inputs, fee } = await getFeeAndInput(URL, satPerByte, headers) | ||
|
||
if (totalAmountAvailable - amountToSend - fee < 0) { | ||
throw new Error("Balance is too low for this transaction"); | ||
} | ||
|
||
//Set transaction input | ||
transaction.from(inputs); | ||
|
||
// set the recieving address and the amount to send | ||
transaction.to(to, Math.floor(amountToSend)); | ||
|
||
// Set change address - Address to receive the left over funds after transfer | ||
transaction.change(from); | ||
for (const unspentOutput of inputs) { | ||
psbt.addInput({ | ||
hash: unspentOutput.txid, | ||
index: unspentOutput.vout, | ||
witnessUtxo: { | ||
script: Buffer.from(unspentOutput.scriptPubKey, 'hex'), | ||
value: unspentOutput.value // value in satoshi | ||
}, | ||
}) | ||
} | ||
|
||
//manually set transaction fees: 20 satoshis per byte | ||
transaction.fee(fee); | ||
psbt.addOutput({address: to, value: amountToSend}); | ||
|
||
// Sign transaction with your private key | ||
transaction.sign(privateKey); | ||
const change = totalAmountAvailable - amountToSend - fee | ||
psbt.addOutput({address: from, value: change}); | ||
|
||
for (let i = 0; i < inputs.length; i++) { | ||
psbt.signInput(i, bitcoin.ECPair.fromWIF(privateKey, bitcoin.networks.testnet)) | ||
} | ||
|
||
// serialize Transactions | ||
const serializedTransaction = transaction.serialize(); | ||
const isValid = psbt.validateSignaturesOfAllInputs() | ||
|
||
return serializedTransaction; | ||
if (isValid) { | ||
psbt.finalizeAllInputs() | ||
// signed transaction hex | ||
const transaction = psbt.extractTransaction() | ||
const signedTransaction = transaction.toHex() | ||
const transactionId = transaction.getId() | ||
|
||
return signedTransaction | ||
} | ||
|
||
} | ||
|
||
|
||
module.exports = signTransaction |
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