Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

比原链Java版本离线签名

yuan081608
147 0 0
    tx_signer
) i& d/ ]3 S% h4 C# h    Javaimplementationofsigningtransactionofflinetobytomd.) t( @/ }) u, M& n
    Pre
8 s" N/ S' X6 I    Getthesourcecode
  G6 n$ @9 ~6 @8 L: j    $gitclonehttps://github.com/Bytom/bytom.git$GOPATH/src/github.com/bytom% V- Y8 N( v- r/ @" r
    gitcheckout
5 z1 p4 E( K# Z; q    $gitcheckoutdev1 E% L( N: D* V6 N/ G$ U' Q  m
    Whyneeddevbranch?Becauseyoucouldcalldecodetransactionapifromdevbranchandobtaintx_idandsomeinputsids.$ Q. t' j% i& Y
    Build( Y# ]2 m- A( Z0 B% l
    $cd$GOPATH/src/github.com/bytom) V+ k8 Y& x2 R, q! D; s
    $makebytomd#buildbytomd' ~" ?. e0 D: g, i8 \
    $makebytomcli#buildbytomcli
, [9 G/ Z2 {) U, s  l& S% I$ }    Whensuccessfullybuildingtheproject,thebytomandbytomclibinaryshouldbepresentincmd/bytomdandcmd/bytomclidirectory,respectively./ `4 f: @- X6 f- e) m! @  ^
    Initialize
  C& I; A! ~; U0 f9 t    Firstofall,initializethenode:
3 \! R4 y7 y9 f/ J* |6 _    $cd./cmd/bytomd* i) h0 a+ K+ n1 o) @
    $./bytomdinit--chain_idsolonet- M" }; x* H5 g) w3 v" G5 m" }2 n" ?
    launch
; g$ k( a: k( u2 t  n    $./bytomdnode--mining2 }( H' Q8 j! e8 l' \2 y5 g2 M3 m
    Usage
  X# C1 K  q3 T8 O; M5 T    Buildjar
9 v; R' j. u* @$ [7 p( k) t
! Y$ D% B% H) P+ t' }    firstgetsourcecode' e1 P; w$ Q' \. w: B# P# H  h$ I
    gitclonehttps://github.com/successli/tx_signer.git* M  |- d: j+ Y5 K& N. |4 I7 D
    getjarpackage
6 p) e: g. @# E# K# |" q( }2 A    $mvnassembly:assembly-Dmaven.test.skip=true
: e+ b% ]- T0 s* g" u    Youcangetajarwithdependencies,andyoucanuseitinyourproject.
9 V6 |" w, X% K( ^; W: }, d# V* E
, Q2 x/ G5 [) T; Q! \    Testcases. ~6 P1 n  U" K8 e/ r; F% m
    Need3Parameters:  x6 G+ [- n7 \- z5 c0 W
    PrivateKeysArray$ i- X) G+ v' k+ N: d0 Z
    TemplateObject- k* S1 B8 ?; X
    AftercallbuildtransactionapireturnaTemplatejsonobject.buildtransactionapi
+ w6 U! w7 Q1 o7 \# F6 R& T" \    usebytomjavasdkreturnaTemplateobject.
9 a4 G. [1 G3 H1 \5 O) C    RawTransaction, K5 z: P& c# A; x! {* s) B1 L
    calldecoderaw-transactionapifromdevbranch.decoderaw-transactionapi
, y5 D# J* z# m4 h    Callmethod:
  L" `- R! `" e. N/ c    //returnaTemplateobjectsignedofflinebasically.; o  I5 p9 S" _/ E
    Templateresult=signatures.generateSignatures(privates,template,rawTransaction);! Q' O" w3 J( _
    //useresult'sraw_transactioncallsigntransactionapitobuildanotherdatabutnotneedpasswordorprivatekey.7 u4 r3 C% [+ t5 f
    Single-keyExample:
' r0 i4 U; R  g1 I# ?    @Test- \) ~- b$ W. H$ C( D5 x$ \  n
    //使用SDK来构造Template对象参数,单签
) Q+ ~$ _- I* K9 X% D    publicvoidtestSignSingleKey()throwsBytomException{* M) k( R3 m& ?  s" N% Y* {
    Clientclient=Client.generateClient();
* T1 l: V) t* `3 ?6 B3 B    Stringasset_id="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";. F1 l  ?1 Z  ~0 B( s0 }
    Stringaddress="sm1qvyus3s5d7jv782syuqe3qrh65fx23lgpzf33em";
! ?0 u3 Y) F8 f( i    //buildtransactionobtainaTemplateobject2 x6 _8 I+ x8 ~$ ~+ B
    Templatetemplate=newTransaction.Builder()1 ~' U0 g9 [" }7 K% ?! n
    .addAction(# R5 ?, L- W2 F* g/ H0 K# u
    newTransaction.Action.SpendFromAccount()9 T; R0 J8 e; N3 L* s
    .setAccountId("0G0NLBNU00A02")4 Y' A$ Y4 L$ z5 O6 L4 k
    .setAssetId(asset_id)7 U" `& ]4 M, `) B/ o
    .setAmount(40000000)* @( w6 `  t6 }- d: z- {) t* V& h
    )
' M" t( B1 {. ~# X, d- j    .addAction(( i4 J2 p, S! _" g; H0 j+ i
    newTransaction.Action.SpendFromAccount()
+ ]9 _5 ^. W$ d. J; b1 n    .setAccountId("0G0NLBNU00A02")
+ F# t# }4 O2 w/ ^7 \( ?    .setAssetId(asset_id)% A; A0 W+ y8 b$ {) ~% w! H# q; n* p  h
    .setAmount(300000000)
3 h; O: c6 D5 B& w1 X: W' q    )
+ {, x2 t* t9 p( ^1 b% p    .addAction(! J2 z. R( H( m8 g1 T5 w& B
    newTransaction.Action.ControlWithAddress()
! k' ?7 `- Z4 @    .setAddress(address)
6 l! u9 a- n) ?: s    .setAssetId(asset_id)8 W$ U8 ?( @6 z# W( d, f. ]1 U
    .setAmount(30000000)6 E3 }5 o% h) {; u+ ^6 ^
    ).build(client);
' L9 G) F9 e/ ^; H9 I    logger.info("template:"+template.toJson());
& y8 r1 }4 v6 M* L' e9 j. a7 |    //useTemplateobject'sraw_transactionidtodecoderaw_transactionobtainaRawTransactionobject9 G, g7 s5 e2 ^  E8 X+ L
    RawTransactiondecodedTx=RawTransaction.decode(client,template.rawTransaction);, k4 J; i2 f( y2 I! e
    logger.info("decodeTx:"+decodedTx.toJson());
$ v! _" ^* C( x9 ], @% o8 `    //needaprivatekeyarray
  F, p; p' Z( i0 c0 a" \    String[]privateKeys=newString[]{"10fdbc41a4d3b8e5a0f50dd3905c1660e7476d4db3dbd9454fa4347500a633531c487e8174ffc0cfa76c3be6833111a9b8cd94446e37a76ee18bb21a7d6ea66b"};4 X" n1 O; E) Q3 _( u+ i, ^
    logger.info("privatekey:"+privateKeys[0]);. [: Z$ O- v. S# j' _/ x
    //callofflinesignmethodtoobtainabasicofflinesignedtemplate
7 @) `, k- G, q* @7 v0 _* M    Signaturessignatures=newSignaturesImpl();4 C% _/ L" t$ |
    TemplatebasicSigned=signatures.generateSignatures(privateKeys,template,decodedTx);
. Q! v. e0 j- h4 W    logger.info("basicsignedraw:"+basicSigned.toJson());# n+ ]  z" g8 I; }" i7 I& e
    //callsigntransactionapitocalculatewholeraw_transactionid: F; @8 f: ^8 z$ a, I! N
    //signpasswordisNoneoranotherrandomString2 G4 @! B! ^: n
    Templateresult=newTransaction.SignerBuilder().sign(client,9 s$ f0 H6 J: n: K* S7 W  l6 }/ W
    basicSigned,"");) M5 y) g2 M4 b1 p: d& b6 s( w
    logger.info("resultraw_transaction:"+result.toJson());& g! F' `5 p. ]- m. V
    //successtosubmittransaction
: }) ]4 U8 y; A3 w    }1 I2 g" p( X1 K7 e1 V$ z* t, w
    Multi-keysExample:
" }9 }, L, M0 r' M5 i* n    Needanaccounthastwoormorekeys.
4 V* b7 ?( M1 p    @Test
# E7 R1 R) w) E! d) H: E4 i    //使用SDK来构造Template对象参数,多签% i9 u9 \! `+ m, [, k9 e
    publicvoidtestSignMultiKeys()throwsBytomException{6 v) w8 M/ |) k0 }) i0 l; D5 R4 O
    Clientclient=Client.generateClient();/ f9 q/ ]8 X# j- z9 j2 n2 u- K' ]# E
    Stringasset_id="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
% ~9 V/ n1 M/ d    Stringaddress="sm1qvyus3s5d7jv782syuqe3qrh65fx23lgpzf33em";% T: V' X/ M7 [9 G1 n. Y* R. D
    //buildtransactionobtainaTemplateobject$ k& O! v0 `2 V! y
    //account0G1RPP6OG0A06hastwokeys
& g4 R$ S' c, S! K/ w+ D  o    Templatetemplate=newTransaction.Builder()
  k. E; r% R6 @: _2 K- t    .setTtl(10)7 c' b' D) Q7 J! Z; J
    .addAction(
6 g6 x! |: `1 ?9 Y% l3 u$ Y6 C    newTransaction.Action.SpendFromAccount()8 Z7 A. c2 W; r
    .setAccountId("0G1RPP6OG0A06")
0 v4 @- w& w' g; X5 O    .setAssetId(asset_id)
* ?1 s/ J- z; x- k4 w7 u7 `0 v    .setAmount(40000000)3 B; C% U' b1 {/ n% \9 @
    )8 L% d9 i  z+ N7 \( |  k
    .addAction(
0 w7 j0 a$ F( ~0 E    newTransaction.Action.SpendFromAccount()
& H) i6 W2 V2 y4 W2 Z- h    .setAccountId("0G1RPP6OG0A06")- J! C' V0 {7 v  G
    .setAssetId(asset_id)
) Q0 f+ g7 y& k( J4 J    .setAmount(300000000)
  G# K( u7 I* q" f    )
% C5 r8 n2 E0 R; x    .addAction(
1 |! B; \; W6 b/ a: J% ~( u( f* p    newTransaction.Action.ControlWithAddress()
' y( g) v5 u/ s5 I7 l    .setAddress(address)
+ g* b( v( w; f2 l8 D3 L+ W) b" m' g    .setAssetId(asset_id)
+ w4 K5 C& R) b8 A; `" r( \    .setAmount(30000000)
  ]2 P3 u  V: E  c: A5 K    ).build(client);. t2 }0 S1 _; A" k
    logger.info("template:"+template.toJson());9 m; a3 X5 `- F* R0 H) `0 f$ e/ Z$ ~# m
    //useTemplateobject'sraw_transactionidtodecoderaw_transactionobtainaRawTransactionobject
1 J! u0 ?' H% ^* c3 h- Z, o. B    RawTransactiondecodedTx=RawTransaction.decode(client,template.rawTransaction);1 x" w. }% C# w1 [' `
    logger.info("decodeTx:"+decodedTx.toJson());- Q1 R% c+ p' J
    //needaprivatekeyarray) Q% n8 ]) I0 Z; s
    String[]privateKeys=newString[]{"08bdbd6c22856c5747c930f64d0e5d58ded17c4473910c6c0c3f94e485833a436247976253c8e29e961041ad8dfad9309744255364323163837cbef2483b4f67",! p/ Y+ I3 `8 H* ]2 V
    "40c821f736f60805ad59b1fea158762fa6355e258601dfb49dda6f672092ae5adf072d5cab2ceaaa0d68dd3fe7fa04869d95afed8c20069f446a338576901e1b"};
- v" a- g) j7 x1 k- u$ V    logger.info("privatekey1:"+privateKeys[0]);4 ]4 [9 d7 u. p5 v5 P+ H1 z
    logger.info("privatekey2:"+privateKeys[1]);
9 n# F4 O  r  D/ d7 w    //callofflinesignmethodtoobtainabasicofflinesignedtemplate, P$ G& \4 b# H3 w3 n0 s9 e/ t
    Signaturessignatures=newSignaturesImpl();
+ E& I8 M  R+ J. j% J2 `    TemplatebasicSigned=signatures.generateSignatures(privateKeys,template,decodedTx);
3 g5 g6 l1 \& N& f. Q" I1 V    logger.info("basicsignedraw:"+basicSigned.toJson());
6 _1 j. `0 k! `% a    //callsigntransactionapitocalculatewholeraw_transactionid
  j. q# H# x2 ~& ]! g9 P9 z5 @% h    //signpasswordisNoneoranotherrandomString. L, |- g7 \7 r' t# I  k
    Templateresult=newTransaction.SignerBuilder().sign(client,9 O" g! |3 c1 z! \3 H  F
    basicSigned,"");3 `+ D8 w! ?3 ^) p6 n6 z$ @
    logger.info("resultraw_transaction:"+result.toJson());
# B; u. n( m" R% D: z% p  S% R    //successtosubmittransaction
# r3 I7 q) N: L  E    }
: N( J4 P5 Y& t    Multi-keysandMulti-inputsExample:
( N0 |6 g- s6 c6 M8 E% |% o    @Test5 K' }2 J) w" B" z) d8 c
    //使用SDK来构造Template对象参数,多签,多输入3 l& r, C8 m: m' ^' i4 [) d1 p
    publicvoidtestSignMultiKeysMultiInputs()throwsBytomException{
; r1 @& z: R. z2 y3 Z( w    Clientclient=Client.generateClient();
" @! U3 a% c! s) k    Stringasset_id="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";; ]3 t& y  Z6 m
    Stringaddress="sm1qvyus3s5d7jv782syuqe3qrh65fx23lgpzf33em";
% L, J6 X- V+ ~/ `$ q) f    //buildtransactionobtainaTemplateobject$ Q, I# c# C. X# K9 W) H, @# p
    Templatetemplate=newTransaction.Builder()) V; Y$ k; \; a9 F) y3 i' {
    .setTtl(10)
2 J4 G3 g1 t( G) Y% v9 N$ W# l% `    //1input5 ~) I' h0 Y5 Y" t
    .addAction(
7 j7 l0 |) Q- I. u    newTransaction.Action.SpendFromAccount()7 x8 p& Q$ l$ [. [" r
    .setAccountId("0G1RPP6OG0A06")//Multi-keysaccount  L1 t) P) o& b  p
    .setAssetId(asset_id)
% I; j- @. w1 Q( m* t+ c; B    .setAmount(40000000)
, s/ W: q0 X/ D3 Q    )2 I0 c. B9 G) i; h& _! e
    .addAction(5 i+ a6 l8 }) O4 h* i3 b5 {8 n* J% F
    newTransaction.Action.SpendFromAccount()
/ w4 J7 f. H" G2 p7 F& O    .setAccountId("0G1RPP6OG0A06")/ i/ g1 X+ @: U. h8 G' h
    .setAssetId(asset_id): V. t  M; L2 b# i9 H
    .setAmount(300000000)
9 x- ^: f3 C! }    )//2input
. D& U$ P0 @: J8 X    .addAction(" W8 R" o9 m# l& A3 u: Y& k- }- `
    newTransaction.Action.SpendFromAccount()
" p  z$ _) m0 }8 a9 f9 `" q    .setAccountId("0G1Q6V1P00A02")//Multi-keysaccount
3 [9 Z/ [# K* ?7 \. J    .setAssetId(asset_id)
4 o( Q4 m/ f* U  B  t! |    .setAmount(40000000)0 x5 b1 k1 G$ e& T
    )3 f5 k0 p3 D5 d! `
    .addAction(& E* w4 m5 O+ D! C% D
    newTransaction.Action.SpendFromAccount()
% D; B; Q4 o' p    .setAccountId("0G1Q6V1P00A02")" z/ j6 w) d( u; j( ]8 s* E* h( s
    .setAssetId(asset_id)
  v' n, {. N8 ~! P3 j    .setAmount(300000000)
; E& p3 ~1 x" x' q    )) E4 V! X: U- ~: b5 o  Z* g
    .addAction(, g( |+ D: [* [& |
    newTransaction.Action.ControlWithAddress()4 W/ \5 E5 R& j
    .setAddress(address)
; q  _; Q( P1 n: f' E! N( w    .setAssetId(asset_id)
' H* ~3 f( |) y+ r& {    .setAmount(60000000)) K- s' [& w& Q! H+ m$ H: [$ ]
    ).build(client);
3 g! v) H% a2 |0 C$ i/ O( W    logger.info("template:"+template.toJson());
. Q/ S; K& V# a7 p4 C3 I2 C    //useTemplateobject'sraw_transactionidtodecoderaw_transactionobtainaRawTransactionobject
: r! C/ v  W9 X* c; n! T9 ]    RawTransactiondecodedTx=RawTransaction.decode(client,template.rawTransaction);
3 D2 A  e1 o. I    logger.info("decodeTx:"+decodedTx.toJson());3 @. n$ j( X5 |8 q$ l  J% i
    //needaprivatekeyarray
  D, U1 x* ^; I1 `% ^" W    String[]privateKeys=newString[]{"08bdbd6c22856c5747c930f64d0e5d58ded17c4473910c6c0c3f94e485833a436247976253c8e29e961041ad8dfad9309744255364323163837cbef2483b4f67",. ?# f6 a1 A4 x- }8 W2 a( L
    "40c821f736f60805ad59b1fea158762fa6355e258601dfb49dda6f672092ae5adf072d5cab2ceaaa0d68dd3fe7fa04869d95afed8c20069f446a338576901e1b",
/ k2 _; |' x, `7 x  G    "08bdbd6c22856c5747c930f64d0e5d58ded17c4473910c6c0c3f94e485833a436247976253c8e29e961041ad8dfad9309744255364323163837cbef2483b4f67"};  [% s9 M- j  h4 \, A
    logger.info("privatekey1:"+privateKeys[0]);
  _. E, t9 y& h4 m6 P, ^    logger.info("privatekey2:"+privateKeys[1]);# J4 I9 d3 g* }: S
    //callofflinesignmethodtoobtainabasicofflinesignedtemplate
4 l  L# Y, A; R4 K9 T4 z    Signaturessignatures=newSignaturesImpl();
# e5 s6 t0 S2 b$ A( w  g    TemplatebasicSigned=signatures.generateSignatures(privateKeys,template,decodedTx);
5 k! c, [% r, V; p    logger.info("basicsignedraw:"+basicSigned.toJson());
. a! f% |' ?: X5 N+ w% k    //callsigntransactionapitocalculatewholeraw_transactionid5 F6 b+ }3 }4 z5 h& N6 G
    //signpasswordisNoneoranotherrandomString; }5 t$ |' _; i+ D" w; {
    Templateresult=newTransaction.SignerBuilder().sign(client,
* B+ i% O, C& C% o2 c    basicSigned,"");) e/ h8 B9 V5 c* H/ P9 G6 G
    logger.info("resultraw_transaction:"+result.toJson());
4 q4 d$ {% @  _- r9 B2 Q9 `% a9 L    //successtosubmittransaction* g" v! l/ N! p3 L$ ^
    }
" _( c' ]. g6 K& E
6 L8 P. i" c4 S" [
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

yuan081608 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    2