Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
createAddressPrivatekey() {
super.createAddressPrivatekey();
this.setDefaultNetwork();
// get Cashaddr
var address = new bitcore.PrivateKey(this.privateKey).toAddress();
this.address = address.toString().split(':')[1];
}
async getBalance(isFormatNumber) {
// NOTE: Only works for compressed-WIF format
let ecpair = this.slp.BITBOX.ECPair.fromWIF(wif);
// we set the previous output for the input
// again, this is for bitcore-lib input sig generation
txn.inputs[input_index].output = new Bitcore.Transaction.Output({
satoshis: input_satoshis,
script: redeemScript
});
// produce a signature that is specific to this input
// NOTE: currently only uses ecdsa
let privateKey = new Bitcore.PrivateKey(wif);
var sig = Bitcore.Transaction.Sighash.sign(txn, privateKey, sigHashType, input_index, redeemScript, Bitcore.crypto.BN.fromNumber(input_satoshis));
// add have to add the sighash type manually.. :(
// NOTE: signature is in DER format and is specific to ecdsa & sigHash 0x41
let sigBuf = Buffer.concat([ sig.toDER(), Buffer.alloc(1, sigHashType) ]);
// we can return a object conforming to InputSigData interface
return {
index: input_index,
pubKeyBuf: ecpair.getPublicKeyBuffer(),
signatureBuf: sigBuf
}
}
// we set the previous output for the input
// again, this is for bitcore-lib input sig generation
txn.inputs[input_index].output = new Bitcore.Transaction.Output({
satoshis: input_satoshis,
script: Bitcore.Script.fromAddress(Utils.toCashAddress(ecpair.getAddress()))
});
// Update input to be non-abstract type so we can get the p2pkh sign method
txn.inputs[input_index] = new Bitcore.Transaction.Input.PublicKeyHash(txn.inputs[input_index]);
// produce a signature that is specific to this input
// NOTE: currently only uses ecdsa
let privateKey = new Bitcore.PrivateKey(wif);
let sig = txn.inputs[input_index].getSignatures(txn, privateKey, input_index, sigHashType);
// add have to add the sighash type manually.. :(
// NOTE: signature is in DER format and is specific to ecdsa & sigHash 0x41
let sigBuf = Buffer.concat([ sig[0].signature.toDER(), Buffer.alloc(1, sigHashType) ]);
// we can return a object conforming to InputSigData interface
return {
index: input_index,
pubKeyBuf: ecpair.getPublicKeyBuffer(),
signatureBuf: sigBuf
}
}
describe('V8', () => {
var wallet={};
wallet.beAuthPrivateKey2= new B.PrivateKey();
describe('#listTransactions', () => {
it('should handle partial json results', (done) => {
class PartialJson extends Client {
listTransactions(opts) {
class MyReadable extends Readable {
constructor(options) {
super(options);
var txStr = JSON.stringify(txs);
this.push(txStr.substr(0,10));
this.push(txStr.substr(10));
this.push(null);
}
};
return new MyReadable;
throw (new Error("Parameter \"from\" must be a wallet object or derivation path."));
}
} else {
fromWallet = from;
}
if ((network == "main") || (network == "livenet")) {
network = "mainnet";
}
if ((network == "test") || (network == "test3")) {
network = "testnet";
}
var fromAddress = this.getAddress(fromWallet, network);
switch (APIType) {
case "bitcoincash":
var UTXOList = await this.getUTXOList(fromAddress, network);
var signingKey = new bitcoreCash.PrivateKey(fromWallet.toWIF());
var txHex = await this.buildRawTransaction(UTXOList, fromAddress, toAddress, signingKey, network, amount, fee);
var API = this.server.config.CP.API.bitcoincash;
var url = API.urls.cryptoapis.sendtx;
if (network == null) {
network = API.default.network;
}
var token = this.server.config.CP.API.tokens.cryptoapis;
if ((network == "main") || (network == "livenet")) {
network = "mainnet";
}
if ((network == "test") || (network == "test3")) {
network = "testnet";
}
url = url.split("%network%").join(network);
var sendTxObject = new Object();
sendTxObject.hex = txHex;
throw (new Error("Parameter \"from\" must be a wallet object or derivation path."));
}
} else {
fromWallet = from;
}
if ((network == "main") || (network == "livenet")) {
network = "mainnet";
}
if ((network == "test") || (network == "test3")) {
network = "testnet";
}
var fromAddress = this.getAddress(fromWallet, network);
switch (APIType) {
case "bitcoincash":
var UTXOList = await this.getUTXOList(fromAddress, network);
var signingKey = new bitcoreCash.PrivateKey(fromWallet.toWIF());
var txHex = await this.buildRawTransaction(UTXOList, fromAddress, toAddress, signingKey, network, amount, fee);
var API = this.server.config.CP.API.bitcoincash;
var url = API.urls.bitcoincom.sendtx;
if (network == null) {
network = API.default.network;
}
if ((network == "main") | (network == "mainnet") || (network == "livenet")) {
network = "rest";
}
if ((network == "test") || (network == "test3") || (network == "testnet")) {
network = "trest";
}
url = url.split("%network%").join(network);
var sendTxObject = new Object();
sendTxObject.hexes = [txHex];
var promise = new Promise((resolve, reject) => {
var fromWallet = wallet.derivePath(from);
} else {
throw (new Error("Parameter \"from\" must be a wallet object or derivation path."));
}
} else {
fromWallet = from;
}
if (network == "main") {
var fromAddress = this.getAddress(fromWallet);
} else {
fromAddress = this.getAddress(fromWallet, "test");
}
switch (APIType) {
case "bitcoincash":
var UTXOList = await this.getUTXOList(fromAddress, network);
var signingKey = new bitcoreCash.PrivateKey(fromWallet.toWIF());
var txHex = await this.buildRawTransaction(UTXOList, fromAddress, toAddress, signingKey, network, amount, fee);
try {
var txHash = await this.nativeRPC(network).sendRawTransaction(txHex, true); //alow high transaction fees (if included)
} catch (err) {
console.error(err);
}
//create expected response object
var txObject = new Object();
txObject.tx = new Object();
txObject.tx.hash = txHash;
if (network == "regtest") {
//"mine" a block immediately to push the transaction onto the chain
var mineAddress = this.getDerivedWallet(this.server.config.CP.API.bitcoincash.default.test.cashOutAddrPath, "regtest", true);
var result = await this.generateRegBlocks(mineAddress, 10); //mine a block
}
return (txObject);
app.get_private_key = () => new bch.PrivateKey(app.get_wif());
app.get_address = () => app.get_private_key().toAddress();
export const generateBCHWallet = (keychain = v4()) => {
const pk = new bch.PrivateKey(mkBCHpk(keychain));
return {
privateKey: pk.toWIF(),
address: pk.toAddress().toString(),
wid: v4(),
alias: generateRandomPair(curNames.BCH),
type: curNames.BCH,
created: new Date(),
readOnly: false,
balance: 0
};
};
export const bchTransaction = (pKey, utxo, listOfReceivers) => {
const pk = new bch.PrivateKey(pKey);
const tx = bch
.Transaction()
.from(utxo.map(_ => ({ ..._, address: pk.toAddress().toString() })));
listOfReceivers.forEach(receiver =>
tx.to(receiver.key, toSatoshi(receiver.amount))
);
tx.sign(pk);
return tx.toString();
};