This repository has been archived by the owner on Jul 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
1. 简单使用
QuincySx edited this page May 8, 2018
·
3 revisions
ExtendedKey aNew = ExtendedKey.createNew();
Key master1 = aNew.getMaster();
master1.getRawPrivateKey();//获取十六进制私钥
master1.getRawPublicKey();//获取十六进制压缩公钥
master1.getRawPublicKey(false);//获取十六进制公钥 传入参数压缩或者不压缩
//以下方法直接调用会报错
master1.getPrivateKey();//获取格式化的私钥
master1.getPublicKey();//获取格式化的公钥
master1.getAddress();//获取格式化的地址
master1.getRawAddress();//获取十六进制格式的地址
//转化为 比特币公私钥,后边参数是否是测试链
BitCoinECKeyPair parse = BitCoinECKeyPair.parse(master1, true);
//转化为 以太坊公私钥
EthECKeyPair parse1 = EthECKeyPair.parse(master1);
//经过转换之后上述方法可以随便调用
MnemonicCode mnemonicCode = new MnemonicCode();
//参数是助记词的个数
byte[] random = RandomSeed.random(Words.TWELVE);
(可以通过此代码使用助记词恢复随机数 byte[] bytes = mnemonicCode.toEntropy(mnemonicWordsInAList );)
List<String> mnemonicWordsInAList = mnemonicCode.toMnemonic(random);
byte[] seed = MnemonicCode.toSeed(mnemonicWordsInAList, "");
ExtendedKey extendedKey = ExtendedKey.create(seed);
//转换之后再使用和产生一样 备注:
现在只支持 Bitcoin、Bitcoin TestNet、Ethereum
//产生 AddressIndex 方式一
AddressIndex address = BIP44.m()
.purpose44()
.coinType(CoinTypes.Bitcoin)
.account(0)
.external()
.address(0);
//产生 AddressIndex 方式二 (必须符合 Bip44 标准)
AddressIndex address = BIP44.parsePath("m/44'/0'/0'/0/0");
CoinPairDerive coinKeyPair = new CoinPairDerive(extendedKey);
ECKeyPair master = coinKeyPair.derive(address);
//直接使用此步骤已经是转换过的公私钥对了
1. 通过 tx 转换
Transaction transaction= new BTCTransaction(十六进制 TX Byte[]);// HexUtils.fromHex(str) 可以转换
2. 自己生成 Tx,此处不会讲明白一个 BTC 交易是如何构建的,只是说一下 API 调用,来构建一个最基本的交易
BTCTransaction.Input[] inputs = new BTCTransaction.Input[1];
BTCTransaction.Output[] outputs = new BTCTransaction.Output[2];
BTCTransaction.OutPoint outPoint = new BTCTransaction.OutPoint(HexUtils.fromHex("utxo"), utxoIndex);
inputs[0] = new BTCTransaction.Input(outPoint, null, 0xffffffff);
BTCTransaction.Output output1 = new BTCTransaction.Output(转账金额, BTCTransaction.Script.buildOutput("address1"));
outputs[0] = output1;
BTCTransaction.Output output2 = new BTCTransaction.Output(转账金额, BTCTransaction.Script.buildOutput("address2"));
outputs[1] = output2;
BTCTransaction transaction = new BTCTransaction(inputs, outputs, 0);
//签名
ECKeyPair master = xxxxx;
byte[] signByte = transaction.sign(master);
String signTx = HexUtils.toHex(signByte);
Transaction transaction = EthTransaction
.create(to,//对方地址不带 0x
amount,//转账金额
nonce,
gasPrice,
gasLimit,
chainId);//链的 Id
byte[] signByte = transaction.sign(master);
String signTx = HexUtils.toHex(signByte);
EthTransaction txConst = CallTransaction
.createCallTransaction(
nonce.longValue(),
gasPrice.longValue(),
gasLimit.longValue(),
"6b3b3386f46d2872a4bbfda001cebc7dec844593",//合约地址
0, //转账金额
CallTransaction.Function.fromSignature("balanceOf", "address"),//传入方法名 调用智能合约方法名 然后后面传入参数列表的参数类型
"cAfEE4583441D2682bEa06b6E8bFA722a7cea848");//传入参数
byte[] data = txConst.getData();//智能合约调用参数
byte[] signByte = txConst.sign(master);
//都可以通过 HexUtils 开转换 十六进制字符串 与 byte[]
//第一个参数是方法名,第二个参数是合约方法的参数类型(可以有多个)
CallTransaction.Function function = CallTransaction.Function.fromSignature("balanceOf", "address");
//填入合约具体参数(具体参数要和上述类型数量一致)
byte[] data = function.encode("cAfEE4583441D2682bEa06b6E8bFA722a7cea848");