Skip to content

Commit 95ab95c

Browse files
新增签名事务
1 parent f067c66 commit 95ab95c

1 file changed

Lines changed: 37 additions & 27 deletions

File tree

src/main/java/com/company/keystore/wallet/TxUtility.java

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -84,40 +84,50 @@ public static String CreateRawTransaction(String fromPubkeyStr, String toPubkeyH
8484
}
8585
}
8686

87-
public static String CreateUnsignedTransaction(String fromPubkeyStr, String toPubkeyHashStr, BigDecimal amount, long nonce,String type,String gasPrice){
87+
public static String signRawBasicTransaction1(String RawTransactionHex,String fromPubkeyStr , String prikeyStr) {
8888
try {
89-
//版本号
90-
byte[] version = new byte[1];
91-
version[0] = 0x01;
92-
//类型:WDC转账
93-
byte[] type1 = Hex.decodeHex(type.toCharArray());
94-
//Nonce 无符号64位
95-
byte[] nonece = BigEndian.encodeUint64(nonce + 1);
96-
//签发者公钥哈希 20字节
97-
byte[] fromPubkeyHash = Hex.decodeHex(fromPubkeyStr.toCharArray());
98-
//gas单价
99-
byte[] gasPrice1 = ByteUtil.longToBytes(Long.parseLong(gasPrice));
100-
//转账金额 无符号64位
101-
BigDecimal bdAmount = amount.multiply(BigDecimal.valueOf(rate));
102-
byte[] Amount = ByteUtil.longToBytes(bdAmount.longValue());
103-
//为签名留白
104-
byte[] signull = new byte[64];
105-
//接收者公钥哈希
106-
byte[] toPubkeyHash = Hex.decodeHex(toPubkeyHashStr.toCharArray());
107-
//长度
108-
byte[] allPayload = BigEndian.encodeUint32(0);
109-
byte[] RawTransaction = ByteUtil.merge(version, type1, nonece, fromPubkeyHash, gasPrice1, Amount, signull, toPubkeyHash, allPayload);
110-
String RawTransactionStr = new String(Hex.encodeHex(RawTransaction));
111-
return RawTransactionStr;
89+
byte[] RawTransaction = Hex.decodeHex(RawTransactionHex.toCharArray());
90+
//私钥字节数组
91+
byte[] privkey = Hex.decodeHex(prikeyStr.toCharArray());
92+
//version
93+
byte[] version = ByteUtil.bytearraycopy(RawTransaction, 0, 1);
94+
//type
95+
byte[] type = ByteUtil.bytearraycopy(RawTransaction, 1, 1);
96+
//nonce
97+
byte[] nonce = ByteUtil.bytearraycopy(RawTransaction, 2, 8);
98+
//from
99+
byte[] from = Hex.decodeHex(fromPubkeyStr.toCharArray());
100+
//gasprice
101+
byte[] gasprice = ByteUtil.bytearraycopy(RawTransaction, 42, 8);
102+
//amount
103+
byte[] amount = ByteUtil.bytearraycopy(RawTransaction, 50, 8);
104+
//signo
105+
byte[] signo = ByteUtil.bytearraycopy(RawTransaction, 58, 64);
106+
//to
107+
byte[] to = ByteUtil.bytearraycopy(RawTransaction, 122, 20);
108+
;
109+
//payloadlen
110+
byte[] payloadlen = ByteUtil.bytearraycopy(RawTransaction, 142, 4);
111+
//payload
112+
byte[] payload = ByteUtil.bytearraycopy(RawTransaction, 146, (int) BigEndian.decodeUint32(payloadlen));
113+
byte[] RawTransactionNoSign = ByteUtil.merge(version, type, nonce, from, gasprice, amount, signo, to, payloadlen, payload);
114+
byte[] RawTransactionNoSig = ByteUtil.merge(version, type, nonce, from, gasprice, amount);
115+
//签名数据
116+
byte[] sig = new Ed25519PrivateKey(privkey).sign(RawTransactionNoSign);
117+
byte[] transha = SHA3Utility.keccak256(ByteUtil.merge(RawTransactionNoSig, sig, to, payloadlen, payload));
118+
byte[] signRawBasicTransaction = ByteUtil.merge(version, transha, type, nonce, from, gasprice, amount, sig, to, payloadlen, payload);
119+
String signRawBasicTransactionHex = new String(Hex.encodeHex(signRawBasicTransaction));
120+
return signRawBasicTransactionHex;
112121
} catch (Exception e) {
113122
return "";
114123
}
115124
}
116125

117-
public static JSONObject CreateSignedTransaction(String fromPubkeyStr, String toPubkeyHashStr, BigDecimal amount, String prikeyStr, Long nonce,String type,String gasPrice) {
126+
127+
public static JSONObject CreateSignedTransaction(String unsignHash,String fromPubkeyStr, String prikeyStr) {
118128
try {
119-
String RawTransactionHex = CreateUnsignedTransaction(fromPubkeyStr, toPubkeyHashStr, amount, nonce,type,gasPrice);
120-
byte[] signRawBasicTransaction = Hex.decodeHex(signRawBasicTransaction(RawTransactionHex, prikeyStr).toCharArray());
129+
String RawTransactionHex = signRawBasicTransaction1(unsignHash, fromPubkeyStr, prikeyStr);
130+
byte[] signRawBasicTransaction = Hex.decodeHex(RawTransactionHex.toCharArray());
121131
byte[] hash = ByteUtil.bytearraycopy(signRawBasicTransaction, 1, 32);
122132
String txHash = new String(Hex.encodeHex(hash));
123133
String traninfo = new String(Hex.encodeHex(signRawBasicTransaction));

0 commit comments

Comments
 (0)