Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

比原链Java版本离线签名

yuan081608
107 0 0
    tx_signer
3 s- a) S5 Q' ]& a    Javaimplementationofsigningtransactionofflinetobytomd.
* u- p' G1 K% Y3 k7 k# E4 \$ s    Pre$ z$ Y3 T- ]; D* a7 }: d9 Y  e6 }
    Getthesourcecode% C9 s8 ?( b* e* n
    $gitclonehttps://github.com/Bytom/bytom.git$GOPATH/src/github.com/bytom6 a* F5 |5 A, S) f) F0 `7 G
    gitcheckout
, _0 D) ?7 {, P) S( Q# \    $gitcheckoutdev! I) r# q5 p8 Z4 j9 ?
    Whyneeddevbranch?Becauseyoucouldcalldecodetransactionapifromdevbranchandobtaintx_idandsomeinputsids./ P7 [7 u# k+ g  H, A; {" N1 T
    Build# o. l. r( V; E+ V0 G5 K
    $cd$GOPATH/src/github.com/bytom; @. G) |5 G% N& C  ?
    $makebytomd#buildbytomd
- {. E% P0 h2 P( \# n0 o9 w, O    $makebytomcli#buildbytomcli2 f5 Q7 t) ]0 I. U) H: R- C0 p, U
    Whensuccessfullybuildingtheproject,thebytomandbytomclibinaryshouldbepresentincmd/bytomdandcmd/bytomclidirectory,respectively.! f: W  r8 D2 e9 n& w  z/ X( {# D
    Initialize. E6 P! z8 ?9 {
    Firstofall,initializethenode:8 Q4 S2 r5 Z3 O. q7 c" O9 g+ I$ E
    $cd./cmd/bytomd! z# }% i  `! E
    $./bytomdinit--chain_idsolonet/ o- ^' U1 V; b: f1 f* V( ?
    launch+ d4 P) ]$ ~- B
    $./bytomdnode--mining
3 {6 a& W" J% U5 [7 B5 c" ]    Usage
# Z. g2 l; I! t8 F  S    Buildjar& _+ y" f0 i/ X% d, m

! t; u" a! `1 w) _/ ~% I    firstgetsourcecode
2 X7 Y8 ^/ T9 W) s. [: q" X    gitclonehttps://github.com/successli/tx_signer.git
5 T$ d2 A- h! A( g+ B/ h" }& X    getjarpackage
5 {3 `" C* R3 ^9 A" r    $mvnassembly:assembly-Dmaven.test.skip=true
3 v8 q( k1 g6 \" [    Youcangetajarwithdependencies,andyoucanuseitinyourproject./ {9 O  [8 `$ y- e; h5 I* U! Z
( n' Y! k( X+ _6 m: W6 l: `/ `
    Testcases, E5 v, N( q/ z$ v0 I
    Need3Parameters:
- I) G) `" ]# T3 N  q* I/ L( D    PrivateKeysArray+ y: a) ~! c5 W& u
    TemplateObject- w, A- n0 ^, m- p
    AftercallbuildtransactionapireturnaTemplatejsonobject.buildtransactionapi: O( p7 z" j* i/ m: Y$ a
    usebytomjavasdkreturnaTemplateobject.5 r; s6 G  o: R  ^/ n$ i$ N4 ]4 r
    RawTransaction
" F" o* D! d( f2 W+ }. y    calldecoderaw-transactionapifromdevbranch.decoderaw-transactionapi
2 Z. n( \" h1 k& P% S    Callmethod:) n3 i2 [3 U2 h; u
    //returnaTemplateobjectsignedofflinebasically.
' D( `4 L9 L( |. V; e* V    Templateresult=signatures.generateSignatures(privates,template,rawTransaction);
& [& g6 P( b& R* C7 S0 U3 Y  W7 [    //useresult'sraw_transactioncallsigntransactionapitobuildanotherdatabutnotneedpasswordorprivatekey.
" b+ |  ?. G' ~    Single-keyExample:. W- @1 K: N) r$ k* |* i3 b. c. X
    @Test
; G; W3 O2 r/ R' G$ R    //使用SDK来构造Template对象参数,单签
# `6 I5 C- f& d3 J    publicvoidtestSignSingleKey()throwsBytomException{1 B8 g9 k  b$ ]: v  q9 \1 ?' |& q- E
    Clientclient=Client.generateClient();
/ a  E$ o0 |" T4 W/ J8 y* ]    Stringasset_id="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";0 _; V2 I9 E+ ?+ ^: J4 f7 B) K) R. g
    Stringaddress="sm1qvyus3s5d7jv782syuqe3qrh65fx23lgpzf33em";
) X  t: p! ^* S! w    //buildtransactionobtainaTemplateobject; j+ E8 V1 m, |" B7 J
    Templatetemplate=newTransaction.Builder()
7 n6 \3 ]3 f9 p/ F# N) p    .addAction(# S7 ~# g. G; Q
    newTransaction.Action.SpendFromAccount()
0 x% v! K9 l2 ]- A# i, ~    .setAccountId("0G0NLBNU00A02")8 N9 J" Z5 T8 g7 ^8 N
    .setAssetId(asset_id)
+ f$ g2 ~9 C, _7 L3 H% I2 a    .setAmount(40000000)7 b; c( K3 H' T! i  _
    )
0 z% C" S7 ]  e9 c6 D" A" O9 Z    .addAction(
6 A, S8 P$ u( p. V9 I    newTransaction.Action.SpendFromAccount()' `% k7 Z/ g5 Y  I
    .setAccountId("0G0NLBNU00A02")9 q2 C& A" A/ p( u
    .setAssetId(asset_id)
4 C7 T7 T6 V8 B0 t$ e7 M    .setAmount(300000000)
( u# Z7 ]3 Q/ b; F% d7 Y    )# r) w% ?( x, }6 S( w4 J
    .addAction(
6 g& A2 _" q& I0 B, K2 R" P* w( q( I9 h    newTransaction.Action.ControlWithAddress()
, [6 ^' ^+ ]! P# E# F0 Z    .setAddress(address)
2 L9 I& z  x( H' X& K& b9 L% a    .setAssetId(asset_id)
+ F* U, P9 m8 s& I$ Q1 H3 m  I    .setAmount(30000000)
% }2 B1 X5 y1 l    ).build(client);8 k9 V& g4 H1 n5 y5 T) ~4 _" W
    logger.info("template:"+template.toJson());- H& C1 j* @5 [6 X6 [
    //useTemplateobject'sraw_transactionidtodecoderaw_transactionobtainaRawTransactionobject& k' j1 Q( r$ d5 ~: `# e  X$ \
    RawTransactiondecodedTx=RawTransaction.decode(client,template.rawTransaction);
- W& G# q) n. o% ]2 }& ~0 ?    logger.info("decodeTx:"+decodedTx.toJson());
: h' k4 G2 s7 a0 _- v. T$ ]; ^    //needaprivatekeyarray
7 r0 m& c: ~! l    String[]privateKeys=newString[]{"10fdbc41a4d3b8e5a0f50dd3905c1660e7476d4db3dbd9454fa4347500a633531c487e8174ffc0cfa76c3be6833111a9b8cd94446e37a76ee18bb21a7d6ea66b"};$ q7 i1 w6 j, K/ b. \% E
    logger.info("privatekey:"+privateKeys[0]);, @$ {6 f7 f  ~! {
    //callofflinesignmethodtoobtainabasicofflinesignedtemplate
! B* G7 y" [; {) E    Signaturessignatures=newSignaturesImpl();
* r7 z) ]- X+ {    TemplatebasicSigned=signatures.generateSignatures(privateKeys,template,decodedTx);
* s$ |! X6 p  T6 K+ E& V: N    logger.info("basicsignedraw:"+basicSigned.toJson());; {- H! r0 t; M4 _% Z) _( C
    //callsigntransactionapitocalculatewholeraw_transactionid/ B  n, B) X$ G, M; ?; ^/ k0 `
    //signpasswordisNoneoranotherrandomString
; b  q; @4 ], a- Y% ?! ~& Y6 H    Templateresult=newTransaction.SignerBuilder().sign(client,
4 ?3 m1 B( }. H    basicSigned,"");. b1 s- B' H4 V
    logger.info("resultraw_transaction:"+result.toJson());
" A" x, w  y% r: E& P. v    //successtosubmittransaction* y/ t  s" }* y
    }
% p9 {9 }& b$ g0 z- c    Multi-keysExample:
* o# p5 y) f: f9 ~, r    Needanaccounthastwoormorekeys.
' [! B2 w: n' u' E  d' l    @Test/ N2 N8 Z$ ]0 U! ?5 i3 b
    //使用SDK来构造Template对象参数,多签" v, E  Q& x: w& P: T
    publicvoidtestSignMultiKeys()throwsBytomException{
2 c7 H& [+ G* R, X: Z2 t    Clientclient=Client.generateClient();2 J% w1 k  ^2 W
    Stringasset_id="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
# m4 f' s  Y; r2 A: ?+ b$ X( J    Stringaddress="sm1qvyus3s5d7jv782syuqe3qrh65fx23lgpzf33em";
8 P3 j2 |, t) @& {) B5 L- w# H    //buildtransactionobtainaTemplateobject  a% ~* k# [, P: h: Z
    //account0G1RPP6OG0A06hastwokeys+ }% B& c7 s4 a) e% b
    Templatetemplate=newTransaction.Builder()
- n& ~5 _+ b. e3 `; |& P& w  ~    .setTtl(10)* B2 E; r) ~3 v0 K  J
    .addAction(0 f" C; y* F- O9 o
    newTransaction.Action.SpendFromAccount()' T- q; u& l) M2 k# @
    .setAccountId("0G1RPP6OG0A06")  p4 y2 F0 R# I3 }" U' _
    .setAssetId(asset_id)
+ j! H  E# \1 X9 j; J; s    .setAmount(40000000)
$ D  e2 S  E, ~    )
) @1 e# t/ _3 G/ O8 y' _    .addAction(# A* p4 ~/ P; g+ K" L& j& e( X
    newTransaction.Action.SpendFromAccount()+ e$ }& o7 {- a2 i$ B5 J
    .setAccountId("0G1RPP6OG0A06")
: U2 ]4 f) J" D! _/ w/ z5 b    .setAssetId(asset_id)
4 |5 o' b$ S5 L& F+ \    .setAmount(300000000)
7 ?" j/ D0 }* {    )/ {: q* c; N* G$ N& ^9 \+ a
    .addAction(
( O9 n. Q. t) {/ t5 h    newTransaction.Action.ControlWithAddress()
2 ^5 S" T* Y1 D% P6 O    .setAddress(address)# c) C, r" d" q5 v. b7 h5 ]' o  r
    .setAssetId(asset_id)
& I- K" D' F2 X    .setAmount(30000000)
- ?! z+ Q; v) p* Z6 z" ^7 M    ).build(client);" {7 A* q; K( E' U3 @1 |( s
    logger.info("template:"+template.toJson());' o" u1 z- l; c
    //useTemplateobject'sraw_transactionidtodecoderaw_transactionobtainaRawTransactionobject
# X3 t2 G. `( i" a  Z. \    RawTransactiondecodedTx=RawTransaction.decode(client,template.rawTransaction);9 N6 K/ Q4 Z" s% {* S0 T+ f3 j% X2 w* G
    logger.info("decodeTx:"+decodedTx.toJson());
2 B. U1 j0 @. E5 X9 ]) N    //needaprivatekeyarray
9 A( [6 d" [$ J( F8 @7 a    String[]privateKeys=newString[]{"08bdbd6c22856c5747c930f64d0e5d58ded17c4473910c6c0c3f94e485833a436247976253c8e29e961041ad8dfad9309744255364323163837cbef2483b4f67",
0 E  _' N3 j6 R9 ]) e3 u/ J    "40c821f736f60805ad59b1fea158762fa6355e258601dfb49dda6f672092ae5adf072d5cab2ceaaa0d68dd3fe7fa04869d95afed8c20069f446a338576901e1b"};3 r# V$ |/ N0 n* a# X8 Z2 g
    logger.info("privatekey1:"+privateKeys[0]);1 y" o  `5 X2 X
    logger.info("privatekey2:"+privateKeys[1]);
& P7 P1 q5 H! V6 L  m    //callofflinesignmethodtoobtainabasicofflinesignedtemplate+ z. }+ q# Q6 Q9 F: j& H  o7 b
    Signaturessignatures=newSignaturesImpl();
9 Q" r' r* a1 Y0 j" q    TemplatebasicSigned=signatures.generateSignatures(privateKeys,template,decodedTx);; ~( p. K: J; [. n4 p, i
    logger.info("basicsignedraw:"+basicSigned.toJson());  r" H$ H* z2 H2 n: {
    //callsigntransactionapitocalculatewholeraw_transactionid
! j0 \. }( C' B) P1 g9 z    //signpasswordisNoneoranotherrandomString7 K. \) |2 [! \0 l" i- y6 `$ V
    Templateresult=newTransaction.SignerBuilder().sign(client,: z; s' F- i8 ~- ]. U( @+ E$ I$ a6 s
    basicSigned,"");5 P: f6 V3 w7 Z/ {
    logger.info("resultraw_transaction:"+result.toJson());; D' W3 E0 W# R
    //successtosubmittransaction
9 Z+ p' V7 s6 v: H5 v    }$ w' s" y3 `4 |1 N* ]! G7 X
    Multi-keysandMulti-inputsExample:
0 }7 e1 q6 W: C( ~/ y    @Test7 O& C. L* j) i) o# L7 m
    //使用SDK来构造Template对象参数,多签,多输入+ _2 u9 x1 ]" j4 t3 O
    publicvoidtestSignMultiKeysMultiInputs()throwsBytomException{$ Z* S! }/ ^; @: H5 q: ~# L
    Clientclient=Client.generateClient();
0 O7 o# J( T* M; q2 h7 _+ \    Stringasset_id="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
9 Q7 x( G+ H& @8 z( p    Stringaddress="sm1qvyus3s5d7jv782syuqe3qrh65fx23lgpzf33em";6 Z- |" `8 |0 U
    //buildtransactionobtainaTemplateobject
/ N6 V/ c# }" [    Templatetemplate=newTransaction.Builder()
; j( v- L, a0 {7 P* j3 M; F    .setTtl(10)7 u8 W$ U8 F% Y; B& n
    //1input8 T2 u9 \( G. F1 |# n1 \; {
    .addAction(
/ P1 x- b  \" S  H" Q, [4 p    newTransaction.Action.SpendFromAccount()9 h4 Z& t% \3 J3 Q4 m" j
    .setAccountId("0G1RPP6OG0A06")//Multi-keysaccount9 P  O1 w! J% i+ c" h
    .setAssetId(asset_id)
8 T3 X. S( \* q! P) M0 b6 p    .setAmount(40000000)9 [; ~' w  q' T
    )
- z" x- j  m$ o  g2 J    .addAction(
% b( r4 Q5 O. U; [( M    newTransaction.Action.SpendFromAccount()( x  V. p  J0 P  {2 f  @# T
    .setAccountId("0G1RPP6OG0A06")
( c% x) e# g4 Z) G' G6 V0 S8 j- P    .setAssetId(asset_id)4 X* q. }2 G, S
    .setAmount(300000000)# h" X0 r$ y1 X$ C
    )//2input2 u. ^& c' U5 b1 |, e4 D& y
    .addAction(0 A( x6 \1 b, w! Y7 ^1 v6 w1 b
    newTransaction.Action.SpendFromAccount()7 r, |9 t4 Q/ T% @4 l7 K
    .setAccountId("0G1Q6V1P00A02")//Multi-keysaccount4 _/ q" h: B( U7 U
    .setAssetId(asset_id)
# B4 p6 A. _) _( Z$ l. q8 t2 W3 f    .setAmount(40000000)" A% B2 X; e+ R+ c6 ?! f! C' {) {
    )1 u1 V) g* f0 D( [+ K3 i+ [
    .addAction(8 _/ C% X/ ~) S5 f( _
    newTransaction.Action.SpendFromAccount()6 A, O/ J5 ?( w' Z7 Q. y
    .setAccountId("0G1Q6V1P00A02")
, q9 }# ]  s' I) t+ |6 |    .setAssetId(asset_id)9 h, F  a2 Y* u! D
    .setAmount(300000000)8 u+ _. Y. U" B8 D. [  Y
    ); y9 [8 X' z1 b
    .addAction(
( p" E1 x- E9 R    newTransaction.Action.ControlWithAddress(); K: x1 u( R; `* U, O0 j
    .setAddress(address)
% g6 i4 ?; E7 R( E    .setAssetId(asset_id); }8 r6 Y, a" `( J7 h
    .setAmount(60000000); l' z, J: n9 E5 ]$ R
    ).build(client);. l+ D  w# z/ A; b  U
    logger.info("template:"+template.toJson());* W* r! ], b8 a8 {5 W9 Z
    //useTemplateobject'sraw_transactionidtodecoderaw_transactionobtainaRawTransactionobject8 x- Y5 n5 ?- |/ J: b; I8 w
    RawTransactiondecodedTx=RawTransaction.decode(client,template.rawTransaction);% W0 K5 M* ~8 f) T' x$ @8 {! y
    logger.info("decodeTx:"+decodedTx.toJson());. ^4 @% m* L; U2 a" Y% k+ i
    //needaprivatekeyarray0 x3 X" K3 x) t  ^
    String[]privateKeys=newString[]{"08bdbd6c22856c5747c930f64d0e5d58ded17c4473910c6c0c3f94e485833a436247976253c8e29e961041ad8dfad9309744255364323163837cbef2483b4f67",+ M1 y" M8 v, O
    "40c821f736f60805ad59b1fea158762fa6355e258601dfb49dda6f672092ae5adf072d5cab2ceaaa0d68dd3fe7fa04869d95afed8c20069f446a338576901e1b",
6 B1 q6 e- N- J, C7 E9 U. Z    "08bdbd6c22856c5747c930f64d0e5d58ded17c4473910c6c0c3f94e485833a436247976253c8e29e961041ad8dfad9309744255364323163837cbef2483b4f67"};
0 ^6 i$ ?' h/ Z. \$ J) j    logger.info("privatekey1:"+privateKeys[0]);
# E. S" {; K. N    logger.info("privatekey2:"+privateKeys[1]);
2 m( `$ h! N! Y8 E* A8 `% a    //callofflinesignmethodtoobtainabasicofflinesignedtemplate
- K& G, W8 k: h+ d    Signaturessignatures=newSignaturesImpl();
3 \! |- p( \5 T7 S    TemplatebasicSigned=signatures.generateSignatures(privateKeys,template,decodedTx);
, b) O+ B& S. ]& a4 S    logger.info("basicsignedraw:"+basicSigned.toJson());
. ?  C! P8 P9 \/ [3 v    //callsigntransactionapitocalculatewholeraw_transactionid8 J* \. G- W+ |' i. ^
    //signpasswordisNoneoranotherrandomString
5 c: i+ s, K$ b& ^: B' I+ _2 \; }    Templateresult=newTransaction.SignerBuilder().sign(client,
( i# K! I& P; |7 K1 e    basicSigned,"");
; w* @6 O7 J/ `% J; w6 M1 C    logger.info("resultraw_transaction:"+result.toJson());
5 q/ J/ j( k( q: X    //successtosubmittransaction
9 w. o& b- P0 C; }3 n    }
9 V3 U5 S  D+ V1 H4 K2 T; z' V* n4 S8 d5 O! Q
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

yuan081608 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    2