比原链Java版本离线签名
yuan081608
发表于 2022-11-13 23:54:55
108
0
0
Javaimplementationofsigningtransactionofflinetobytomd.
Pre
Getthesourcecode
$gitclonehttps://github.com/Bytom/bytom.git$GOPATH/src/github.com/bytom9 ^4 S3 K* {" ^' g M: d
gitcheckout
$gitcheckoutdev$ m9 G* I" r5 i% y& s. @
Whyneeddevbranch?Becauseyoucouldcalldecodetransactionapifromdevbranchandobtaintx_idandsomeinputsids.
Build
$cd$GOPATH/src/github.com/bytom
$makebytomd#buildbytomd# {# J& y8 s( `- }, u
$makebytomcli#buildbytomcli
Whensuccessfullybuildingtheproject,thebytomandbytomclibinaryshouldbepresentincmd/bytomdandcmd/bytomclidirectory,respectively.
Initialize O; t' K* ?: ?8 s& m
Firstofall,initializethenode:
$cd./cmd/bytomd
$./bytomdinit--chain_idsolonet
launch
$./bytomdnode--mining5 X& w! {4 N, e4 u6 ^
Usage
Buildjar
firstgetsourcecode5 G" y" R( j2 T7 h" c. p1 r
gitclonehttps://github.com/successli/tx_signer.git9 ?2 [% L/ J9 r
getjarpackage- J8 u0 E; A) u$ ?3 Z
$mvnassembly:assembly-Dmaven.test.skip=true( B1 x" p1 ~2 Y% W7 w9 Q& y; _2 l Y
Youcangetajarwithdependencies,andyoucanuseitinyourproject.
Testcases
Need3Parameters:, N+ Z/ l7 m8 Q G+ B0 R/ s
PrivateKeysArray
TemplateObject7 W, r+ m: `, r2 F
AftercallbuildtransactionapireturnaTemplatejsonobject.buildtransactionapi3 z+ {5 A8 w- Z( O; s. p% d
usebytomjavasdkreturnaTemplateobject.2 ^# A; {# v' z4 X( M& o
RawTransaction- s, z1 u9 a$ ^
calldecoderaw-transactionapifromdevbranch.decoderaw-transactionapi6 i; h \+ e) a ?
Callmethod:* B& J, _$ E* a
//returnaTemplateobjectsignedofflinebasically.
Templateresult=signatures.generateSignatures(privates,template,rawTransaction);9 c' j( n( ~, U) o# K+ `' R: d
//useresult'sraw_transactioncallsigntransactionapitobuildanotherdatabutnotneedpasswordorprivatekey.: K! y% r$ q2 }
Single-keyExample:3 J, ?& i; K1 {' n% ]1 E1 K
@Test6 ^$ F" @9 `% s" {2 t
//使用SDK来构造Template对象参数,单签
publicvoidtestSignSingleKey()throwsBytomException{; m; P& \3 }$ ~0 i% F# H8 `
Clientclient=Client.generateClient();
Stringasset_id="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";1 `2 l' U6 j/ @) _3 q' t4 @4 {6 {
Stringaddress="sm1qvyus3s5d7jv782syuqe3qrh65fx23lgpzf33em";5 L! b, f7 Q3 \# B0 d# [
//buildtransactionobtainaTemplateobject3 Z( }1 L0 w' x$ U# z
Templatetemplate=newTransaction.Builder()0 e8 u6 h( V5 p+ f' M9 z
.addAction(
newTransaction.Action.SpendFromAccount()
.setAccountId("0G0NLBNU00A02"): @. h T; l2 w" ~& N
.setAssetId(asset_id)
.setAmount(40000000)- V$ k h3 o7 h" f. E# `
)
.addAction(: S) ?# ~0 V% I( f. l# D7 w
newTransaction.Action.SpendFromAccount()
.setAccountId("0G0NLBNU00A02")
.setAssetId(asset_id)8 z5 `- ]2 G% z6 ?; Z8 J
.setAmount(300000000)# z, N$ a5 l0 K3 C
)6 j' E* A4 p, ^% Q% u
.addAction(
newTransaction.Action.ControlWithAddress()3 \! l, q0 S( {1 u- O" M! C
.setAddress(address)& j. `7 H3 M, H% l I0 X
.setAssetId(asset_id): z F9 f) K' {8 C6 O8 Z
.setAmount(30000000)
).build(client);
logger.info("template:"+template.toJson());
//useTemplateobject'sraw_transactionidtodecoderaw_transactionobtainaRawTransactionobject
RawTransactiondecodedTx=RawTransaction.decode(client,template.rawTransaction);
logger.info("decodeTx:"+decodedTx.toJson());8 w; H* C$ Q0 G
//needaprivatekeyarray
String[]privateKeys=newString[]{"10fdbc41a4d3b8e5a0f50dd3905c1660e7476d4db3dbd9454fa4347500a633531c487e8174ffc0cfa76c3be6833111a9b8cd94446e37a76ee18bb21a7d6ea66b"};" o: a ]/ {, f$ Q5 a- L$ _ V/ n
logger.info("privatekey:"+privateKeys[0]);5 [- L, t- N U/ p. k8 N
//callofflinesignmethodtoobtainabasicofflinesignedtemplate
Signaturessignatures=newSignaturesImpl();
TemplatebasicSigned=signatures.generateSignatures(privateKeys,template,decodedTx);
logger.info("basicsignedraw:"+basicSigned.toJson());# q* _. m2 O* e- s! B( F
//callsigntransactionapitocalculatewholeraw_transactionid) c1 H* _7 c$ z
//signpasswordisNoneoranotherrandomString. p# P+ t$ p: F( F$ ]0 A' i
Templateresult=newTransaction.SignerBuilder().sign(client,
basicSigned,"");
logger.info("resultraw_transaction:"+result.toJson());
//successtosubmittransaction0 p9 T: @+ c, E; Y' u* C+ G* c
}
Multi-keysExample:! M7 }) F2 z' Z
Needanaccounthastwoormorekeys.
@Test
//使用SDK来构造Template对象参数,多签9 ?. o4 T3 y) ~- [- s" ]$ P
publicvoidtestSignMultiKeys()throwsBytomException{. a5 d: D K. o) w3 O
Clientclient=Client.generateClient();
Stringasset_id="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";5 E5 [1 O* l/ D* F2 [3 I
Stringaddress="sm1qvyus3s5d7jv782syuqe3qrh65fx23lgpzf33em";
//buildtransactionobtainaTemplateobject W* o/ R6 H( p
//account0G1RPP6OG0A06hastwokeys
Templatetemplate=newTransaction.Builder()/ h1 G6 c5 N) Q' W" ]' I8 m) J6 b
.setTtl(10)
.addAction(
newTransaction.Action.SpendFromAccount()
.setAccountId("0G1RPP6OG0A06")
.setAssetId(asset_id)* o4 A D( d! D5 p
.setAmount(40000000)$ f- `; |! R9 E% `# T: r& f7 c1 e
)
.addAction(, j$ f( Q% I; b; s
newTransaction.Action.SpendFromAccount()
.setAccountId("0G1RPP6OG0A06")
.setAssetId(asset_id)+ Q( M! `9 S$ i& S, G2 e; h
.setAmount(300000000)
)
.addAction(
newTransaction.Action.ControlWithAddress()
.setAddress(address)
.setAssetId(asset_id): l5 k5 Q# I. {9 |) t3 b0 @" N
.setAmount(30000000)" ?! H$ V; `, e* c$ ]
).build(client);/ m/ E7 u, K0 ^) ]! F2 q* N
logger.info("template:"+template.toJson());7 F! w; |% V3 c7 x+ ^. [9 k
//useTemplateobject'sraw_transactionidtodecoderaw_transactionobtainaRawTransactionobject5 q3 X4 U$ r h% v
RawTransactiondecodedTx=RawTransaction.decode(client,template.rawTransaction);
logger.info("decodeTx:"+decodedTx.toJson()); x# G4 T9 Y& P7 j0 i" p) j, i
//needaprivatekeyarray$ n2 x2 k1 ?% d! |* A9 Q
String[]privateKeys=newString[]{"08bdbd6c22856c5747c930f64d0e5d58ded17c4473910c6c0c3f94e485833a436247976253c8e29e961041ad8dfad9309744255364323163837cbef2483b4f67",
"40c821f736f60805ad59b1fea158762fa6355e258601dfb49dda6f672092ae5adf072d5cab2ceaaa0d68dd3fe7fa04869d95afed8c20069f446a338576901e1b"};
logger.info("privatekey1:"+privateKeys[0]);8 P. K2 l9 Q. G5 Z
logger.info("privatekey2:"+privateKeys[1]);
//callofflinesignmethodtoobtainabasicofflinesignedtemplate
Signaturessignatures=newSignaturesImpl();
TemplatebasicSigned=signatures.generateSignatures(privateKeys,template,decodedTx);
logger.info("basicsignedraw:"+basicSigned.toJson());/ Z% D8 N3 b' v S
//callsigntransactionapitocalculatewholeraw_transactionid
//signpasswordisNoneoranotherrandomString& @7 Z8 \0 f% M: m7 y/ @! a
Templateresult=newTransaction.SignerBuilder().sign(client,; [8 K8 z7 N$ `
basicSigned,"");9 {. X: @2 L9 b U2 G
logger.info("resultraw_transaction:"+result.toJson());
//successtosubmittransaction
}- @1 t' O/ X, M5 M) Y ^
Multi-keysandMulti-inputsExample:
@Test$ n* O5 [) i0 c
//使用SDK来构造Template对象参数,多签,多输入
publicvoidtestSignMultiKeysMultiInputs()throwsBytomException{7 I9 [, H3 T" `4 g3 t' ~3 m$ c- e
Clientclient=Client.generateClient();
Stringasset_id="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
Stringaddress="sm1qvyus3s5d7jv782syuqe3qrh65fx23lgpzf33em";
//buildtransactionobtainaTemplateobject! Y" l1 t p+ z6 t/ Y# s" X3 Z; H
Templatetemplate=newTransaction.Builder()/ `. Y( y4 O+ p& b4 d3 r
.setTtl(10)) Y" `7 `( j" |6 @ m: f
//1input4 A' X/ r0 v- y, Z. ]. P
.addAction(/ h: {$ t3 Y5 W# `' D! P' \0 G- p
newTransaction.Action.SpendFromAccount()1 T- @( u) J& t+ f
.setAccountId("0G1RPP6OG0A06")//Multi-keysaccount
.setAssetId(asset_id)7 H* m3 y4 N2 J) F5 _
.setAmount(40000000) @' V6 g4 U1 w Y
) g) Z2 t; Y9 @* }3 S* q" @
.addAction(
newTransaction.Action.SpendFromAccount()8 {! b3 S( g9 v1 y0 f- g: E
.setAccountId("0G1RPP6OG0A06")
.setAssetId(asset_id)+ o- S( m: h& h! }0 n- K
.setAmount(300000000)
)//2input% D* ]6 u% I( Q3 g, t
.addAction(
newTransaction.Action.SpendFromAccount()
.setAccountId("0G1Q6V1P00A02")//Multi-keysaccount* f" ]) K+ P% h7 } {
.setAssetId(asset_id)7 M, n9 D. P, Z% F7 |
.setAmount(40000000)
)
.addAction(0 I. o. Q8 i* O* P5 Z8 _
newTransaction.Action.SpendFromAccount(); p7 ] u* z3 P# {
.setAccountId("0G1Q6V1P00A02")8 z1 z2 E& g0 A0 W
.setAssetId(asset_id)4 g% ~/ I1 A1 h4 s$ [- u" @
.setAmount(300000000)4 m4 W5 h3 c* _, C! S6 J
)
.addAction(
newTransaction.Action.ControlWithAddress()1 Q% ]% V" x/ R/ Y
.setAddress(address)
.setAssetId(asset_id)7 }+ _: V6 q4 P+ J+ H: Z4 \) ?7 |+ v
.setAmount(60000000)9 |/ h) b! C' `
).build(client);' b# k5 C9 c5 W! X- r) ~
logger.info("template:"+template.toJson());! t! Q: x6 K, H$ J3 i! L
//useTemplateobject'sraw_transactionidtodecoderaw_transactionobtainaRawTransactionobject
RawTransactiondecodedTx=RawTransaction.decode(client,template.rawTransaction);1 n1 [5 q0 y/ U2 C/ r
logger.info("decodeTx:"+decodedTx.toJson()); v) y: ]8 h0 B+ O/ b3 ?! ]& [$ P
//needaprivatekeyarray* c" z) C x* W
String[]privateKeys=newString[]{"08bdbd6c22856c5747c930f64d0e5d58ded17c4473910c6c0c3f94e485833a436247976253c8e29e961041ad8dfad9309744255364323163837cbef2483b4f67",& E, D" ~" r8 o6 b$ U
"40c821f736f60805ad59b1fea158762fa6355e258601dfb49dda6f672092ae5adf072d5cab2ceaaa0d68dd3fe7fa04869d95afed8c20069f446a338576901e1b",
"08bdbd6c22856c5747c930f64d0e5d58ded17c4473910c6c0c3f94e485833a436247976253c8e29e961041ad8dfad9309744255364323163837cbef2483b4f67"};) c# c! |( @) K1 T/ A- J
logger.info("privatekey1:"+privateKeys[0]);. u" A* h% }: v" Y
logger.info("privatekey2:"+privateKeys[1]);
//callofflinesignmethodtoobtainabasicofflinesignedtemplate) s6 H# Q5 h9 C, J J% J, [
Signaturessignatures=newSignaturesImpl();
TemplatebasicSigned=signatures.generateSignatures(privateKeys,template,decodedTx);
logger.info("basicsignedraw:"+basicSigned.toJson());
//callsigntransactionapitocalculatewholeraw_transactionid
//signpasswordisNoneoranotherrandomString
Templateresult=newTransaction.SignerBuilder().sign(client,) B7 r& v2 u/ K, E x
basicSigned,"");- K3 `! W0 _% K" R% H, k0 x
logger.info("resultraw_transaction:"+result.toJson());
//successtosubmittransaction/ }5 @) ~1 D2 b& ]2 \7 G
}
成为第一个吐槽的人