Comparing version 0.0.32 to 0.0.33
11
index.js
@@ -0,1 +1,2 @@ | ||
import * as constants from "./lib/constants" | ||
import * as network from './lib/network' | ||
@@ -14,5 +15,8 @@ import * as getAddress from './lib/getAddress' | ||
import {updateWalletWithUnconfirmedUtxos} from "./lib/updateWalletWithUnconfirmedUtxos" | ||
import createAndSendTransaction from "./lib/createAndSendTransaction"; | ||
import createAndSendTransaction from "./lib/createAndSendTransaction" | ||
import getValidatorPublicKeyOfEmail from "./lib/getValidatorPublicKeyOfEmail" | ||
export {network, | ||
export { | ||
constants, | ||
network, | ||
getAddress, | ||
@@ -30,3 +34,4 @@ createHdKeyFromMnemonic, | ||
updateWalletWithUnconfirmedUtxos, | ||
createAndSendTransaction | ||
createAndSendTransaction, | ||
getValidatorPublicKeyOfEmail | ||
} |
@@ -5,5 +5,4 @@ const bitcoin = require('bitcoinjs-lib') | ||
const createAndSendTransaction = async (decryptedSeedPhrase,password,amount,destAddress,our_wallet,nameId,nameValue) => { | ||
const createAndSendTransaction = async (decryptedSeedPhrase,password,amount,destAddress,our_wallet) => { | ||
const hdKey = createHdKeyFromMnemonic(decryptedSeedPhrase,password) | ||
@@ -62,3 +61,3 @@ console.log("sending " + amount + "schwartz to ", destAddress) | ||
const txResponse = await sendToAddress(addressKeys, destAddress, changeAddress, amount, selectedInputs) //chai.expect(addressesOfBob[0].address.substring(0,1)).to.not.be.uppercase | ||
const txResponse = await sendToAddress(addressKeys, destAddress, changeAddress, amount, selectedInputs,nameId,nameValue) //chai.expect(addressesOfBob[0].address.substring(0,1)).to.not.be.uppercase | ||
updateWalletWithUnconfirmedUtxos(txResponse,our_wallet) | ||
@@ -65,0 +64,0 @@ } |
@@ -0,40 +1,77 @@ | ||
import bitcoin from 'bitcoinjs-lib' | ||
import {VERSION, NETWORK_FEE,VALIDATOR_FEE,EMAIL_VERIFICATION_FEE,TRANSACTION_FEE} from './constants' | ||
import broadcastTransaction from './broadcastTransaction' | ||
const bitcoin = require("bitcoinjs-lib") | ||
const VERSION = 0x7100 | ||
export const sendToAddress = (keypair, destAddress, changeAddress, amount, inputsSelected, network) => { | ||
if(!network) network = global.DEFAULT_NETWORK | ||
if(inputsSelected===undefined){ //TODO get required inputs from current available transactions (confirmed / unconfirmed) | ||
} | ||
const inputs = inputsSelected | ||
const txb = new bitcoin.TransactionBuilder(network) | ||
let inputsBalance = 0 | ||
if(inputs){ | ||
inputs.forEach((input) => { | ||
inputsBalance+=input.amount | ||
txb.addInput(input.txid, input.n) | ||
}) | ||
} | ||
const fee = inputs.length*180+2*34*1000 | ||
// https://bitcoin.stackexchange.com/questions/1195/how-to-calculate-transaction-size-before-sending-legacy-non-segwit-p2pkh-p2sh | ||
const changeAmount = Math.round(inputsBalance*100000000-amount-fee) | ||
txb.addOutput(destAddress, amount) | ||
txb.addOutput(changeAddress, changeAmount) | ||
//txb.setVersion(VERSION) //use this for name transactions | ||
if(!Array.isArray(keypair)) | ||
txb.sign(0, keypair) | ||
else{ | ||
for(let i = 0;i<keypair.length;i++){ | ||
console.log('signing with keypair '+i, keypair[i]) | ||
txb.sign(i, keypair[i]) | ||
export const sendToAddress = (keypair, destAddress, changeAddress, amount, inputsSelected, nameId, nameValue, network) => { | ||
let opCodesStackScript = undefined | ||
//check if we want a nameId or nameValue transaction (create OpCodeStackScript) | ||
if (nameId && nameValue && nameId instanceof String && nameValue instanceof String) { | ||
let nameIdPart2 = '' | ||
if (nameId.length > 57) //we have only space for 77 chars in the name in case its longer as in signatures put the rest into the value | ||
{ | ||
nameIdPart2 = nameId.substring(57, nameId.length) | ||
nameId = nameId.substring(0, 57) | ||
nameValue = nameIdPart2 + ' ' + nameValue | ||
} | ||
} | ||
const op_name = conv(nameId, {in: 'binary', out: 'hex'}) | ||
let op_value = conv(nameValue, {in: 'binary', out: 'hex'}) | ||
const op_address = base58.decode(destAddress).toString('hex').substr(2, 40); | ||
opCodesStackScript = bitcoin.script.fromASM( | ||
` | ||
OP_10 | ||
${op_name} | ||
${op_value} | ||
OP_2DROP | ||
OP_DROP | ||
OP_DUP | ||
OP_HASH160 | ||
${op_address} | ||
OP_EQUALVERIFY | ||
OP_CHECKSIG | ||
`.trim().replace(/\s+/g, ' '), | ||
) | ||
} else //if no nameId it could be nameId is a network object | ||
if (nameId instanceof Object) network = nameId | ||
if (!network) network = global.DEFAULT_NETWORK | ||
try { | ||
const txSignedSerialized = txb.build().toHex() | ||
return broadcastTransaction(null,txSignedSerialized,null,null,destAddress) | ||
}catch (e) { | ||
console.log('error broadcasting transaction',e) | ||
} | ||
if (inputsSelected === undefined) { //TODO get required inputs from current available transactions (confirmed / unconfirmed) | ||
} | ||
const inputs = inputsSelected | ||
const txb = new bitcoin.TransactionBuilder(network) | ||
let inputsBalance = 0 | ||
if (inputs) { | ||
inputs.forEach((input) => { | ||
inputsBalance += input.amount | ||
txb.addInput(input.txid, input.n) | ||
}) | ||
} | ||
const fee = inputs.length * 180 + 2 * 34 * 1000 | ||
// https://bitcoin.stackexchange.com/questions/1195/how-to-calculate-transaction-size-before-sending-legacy-non-segwit-p2pkh-p2sh | ||
const changeAmount = Math.round(inputsBalance * 100000000 - amount - fee) | ||
txb.addOutput(destAddress, amount) | ||
txb.addOutput(changeAddress, changeAmount) | ||
if(opCodesStackScript){ | ||
txb.setVersion(VERSION) //use this for name transactions | ||
txb.addOutput(opCodesStackScript, NETWORK_FEE.satoshis) | ||
} | ||
if (!Array.isArray(keypair)) | ||
txb.sign(0, keypair) | ||
else { | ||
for (let i = 0; i < keypair.length; i++) { | ||
console.log('signing with keypair ' + i, keypair[i]) | ||
txb.sign(i, keypair[i]) | ||
} | ||
} | ||
try { | ||
const txSignedSerialized = txb.build().toHex() | ||
return broadcastTransaction(null, txSignedSerialized, null, null, destAddress) | ||
} catch (e) { | ||
console.log('error broadcasting transaction', e) | ||
} | ||
} | ||
@@ -41,0 +78,0 @@ |
{ | ||
"name": "doichain", | ||
"version": "0.0.32", | ||
"version": "0.0.33", | ||
"description": "A js-Doichain library. The goal is to fully cover the Doichain protocol", | ||
@@ -41,4 +41,6 @@ "main": "index.js", | ||
"hdkey": "^1.1.1", | ||
"node-fetch": "^2.6.0" | ||
"node-fetch": "^2.6.0", | ||
"satoshi-bitcoin": "^1.0.4", | ||
"secp256k1": "^3.7.1" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44241
33
887
7
15
+ Addedsatoshi-bitcoin@^1.0.4
+ Addedsecp256k1@^3.7.1
+ Addedbig.js@3.2.0(transitive)
+ Addedsatoshi-bitcoin@1.0.5(transitive)