• 转账的时候需要手动寻找每一笔未花费记录实在太费时。
  • 比特币转账需要提取或合并所有未花费的交易中的比特币,才能实现交易。
import * as bitcoin from 'bitcoinjs-lib';
import fetch, { Response } from 'node-fetch'; const quantitySat = 0.0001 * 1e8;
const feeSat = 0.0001 * 1e8; (async () => {
try {
let NETWORK = bitcoin.networks.testnet;
const keyPair = bitcoin.ECPair.fromWIF(from_pvtkey, NETWORK);
const p2pkh = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey, network: NETWORK });
let from = p2pkh.address;
const utxoResponse: Response = await fetch(`https://api.blockcypher.com/v1/btc/test3/addrs/${from}`);
const json = await utxoResponse.json();
console.log(json);
let balance = json.balance;
let unspentList: Array<any> = [];
// 过滤掉已经被花费了的交易和未确认的交易,以及自己不在接收列表的交易
const txrefs = json.txrefs;
const unconfirmed_txrefs = json.unconfirmed_txrefs;
if (unconfirmed_txrefs && unconfirmed_txrefs.length > 0) {
// 要把未确认的余额给去掉
balance += json.unconfirmed_balance;
unspentList = unspentList.concat(unconfirmed_txrefs.filter((item: any) => !item.spent_by && item.tx_output_n !== -1));
}
if (txrefs.length > 0) {
unspentList = unspentList.concat(txrefs.filter((item: any) => !item.spent_by && item.tx_output_n !== -1));
} // 构建交易对象
let txb = new bitcoin.TransactionBuilder(NETWORK); // 批量插入未花费交易
unspentList.forEach((item: any) => txb.addInput(item.tx_hash, item.tx_output_n));
// 转出账户
txb.addOutput(to, quantitySat);
// 预留手续费
txb.addOutput(from, balance - quantitySat - feeSat);
// 批量签名,根据索引即可
unspentList.forEach((item: any, index: any) => { txb.sign(index, keyPair) });
// 序列化交易
let tx = txb.build();
console.log(tx.getHash().toString('hex')); // 在一个测试链的节点把交易广布出去
const result = await fetch('https://api.blockcypher.com/v1/btc/test3/txs/push',{
method:'post',
headers:{'Content-Type':'application/json'},
body:JSON.stringify({tx: tx.toHex()})
}); } catch (error) {
console.error(error);
}
})();

bitcoinjs-lib 哈希交易之多笔交易合并。的更多相关文章

  1. 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。

    给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股票. 示例 ...

  2. chunk writer 中需要对抛错的交易进行回滚,同时又要在其他表中记录是哪一笔交易记录失败

    首先根据我有限的知识判断,回滚之后进行写表,该写表动作只能使用listener来进行. 考虑使用的listener有:ItemWriteListener     StepExecutionListen ...

  3. 开源支持东财自动交易的简单量化交易框架pytrader

    pytrader是基于 easytrader 和 easyquotation 的量化交易框架,支持东方财富自动交易,支持进行策略回测. 开源地址:https://github.com/jadepeng ...

  4. Derek解读Bytom源码-创世区块

    作者:Derek 简介 Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom ...

  5. [转帖]Merkle树

    Merkle树 https://www.jianshu.com/p/fc439a8fd0de 所谓比特币交易就是从一个比特币钱包向另一个中转账,每笔交易都有数字签名来保证安全.一个交易一旦发生那么就是 ...

  6. 以太坊虚拟机(EVM)

    转载链接:https://ethfans.org/posts/solidity-chapter1-introduciton-to-smart-contracts 概括总览: 以太坊虚拟机(EVM)是以 ...

  7. Other | 十招教你找到海量PPT模板

    转载自:https://www.douban.com/note/330962457/ 问:PPT模板是什么含义? 答: 先假定你们要的是这种网上到处泛滥成灾的主题PPT吧,下面请耐心看到最后,秋叶老师 ...

  8. cpp 区块链模拟示例(六) 交易

    交易(transaction)是比特币的核心所在,而区块链的唯一目的,也正是为了能够安全可靠地存储交易.在区块链中,交易一旦被创建,就没有任何人能够再去修改或是删除它.在今天的文章中,我们会实现交易的 ...

  9. Bytom交易说明(账户管理模式)

    比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom 该部分主 ...

随机推荐

  1. Spring容器的refresh()介绍

    Spring容器的refresh()[创建刷新]; 1.prepareRefresh()刷新前的预处理; 1).initPropertySources()初始化一些属性设置;子类自定义个性化的属性设置 ...

  2. 虚拟机安装xp系统教程

    xp和win7安装过程有区别 xp.iso和win7.iso不一样 安装xp必须用特殊方法,此方法的大致步骤如下: 1.VMware新建xp类型的虚拟机 2.进入xp的PE系统 3.在PE系统分2个区 ...

  3. Hive安装部署与配置

    Hive安装部署与配置 1.1 Hive安装地址 1)Hive官网地址: http://hive.apache.org/ 2)文档查看地址: https://cwiki.apache.org/conf ...

  4. Prometheus学习笔记(1)Prometheus架构简介

    Prometheus简介和架构 Prometheus 是由 SoundCloud 开源监控告警解决方案.架构图如下: 如上图,Prometheus主要由以下部分组成: Prometheus Serve ...

  5. Linux学习之组管理和权限管理

    Linux组的基本介绍 在Linux中的每个用户必须属于一个组,不能独立于组外.在Linux中每个文件有所有者,所在组,其他组的概念. 1)所有者 2)所在组 3)其他组 4)改变用户所在的组 文件/ ...

  6. 【解决】Error: ENOSPC: no space left on device, watch

    发现问题: 启动 node 项目ReactNative时候出现报错Error: ENOSPC: no space left on device, watch [root@iz2zeihk6kfcls5 ...

  7. 逆向破解之160个CrackMe —— 015

    CrackMe —— 015 160 CrackMe 是比较适合新手学习逆向破解的CrackMe的一个集合一共160个待逆向破解的程序 CrackMe:它们都是一些公开给别人尝试破解的小程序,制作 c ...

  8. httprunner学习23-加解密

    前言 有些接口的请求参数是加密的,返回的接口内容也是需要解密才能看到. 加密接口 比如当我们访问下面这个登陆的接口时,请求参数账号和密码都是需要加密,通过parms参数传过去,服务器才能识别到 没加密 ...

  9. Bean property ‘mapperHelper’ is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

    spring boot 报错: Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property ...

  10. python应用-猜数字

    """ 猜数字游戏(电脑给数字人猜) Author:罗万财 Date:2017-6-3 """ from random import ran ...