Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

比原链Java版本离线签名

yuan081608
162 0 0
    tx_signer# f8 z$ Z/ |5 Y7 T5 m3 b# ?# z
    Javaimplementationofsigningtransactionofflinetobytomd." ~5 ^. `5 |3 e5 D
    Pre
( z- b& s) v8 u) c    Getthesourcecode
6 n5 C, u/ _$ U% D3 f& n    $gitclonehttps://github.com/Bytom/bytom.git$GOPATH/src/github.com/bytom1 c# ^8 e* M" m
    gitcheckout
; u7 `( h% r" w3 a& m/ t0 {" N    $gitcheckoutdev
( _+ l+ A/ }  b8 h" a8 h    Whyneeddevbranch?Becauseyoucouldcalldecodetransactionapifromdevbranchandobtaintx_idandsomeinputsids.
1 S0 l" T" G( F    Build
' c, S9 _2 y7 K8 L, y  h+ D8 W    $cd$GOPATH/src/github.com/bytom
6 [. i7 U% O% D. c, b) p: p    $makebytomd#buildbytomd
6 l$ {1 v4 k" f- V) _- a2 N    $makebytomcli#buildbytomcli  E& ]! e; |$ _9 X. ~
    Whensuccessfullybuildingtheproject,thebytomandbytomclibinaryshouldbepresentincmd/bytomdandcmd/bytomclidirectory,respectively.
  L; B8 p7 Z9 M/ S! ]% P: o1 A    Initialize
# M# N# `* g, n4 J( L* g  J    Firstofall,initializethenode:+ m) q2 X# v; z. u; h1 d' ?% H( ]
    $cd./cmd/bytomd- s/ L4 l. I$ `5 A
    $./bytomdinit--chain_idsolonet; ^( o. n  y( H+ C2 \9 x5 K
    launch! D6 J' ], ?0 R# o/ ]! {
    $./bytomdnode--mining3 f: e6 S* x/ @, @% i; h
    Usage' w! E; d( Z" i1 y- i1 z1 J. a
    Buildjar
# u! O2 O0 a  Y+ d
! n2 m) h5 r% [( B9 x7 T    firstgetsourcecode1 f, ^0 P+ ]& l) D$ J1 x$ Z
    gitclonehttps://github.com/successli/tx_signer.git
4 n! _3 f5 d7 L9 ]4 H    getjarpackage  q. _( N7 }8 O7 J& U" r4 W
    $mvnassembly:assembly-Dmaven.test.skip=true
/ x" [* n( J  P( C, I: u- T2 _    Youcangetajarwithdependencies,andyoucanuseitinyourproject." ?/ V8 b2 s0 K6 Y# ~3 W
% s9 |4 z5 _- R' A+ R
    Testcases
$ j9 T1 F; W+ u8 U9 Q* A    Need3Parameters:
' Y6 k% a) z2 H1 O    PrivateKeysArray" N: j0 r" g$ O2 ]
    TemplateObject( l2 y+ N. U5 R& P
    AftercallbuildtransactionapireturnaTemplatejsonobject.buildtransactionapi
! y$ `8 g/ Z' L0 j+ H: E2 \    usebytomjavasdkreturnaTemplateobject.5 _5 z2 z, p7 V3 y! }6 t
    RawTransaction" o% L! {) |$ f! ]& [/ b$ z. ^: D# ]
    calldecoderaw-transactionapifromdevbranch.decoderaw-transactionapi3 m( @( o+ y3 r& j8 x
    Callmethod:; N  ]$ [( _" ?
    //returnaTemplateobjectsignedofflinebasically.- `. b/ d3 t; x3 w' j" |) R, a; t
    Templateresult=signatures.generateSignatures(privates,template,rawTransaction);0 u2 [# l& ]! V4 t- o6 u. q( J
    //useresult'sraw_transactioncallsigntransactionapitobuildanotherdatabutnotneedpasswordorprivatekey.3 L1 f0 a9 M+ X7 A5 }# a4 E2 y
    Single-keyExample:
7 q6 b0 e* w( o& k+ i" `" i& b  j$ `    @Test
% f! A- `0 g2 y: I; w    //使用SDK来构造Template对象参数,单签
# J; @1 Q8 k5 [    publicvoidtestSignSingleKey()throwsBytomException{: N+ r: U  Y  K: G* I/ F
    Clientclient=Client.generateClient();
* N* |& }1 L$ G" k% H+ A' Z    Stringasset_id="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
, m( T! |! N( W5 W    Stringaddress="sm1qvyus3s5d7jv782syuqe3qrh65fx23lgpzf33em";( S+ ]6 d' m$ d# S
    //buildtransactionobtainaTemplateobject
/ ?8 S4 Q6 ^# F. B    Templatetemplate=newTransaction.Builder()' U9 L- q% a4 [3 \+ c  ?
    .addAction(7 S. W  x* a- t: g& F
    newTransaction.Action.SpendFromAccount()
6 @- P9 _$ U, Y# \( x4 w    .setAccountId("0G0NLBNU00A02")
6 j, m: r4 D3 }) E# s6 i    .setAssetId(asset_id)
0 ~4 U& Z* u8 p8 T( y, z4 G, ~4 [    .setAmount(40000000). ?* I1 r& |7 U6 D- x- T
    )  j" M4 W- w) o. N5 ^( ]0 j
    .addAction(0 I8 I) K; T+ Y
    newTransaction.Action.SpendFromAccount()% z: ~* x5 e' p) g
    .setAccountId("0G0NLBNU00A02")5 A' O8 T/ q- e1 @5 r8 i' f
    .setAssetId(asset_id)
% b. }- x3 h3 H7 x" S$ e    .setAmount(300000000)
, c* k" S2 r3 L2 C: ?    )% f. G; U1 f' U0 }$ r; y* k
    .addAction(' u8 k; Z- }. R! T
    newTransaction.Action.ControlWithAddress()" L( [1 @. e) v# I
    .setAddress(address)
9 C! F5 |8 M& t. F2 b    .setAssetId(asset_id)2 w* ?+ m2 b! Z: E& {
    .setAmount(30000000)  `; J# o+ t# D* O  z- C/ C
    ).build(client);/ ^" j0 _" f+ k- G$ K
    logger.info("template:"+template.toJson());/ C9 h( u9 ?0 s; q& `3 |* p
    //useTemplateobject'sraw_transactionidtodecoderaw_transactionobtainaRawTransactionobject
* G" ~4 f6 z+ X& e8 ~1 b& _: R    RawTransactiondecodedTx=RawTransaction.decode(client,template.rawTransaction);9 ?0 v5 r8 E5 G. Q0 S
    logger.info("decodeTx:"+decodedTx.toJson());
! m3 J: L4 a9 }9 W    //needaprivatekeyarray$ X1 D, }. i$ N3 W9 w
    String[]privateKeys=newString[]{"10fdbc41a4d3b8e5a0f50dd3905c1660e7476d4db3dbd9454fa4347500a633531c487e8174ffc0cfa76c3be6833111a9b8cd94446e37a76ee18bb21a7d6ea66b"};. Y! D, e; U  A/ j
    logger.info("privatekey:"+privateKeys[0]);
: I, `- `8 X# Z* C2 b- ^! l6 n, Y6 P    //callofflinesignmethodtoobtainabasicofflinesignedtemplate
% n; D' |4 j$ D7 }: B$ z( y6 ?    Signaturessignatures=newSignaturesImpl();# C9 C! \$ \+ f
    TemplatebasicSigned=signatures.generateSignatures(privateKeys,template,decodedTx);; H1 V3 ]' X0 ^5 A( a1 u; R! w
    logger.info("basicsignedraw:"+basicSigned.toJson());
* I1 ?! w2 }! ~! G9 q. N    //callsigntransactionapitocalculatewholeraw_transactionid
# r) ?: T8 B/ ^+ r" c: s    //signpasswordisNoneoranotherrandomString) O- E. Z& r. Z4 m
    Templateresult=newTransaction.SignerBuilder().sign(client,
/ M3 q" F3 S8 N9 H' J& Y- Z0 q    basicSigned,"");" M" }, P( ]6 r2 Y2 s( v
    logger.info("resultraw_transaction:"+result.toJson());
( f0 v) v( Y. p6 ~    //successtosubmittransaction& ?7 ?6 s4 N& K& V. m$ x) I; _
    }
/ o6 ?1 H* V  I/ \    Multi-keysExample:
( o8 h! L6 {( \$ I0 ^( m  @    Needanaccounthastwoormorekeys.3 R* T, D: [0 |. W# m. V
    @Test; S- U4 E' f8 C; I6 \' E
    //使用SDK来构造Template对象参数,多签1 t& F8 {0 {( I% w) U" ]
    publicvoidtestSignMultiKeys()throwsBytomException{8 C0 A; D; J' S" P" @0 }
    Clientclient=Client.generateClient();
, E0 K+ n) v5 }- e. [! @2 `    Stringasset_id="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";5 X  S: @0 a+ b$ c+ S
    Stringaddress="sm1qvyus3s5d7jv782syuqe3qrh65fx23lgpzf33em";
# o1 C  k9 D, ?/ Q    //buildtransactionobtainaTemplateobject
8 s5 ~6 z7 g3 T% J9 c. A    //account0G1RPP6OG0A06hastwokeys
, r9 E7 E/ w) b+ c1 W8 f% F    Templatetemplate=newTransaction.Builder()
: x) Q* ^3 W1 d5 w/ j9 q    .setTtl(10)" z: ?6 b, p/ l6 n( P! J- l; e- P
    .addAction(. h" x# R& z3 {
    newTransaction.Action.SpendFromAccount()
8 M/ a+ f# ~! d    .setAccountId("0G1RPP6OG0A06")
" u% k4 T  G/ C4 v' z) r  k    .setAssetId(asset_id)
$ g4 I- V+ e) B% L" J; i    .setAmount(40000000)
$ \: w- q5 A6 n7 ^$ c8 L+ `    )7 R& H! o- I+ n  h4 x. T/ e
    .addAction(( h( @8 Z9 o4 ~" P! f% |3 q9 a) H4 [
    newTransaction.Action.SpendFromAccount(), [: R5 {( @0 ]$ L, M( W. B
    .setAccountId("0G1RPP6OG0A06")
  g* v8 H& ^  ]    .setAssetId(asset_id)
  g0 k- P5 p/ C3 a# M# p+ m" Q$ g    .setAmount(300000000)
1 E$ i7 q% v) R$ i( k1 f- ?    )
+ I5 w3 e; m% D+ ~5 W    .addAction(
; |% H) |  {( ?$ y) I  I    newTransaction.Action.ControlWithAddress()$ F% D" d6 d" f7 d6 f4 z
    .setAddress(address)4 X* A1 @& o- I/ E. z: y
    .setAssetId(asset_id)
9 b" `/ p6 v8 c& [$ A# I0 }4 B( {. `    .setAmount(30000000)" E" y  A: X$ t8 c7 k
    ).build(client);# y2 o7 E6 m& j9 C& Z1 A
    logger.info("template:"+template.toJson());$ r, K( @/ B6 U$ S3 k
    //useTemplateobject'sraw_transactionidtodecoderaw_transactionobtainaRawTransactionobject" P$ a0 Z: C4 e$ L; \* r/ j: M! v
    RawTransactiondecodedTx=RawTransaction.decode(client,template.rawTransaction);
9 x* x& |- h- L( _0 x1 U; L* F    logger.info("decodeTx:"+decodedTx.toJson());
, i! g. R7 U5 s    //needaprivatekeyarray
. c2 h5 w: D5 `  l. \7 t    String[]privateKeys=newString[]{"08bdbd6c22856c5747c930f64d0e5d58ded17c4473910c6c0c3f94e485833a436247976253c8e29e961041ad8dfad9309744255364323163837cbef2483b4f67",5 x5 q6 \3 t' H3 k% t. i+ j) p
    "40c821f736f60805ad59b1fea158762fa6355e258601dfb49dda6f672092ae5adf072d5cab2ceaaa0d68dd3fe7fa04869d95afed8c20069f446a338576901e1b"};7 H6 i1 u4 g) [& l% A
    logger.info("privatekey1:"+privateKeys[0]);2 ?. Q: b/ E3 S- c' A8 i
    logger.info("privatekey2:"+privateKeys[1]);- c8 X) B1 ]$ P* H# s  A* x
    //callofflinesignmethodtoobtainabasicofflinesignedtemplate% I$ n0 A/ K, D+ |" X
    Signaturessignatures=newSignaturesImpl();
  Q4 B; V3 A) O( k* j) M' L    TemplatebasicSigned=signatures.generateSignatures(privateKeys,template,decodedTx);! R+ U# E# e! l2 x% r9 f& g
    logger.info("basicsignedraw:"+basicSigned.toJson());
. u) w- N7 t6 R1 |' H' `- c# b    //callsigntransactionapitocalculatewholeraw_transactionid/ m0 k" d( v% \" \8 {5 B( ]. L
    //signpasswordisNoneoranotherrandomString% A# h8 T5 b" u
    Templateresult=newTransaction.SignerBuilder().sign(client,2 T* V+ {8 x) |: m$ P* Q! q
    basicSigned,"");
7 ~( V. p5 B( [5 j" h# Z8 D7 S    logger.info("resultraw_transaction:"+result.toJson());+ g. _* W9 F( f7 @
    //successtosubmittransaction
2 J5 Q+ M( a3 w' ]& z4 x    }
! Y; C" b$ I) R' ?7 l( o    Multi-keysandMulti-inputsExample:) K: k7 O$ W! S$ a$ J% a- ~$ T
    @Test
! q) d5 V2 n# l; @    //使用SDK来构造Template对象参数,多签,多输入
! V8 K- Y5 u& ], z, a6 b7 m    publicvoidtestSignMultiKeysMultiInputs()throwsBytomException{
3 _( U7 G) g. |& B2 \8 p# C& H    Clientclient=Client.generateClient();
$ H: I8 d( X& F+ p, X' L: S    Stringasset_id="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";* Q4 i5 B) q$ S
    Stringaddress="sm1qvyus3s5d7jv782syuqe3qrh65fx23lgpzf33em";% a8 B7 ]# J" x6 u
    //buildtransactionobtainaTemplateobject
7 x$ J$ R6 {7 R: V$ i8 _    Templatetemplate=newTransaction.Builder()
# [0 `6 O! ]- f    .setTtl(10)4 P9 _; _9 R' T) s' u% {/ Q* u7 Y
    //1input
4 g; y: w) w1 b( I    .addAction(
- d" q! X( X- J% C  Y1 P; V) t    newTransaction.Action.SpendFromAccount()
$ @0 _, x+ m! D" _% o7 Z) |    .setAccountId("0G1RPP6OG0A06")//Multi-keysaccount8 w# b+ E& X& M- H( n. T
    .setAssetId(asset_id)0 M  y& e' }9 G8 d# s
    .setAmount(40000000): p. F. @; D) s) j0 q3 {( t
    ): n/ [  x1 `8 m, V
    .addAction(
* z, P3 ^2 ]4 w    newTransaction.Action.SpendFromAccount()/ ?# y. w# i2 |
    .setAccountId("0G1RPP6OG0A06")+ S$ ^  K5 G$ Y: n
    .setAssetId(asset_id)( l. q! `' M" n( D
    .setAmount(300000000)
7 o; C9 d9 y/ K5 Y9 J    )//2input
4 x8 `! H5 A  {- f3 J    .addAction(5 u/ p8 c- u; h) U
    newTransaction.Action.SpendFromAccount()& m% N5 O7 w; e( Z7 y
    .setAccountId("0G1Q6V1P00A02")//Multi-keysaccount
+ r2 H% P: I4 k$ M( F/ C    .setAssetId(asset_id)
7 p; j- C: f0 |; l' @    .setAmount(40000000)
0 W9 S7 ]) k+ t* w5 {: s% @+ b# @    )" E: V$ |# D* [7 b4 V0 I3 b9 c& m: d
    .addAction(
* U  G) p! g) `1 \- j, E    newTransaction.Action.SpendFromAccount()/ _' M# R5 a" \0 W2 q: _' u
    .setAccountId("0G1Q6V1P00A02")
" d+ C- A) }! z* s    .setAssetId(asset_id)
7 ~6 d  A# _. m3 H    .setAmount(300000000)+ G3 Z8 M" G- m# m% M$ t' c
    )5 p7 |; a  [: a, d
    .addAction(
7 v. o' e* h  j! U( x) g    newTransaction.Action.ControlWithAddress()* X5 T& x  p% `
    .setAddress(address)
9 f+ V( G& y' C) P2 y    .setAssetId(asset_id)
  A  _4 M) k0 }% n: W    .setAmount(60000000)2 u  i: ?6 c" o9 P
    ).build(client);
; L9 P1 |2 E0 e$ C* x* ]    logger.info("template:"+template.toJson());" g7 Z7 a0 F1 h1 Y
    //useTemplateobject'sraw_transactionidtodecoderaw_transactionobtainaRawTransactionobject
$ ^. S6 e& S  L3 @' F    RawTransactiondecodedTx=RawTransaction.decode(client,template.rawTransaction);
% e# D8 K0 F: Z1 k7 |8 q    logger.info("decodeTx:"+decodedTx.toJson());
( r1 w4 K% I& O) l    //needaprivatekeyarray) e# N/ i5 G8 b) W8 y3 T+ ]
    String[]privateKeys=newString[]{"08bdbd6c22856c5747c930f64d0e5d58ded17c4473910c6c0c3f94e485833a436247976253c8e29e961041ad8dfad9309744255364323163837cbef2483b4f67",6 j# J9 m2 Q  {
    "40c821f736f60805ad59b1fea158762fa6355e258601dfb49dda6f672092ae5adf072d5cab2ceaaa0d68dd3fe7fa04869d95afed8c20069f446a338576901e1b",
/ h1 A8 D* ^# g( b    "08bdbd6c22856c5747c930f64d0e5d58ded17c4473910c6c0c3f94e485833a436247976253c8e29e961041ad8dfad9309744255364323163837cbef2483b4f67"};$ n+ @: K3 d8 o% y6 n4 Z
    logger.info("privatekey1:"+privateKeys[0]);
* j$ j4 N/ `2 i8 ^9 b. l    logger.info("privatekey2:"+privateKeys[1]);: N2 p2 B- r" c
    //callofflinesignmethodtoobtainabasicofflinesignedtemplate9 M. Q. O, d; v( }0 i( Y  {/ r, A
    Signaturessignatures=newSignaturesImpl();
; I8 ~4 _. a% B2 D! I    TemplatebasicSigned=signatures.generateSignatures(privateKeys,template,decodedTx);3 }. }" x3 A8 O! f  W/ |2 s: o
    logger.info("basicsignedraw:"+basicSigned.toJson());
& J/ S) I. ?- A1 y! H7 P    //callsigntransactionapitocalculatewholeraw_transactionid  s1 X6 x& v  R& ^4 B
    //signpasswordisNoneoranotherrandomString
% K" z+ f7 p& M5 L4 R  ~    Templateresult=newTransaction.SignerBuilder().sign(client,- `  W% ]8 v9 i8 F0 L1 p, F1 d+ S2 o4 @
    basicSigned,"");
" y7 Q! H! c3 \) a; C4 w, o' _% N9 @    logger.info("resultraw_transaction:"+result.toJson());
) B, Y4 @+ _7 {2 O7 _    //successtosubmittransaction( m, s. V5 }: R+ |: a
    }
5 o5 T) |  R9 L$ [) n- w6 X$ v( w  ]9 k6 y: {3 Z  l0 P
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

yuan081608 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    2