Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

比原链Java版本离线签名

yuan081608
114 0 0
    tx_signer! t' f& K: Z: {0 R
    Javaimplementationofsigningtransactionofflinetobytomd.1 [% E/ N" O4 T2 t: q
    Pre
& [+ m. F* x$ W0 t- d: X    Getthesourcecode
7 f7 A$ H2 N+ d2 b6 t    $gitclonehttps://github.com/Bytom/bytom.git$GOPATH/src/github.com/bytom
9 z! D& J# |( B/ M    gitcheckout
5 q. s  y3 t4 P    $gitcheckoutdev
6 s4 m( w' j! l7 w    Whyneeddevbranch?Becauseyoucouldcalldecodetransactionapifromdevbranchandobtaintx_idandsomeinputsids.
0 I. G! ^9 z& Y    Build
1 H0 [; f4 D1 p8 D: z    $cd$GOPATH/src/github.com/bytom4 `& ~/ P( w9 w5 a3 P, F
    $makebytomd#buildbytomd4 T: ?! o* Z$ D) J- q& H' i
    $makebytomcli#buildbytomcli1 g$ Y7 K& a/ x6 x9 O$ l
    Whensuccessfullybuildingtheproject,thebytomandbytomclibinaryshouldbepresentincmd/bytomdandcmd/bytomclidirectory,respectively.
4 m9 R* a& ^3 u8 p5 w    Initialize
1 m1 }9 j5 r+ [- L' ]& \# P- Y    Firstofall,initializethenode:
# s. m$ U: F- o6 A) G& H3 J    $cd./cmd/bytomd. O: S2 O" I5 f, e1 u
    $./bytomdinit--chain_idsolonet
4 ]1 ?* v/ F9 f2 {# W& m    launch  @0 G. `  \- T# l# u
    $./bytomdnode--mining: f& P: q  r4 `, N) Y. d
    Usage
4 D3 {: W. l0 g2 J6 F8 o! v/ O    Buildjar
4 D6 g8 B. a0 ?  U! Z( m2 K0 T
    firstgetsourcecode4 B% }9 }# _# k) P
    gitclonehttps://github.com/successli/tx_signer.git
3 B0 V' n. I. O3 L4 ]) d: p" U    getjarpackage8 N% K: \9 E! k; Y* N
    $mvnassembly:assembly-Dmaven.test.skip=true
5 U' t; }3 S, x    Youcangetajarwithdependencies,andyoucanuseitinyourproject., k% L1 x7 d2 d* z- y" a: c4 O/ k8 A
  w. d5 ^1 C7 t
    Testcases
$ I; c: I. y; n3 V# p2 {2 Y    Need3Parameters:5 C1 s- M* B5 j/ e6 c' D
    PrivateKeysArray
" u2 t" I+ v; L. y    TemplateObject
) @: x6 c( k! {0 H. @& W: f    AftercallbuildtransactionapireturnaTemplatejsonobject.buildtransactionapi
& p' Z/ L2 |# v* p: e    usebytomjavasdkreturnaTemplateobject.* z7 h# q! k2 x8 Y
    RawTransaction
- Q$ w7 k3 D4 l+ Z$ B# n! b% H    calldecoderaw-transactionapifromdevbranch.decoderaw-transactionapi
' b! l$ q! _/ r; ~" m& P    Callmethod:4 c' I, f2 K& a" d. f9 [
    //returnaTemplateobjectsignedofflinebasically.3 K5 l# ~% ^: C: |4 T' y5 K7 I3 V
    Templateresult=signatures.generateSignatures(privates,template,rawTransaction);
" L8 Z$ X: U) ]    //useresult'sraw_transactioncallsigntransactionapitobuildanotherdatabutnotneedpasswordorprivatekey.
( g, y* ?* W# G    Single-keyExample:! C3 ^" Q4 ?9 }8 e
    @Test
7 E" U4 Z' }0 t% `6 B( u1 s  y    //使用SDK来构造Template对象参数,单签
: V; u; ]" n" }* [% s    publicvoidtestSignSingleKey()throwsBytomException{: f8 f5 J  X. B9 W1 R+ y6 M* n5 R
    Clientclient=Client.generateClient();3 z" U  e/ t% ~) \
    Stringasset_id="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
8 `* M2 K! l: u    Stringaddress="sm1qvyus3s5d7jv782syuqe3qrh65fx23lgpzf33em";+ Y! h- l* j; J5 ]
    //buildtransactionobtainaTemplateobject' f  {! {# Y! y4 n+ y
    Templatetemplate=newTransaction.Builder()$ E/ I6 r6 j/ N0 u6 W
    .addAction(
1 x1 f% D" D6 O$ D: Q' Y+ _+ e9 c. P" \    newTransaction.Action.SpendFromAccount()2 T( }% v0 \8 B3 S8 q( b
    .setAccountId("0G0NLBNU00A02")
8 r/ J8 k8 n: p+ a( c" j# Y0 W* Y    .setAssetId(asset_id)
, j. \# B$ F( P* H    .setAmount(40000000)
0 H4 Y% }4 V! {9 M) B    )* S" [  p6 R0 h6 e3 n5 b+ a2 s
    .addAction(
2 X+ x# v# I: ]* _1 n* g    newTransaction.Action.SpendFromAccount()
* ~% C: j/ C& y% p$ E    .setAccountId("0G0NLBNU00A02")
9 }2 J. i  J  a) u( N& `$ X. f- {" J% p    .setAssetId(asset_id)
5 s' M+ @1 o/ O! Y  X3 [6 f" ^    .setAmount(300000000)
2 j9 E$ c" R5 N% c) @+ e    )
7 z; T, l+ T2 v+ d8 y1 e. S    .addAction(
/ J' [( O9 ~1 o" \    newTransaction.Action.ControlWithAddress()  a# }# `- q# e
    .setAddress(address)8 J. g. d- A: `3 g) n( C, |6 Y8 @
    .setAssetId(asset_id)
' U; l3 ]# f' v: L/ A+ ]    .setAmount(30000000)& D/ k+ {5 @- V9 V$ M
    ).build(client);
  |, H& m% h: [) w# h0 }9 p% D    logger.info("template:"+template.toJson());7 L1 g" x, h( c! E4 X
    //useTemplateobject'sraw_transactionidtodecoderaw_transactionobtainaRawTransactionobject4 g; b6 c* E' L+ v7 T
    RawTransactiondecodedTx=RawTransaction.decode(client,template.rawTransaction);1 L/ i7 \" l; b' v) A1 U
    logger.info("decodeTx:"+decodedTx.toJson());
' w- c9 f+ {/ v3 r    //needaprivatekeyarray
$ {7 p! j7 `# B6 S, ]' H* X% `; u    String[]privateKeys=newString[]{"10fdbc41a4d3b8e5a0f50dd3905c1660e7476d4db3dbd9454fa4347500a633531c487e8174ffc0cfa76c3be6833111a9b8cd94446e37a76ee18bb21a7d6ea66b"};
/ O8 c% K  B) a. {& M    logger.info("privatekey:"+privateKeys[0]);0 U7 c& G9 T. @$ p8 k7 `+ N
    //callofflinesignmethodtoobtainabasicofflinesignedtemplate
; f" o9 _" i% [1 B6 Z    Signaturessignatures=newSignaturesImpl();- q7 u% ~" r) T9 i4 d
    TemplatebasicSigned=signatures.generateSignatures(privateKeys,template,decodedTx);) [' }( [4 u( A: B1 F( P" |: e
    logger.info("basicsignedraw:"+basicSigned.toJson());1 n% [. |  M& H' B# s
    //callsigntransactionapitocalculatewholeraw_transactionid2 v* i  n' s: B  p( `/ N/ u
    //signpasswordisNoneoranotherrandomString
2 a8 h  t+ N" H; ?7 }/ k    Templateresult=newTransaction.SignerBuilder().sign(client," R: k7 N, {' `/ G, p* l$ h5 b* @% A
    basicSigned,"");
1 h) Q. j2 B4 r8 _0 a    logger.info("resultraw_transaction:"+result.toJson());
1 X) i/ l' [9 d) ^0 W8 H0 G    //successtosubmittransaction
8 ?& G* a8 D: M, o+ p    }# _# J4 G6 X& |: E9 B* t! l% J
    Multi-keysExample:
6 E0 }% i( B+ n' n! P+ t    Needanaccounthastwoormorekeys.! l+ F& y* N& I; J) f- F
    @Test# H; ]/ w. L; j0 A3 C1 B: @$ M4 a
    //使用SDK来构造Template对象参数,多签
# c( }2 f. y' n1 o& P4 r    publicvoidtestSignMultiKeys()throwsBytomException{
  v: v# j: n0 E. S+ A    Clientclient=Client.generateClient();
7 F7 I7 {1 M% D+ {! `    Stringasset_id="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";7 J2 V- o" w) ^! z6 x: {
    Stringaddress="sm1qvyus3s5d7jv782syuqe3qrh65fx23lgpzf33em";& P" c& c- Q+ p& F. f) b8 @
    //buildtransactionobtainaTemplateobject5 M2 V: H" R. B/ J6 u
    //account0G1RPP6OG0A06hastwokeys
8 \4 s3 v) S4 \0 Z, {$ Q! D. y    Templatetemplate=newTransaction.Builder()8 Q) k1 @% J6 J$ |" s8 X
    .setTtl(10)
- `) V/ z! V# Z9 J! ^( E! R    .addAction(! @& f) z/ G- U! n7 [+ H4 a
    newTransaction.Action.SpendFromAccount()' `' ?/ Z# x' V
    .setAccountId("0G1RPP6OG0A06")9 {) x: p& d9 c& _1 w- c0 @) y) C
    .setAssetId(asset_id)$ a6 j) C& C8 L$ R3 C9 b$ P9 t
    .setAmount(40000000). u) T& B& R, e& F6 t
    )) P; e3 W7 H+ Y. K' ?
    .addAction(( A% v1 q' O0 F- w! N
    newTransaction.Action.SpendFromAccount()( r. R" u% X+ E( G. _. E; M& h
    .setAccountId("0G1RPP6OG0A06")
$ g+ @+ d  ]# r' s    .setAssetId(asset_id)
" r. k) e" N2 A7 G' y6 s0 X    .setAmount(300000000)
0 u4 `3 u) }# e, D: _3 Z5 E    )
+ U* B- k+ ~; a3 D4 L9 H  d% c, C: Y  O    .addAction(8 T/ t% y$ {  I% f: e) P, _7 s5 x
    newTransaction.Action.ControlWithAddress()2 Q% S; f. ^9 t+ f- W  N
    .setAddress(address)
% d+ L! J0 U+ D- L    .setAssetId(asset_id)
; y' B6 u% m2 p: R    .setAmount(30000000)" a  K3 e3 L+ Z  q$ u' @
    ).build(client);
" b: p7 H8 `) j    logger.info("template:"+template.toJson());
8 m3 h/ t4 g  e& q    //useTemplateobject'sraw_transactionidtodecoderaw_transactionobtainaRawTransactionobject
. i7 g8 i# l! e7 _, ~  a    RawTransactiondecodedTx=RawTransaction.decode(client,template.rawTransaction);
. H0 r) E9 g9 A! c    logger.info("decodeTx:"+decodedTx.toJson());
. ^  Z( ~1 I- |2 _2 x9 f/ b/ |    //needaprivatekeyarray9 r9 u$ ?; V3 _9 U9 H9 P
    String[]privateKeys=newString[]{"08bdbd6c22856c5747c930f64d0e5d58ded17c4473910c6c0c3f94e485833a436247976253c8e29e961041ad8dfad9309744255364323163837cbef2483b4f67",
( q7 C; Y( J0 ?: ?1 H! Y) M1 h    "40c821f736f60805ad59b1fea158762fa6355e258601dfb49dda6f672092ae5adf072d5cab2ceaaa0d68dd3fe7fa04869d95afed8c20069f446a338576901e1b"};
* X+ Y( c" |. A" z, o3 g    logger.info("privatekey1:"+privateKeys[0]);
" f5 h/ M, S  i7 a$ W    logger.info("privatekey2:"+privateKeys[1]);
; U, Q+ |5 \! }; ~7 J    //callofflinesignmethodtoobtainabasicofflinesignedtemplate9 Q3 ^* K; D; u! C0 e$ z, k* h3 M
    Signaturessignatures=newSignaturesImpl();5 f  g  B- d( w" i
    TemplatebasicSigned=signatures.generateSignatures(privateKeys,template,decodedTx);
/ g& w. K0 _5 `1 Z# U    logger.info("basicsignedraw:"+basicSigned.toJson());) r; w' ^/ u+ K. y; q, N; ~
    //callsigntransactionapitocalculatewholeraw_transactionid
. y5 t% k0 y$ \! d; x3 u# k( N/ s$ ^' p    //signpasswordisNoneoranotherrandomString4 B. j1 {9 M% t1 D0 s8 j
    Templateresult=newTransaction.SignerBuilder().sign(client,
7 ]$ R1 j# S& s7 H% f: `/ D    basicSigned,"");
2 f' e! B" ]& Y: O& C8 c* X& I    logger.info("resultraw_transaction:"+result.toJson());! R; h/ ~8 O; {. f
    //successtosubmittransaction- O5 c/ o5 ~& C( L, l. R; F/ x6 p2 P
    }
, G( J# q# L5 y$ ^5 e4 F    Multi-keysandMulti-inputsExample:
! @& H6 }( ?4 w+ g8 P    @Test1 N. @0 E3 b% F3 p8 I& Y' w
    //使用SDK来构造Template对象参数,多签,多输入. P+ p# e8 w2 d, d, }3 o
    publicvoidtestSignMultiKeysMultiInputs()throwsBytomException{
; s, u8 l0 l5 v; y! ~& Z' l# a    Clientclient=Client.generateClient();" g+ {+ Z# k7 L% O+ |) W  a( w
    Stringasset_id="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";; d5 t7 M# J5 d) l  \
    Stringaddress="sm1qvyus3s5d7jv782syuqe3qrh65fx23lgpzf33em";# v. v+ z( F$ f+ \
    //buildtransactionobtainaTemplateobject
8 M2 z7 r9 \9 ~! y; f( b    Templatetemplate=newTransaction.Builder()9 A1 Q% s( Q5 O+ @  }
    .setTtl(10); k8 G; x& C1 m4 k2 g. O% @2 F( K
    //1input/ e$ v+ P/ o4 m3 ]# W: ]( ^$ T! W
    .addAction(2 ^& d0 O" O4 [" R0 L
    newTransaction.Action.SpendFromAccount()
* S. e+ `5 D/ p  j    .setAccountId("0G1RPP6OG0A06")//Multi-keysaccount
5 G7 H" I1 Q/ T9 G' N# P    .setAssetId(asset_id)6 N3 }* b. ~6 f+ Y: H
    .setAmount(40000000)
* z  ~* ?  m+ g    )8 E# b$ ~9 Y% f
    .addAction($ y/ D9 H7 V, P% `
    newTransaction.Action.SpendFromAccount()' O- D2 @3 T( {, {8 N: r
    .setAccountId("0G1RPP6OG0A06")
2 H  I" j# R; z$ X$ z( c* U    .setAssetId(asset_id)
' O& A( L! U$ {' a2 H/ e$ j    .setAmount(300000000)( C& G# U4 h+ K1 A+ Y, s4 Q& C
    )//2input- \* X1 L7 {8 U; s/ N
    .addAction(  p/ H, N' ]  c' E% r
    newTransaction.Action.SpendFromAccount()  w* R3 ^9 y4 v
    .setAccountId("0G1Q6V1P00A02")//Multi-keysaccount
) K/ E$ }: u( k, f4 b3 t    .setAssetId(asset_id)! F8 \! p' \( s( B! ~
    .setAmount(40000000)0 _* {2 u8 Y  f* d4 |
    )9 L! Y6 }! J& D8 P8 `; x
    .addAction($ v' K$ K: ^2 G4 [6 G
    newTransaction.Action.SpendFromAccount()
8 ^; M5 k; D" S, Q9 e- a9 g. Y8 N    .setAccountId("0G1Q6V1P00A02")
6 D: u3 o! n5 ^$ h- y0 \    .setAssetId(asset_id)" _) G& F4 X$ Z5 X
    .setAmount(300000000)8 V8 ^2 S$ c( P' p8 I+ h
    )6 y8 f0 a* |3 _5 r9 X
    .addAction(& s2 S; A' J" `2 i  o0 K9 k
    newTransaction.Action.ControlWithAddress()9 }) j' M0 H  d6 J
    .setAddress(address)0 E% N, ^0 S1 N
    .setAssetId(asset_id)9 [1 r& w$ i4 [
    .setAmount(60000000)6 `. H, d% J3 B( ~; |( e1 @
    ).build(client);! g" @& O+ B0 |+ i& y9 K; i
    logger.info("template:"+template.toJson());+ K- u- w# f/ q8 q6 T
    //useTemplateobject'sraw_transactionidtodecoderaw_transactionobtainaRawTransactionobject0 H6 E  \0 ?4 D0 S; ~; b9 C( w
    RawTransactiondecodedTx=RawTransaction.decode(client,template.rawTransaction);
. O7 F* q+ p# W& q! I+ ~' a    logger.info("decodeTx:"+decodedTx.toJson());& c# m* B1 I( Y% M. C( d. Y; ^6 r
    //needaprivatekeyarray0 k( ]( Y# V0 d) d8 ]
    String[]privateKeys=newString[]{"08bdbd6c22856c5747c930f64d0e5d58ded17c4473910c6c0c3f94e485833a436247976253c8e29e961041ad8dfad9309744255364323163837cbef2483b4f67",* d5 Q  y! ?# ]! B/ @6 i$ h2 c
    "40c821f736f60805ad59b1fea158762fa6355e258601dfb49dda6f672092ae5adf072d5cab2ceaaa0d68dd3fe7fa04869d95afed8c20069f446a338576901e1b",
3 I, M6 r' E: f+ j    "08bdbd6c22856c5747c930f64d0e5d58ded17c4473910c6c0c3f94e485833a436247976253c8e29e961041ad8dfad9309744255364323163837cbef2483b4f67"};
% G. i" @$ d' {$ z) z5 ^+ b) z) P    logger.info("privatekey1:"+privateKeys[0]);  P* i/ h9 Q, d2 a: Q
    logger.info("privatekey2:"+privateKeys[1]);1 t! C" R. Z9 Y" @' [$ f1 _6 H
    //callofflinesignmethodtoobtainabasicofflinesignedtemplate9 t* ^! F. u5 A2 {% Q: E
    Signaturessignatures=newSignaturesImpl();
5 ]9 P1 L8 y4 Z2 g    TemplatebasicSigned=signatures.generateSignatures(privateKeys,template,decodedTx);
* x2 V2 l$ c8 F$ X    logger.info("basicsignedraw:"+basicSigned.toJson());" F% n, b# T& [- f) N
    //callsigntransactionapitocalculatewholeraw_transactionid( h. ^, N& h8 \4 v+ w3 E7 c3 @
    //signpasswordisNoneoranotherrandomString; A' v# r2 P' m- n/ e
    Templateresult=newTransaction.SignerBuilder().sign(client,2 L& c2 C  p) Q! R- h8 _
    basicSigned,"");5 s  p" v1 z. k2 H$ s' H
    logger.info("resultraw_transaction:"+result.toJson());
5 [0 y- G2 \# b    //successtosubmittransaction  C6 f: I3 p3 n% j, n" D- s' D4 N
    }% @7 p- S! t& k% u
+ G" O( U9 W: L& U% S( T# q3 k
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

yuan081608 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    2