daily pastebin goal
59%
SHARE
TWEET

Untitled

a guest Sep 14th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package context
  2.  
  3. import (
  4.     "fmt"
  5.     "github.com/pkg/errors"
  6.     "strings"
  7.  
  8.     "github.com/ethereum/go-ethereum/accounts"
  9.     "github.com/ethereum/go-ethereum/common"
  10.     ethcrypto "github.com/ethereum/go-ethereum/crypto"
  11.     rlp "github.com/ethereum/go-ethereum/rlp"
  12.     rpcclient "github.com/tendermint/tendermint/rpc/client"
  13.     ctypes "github.com/tendermint/tendermint/rpc/core/types"
  14.  
  15.     "github.com/FourthState/plasma-mvp-sidechain/client"
  16.     "github.com/FourthState/plasma-mvp-sidechain/types"
  17.     "github.com/FourthState/plasma-mvp-sidechain/utils"
  18. )
  19.  
  20. // Broadcast the transaction bytes to Tendermint
  21. func (ctx ClientContext) BroadcastTx(tx []byte) (*ctypes.ResultBroadcastTxCommit, error) {
  22.     node, err := ctx.GetNode()
  23.     if err != nil {
  24.         return nil, err
  25.     }
  26.  
  27.     res, err := node.BroadcastTxCommit(tx)
  28.     if err != nil {
  29.         return res, err
  30.     }
  31.  
  32.     if res.CheckTx.Code != uint32(0) {
  33.         return res, errors.Errorf("CheckTx failed: (%d) %s",
  34.             res.CheckTx.Code, res.CheckTx.Log)
  35.     }
  36.     if res.DeliverTx.Code != uint32(0) {
  37.         return res, errors.Errorf("DeliverTx failed: (%d) %s",
  38.             res.DeliverTx.Code, res.DeliverTx.Log)
  39.     }
  40.     return res, err
  41.  
  42. }
  43.  
  44. // sign and build the transaction from the msg
  45. func (ctx ClientContext) SignBuildBroadcast(addrs [2]common.Address, msg types.SpendMsg, dir string) (res *ctypes.ResultBroadcastTxCommit, err error) {
  46.  
  47.     sig, err := ctx.GetSignature(addrs[0], msg, dir)
  48.     if err != nil {
  49.         return nil, err
  50.     }
  51.     sigs := []types.Signature{types.Signature{sig}}
  52.  
  53.     if utils.ValidAddress(addrs[1]) {
  54.         sig, err = ctx.GetSignature(addrs[1], msg, dir)
  55.         if err != nil {
  56.             return nil, err
  57.         }
  58.         sigs = append(sigs, types.Signature{sig})
  59.     }
  60.  
  61.     tx := types.NewBaseTx(msg, sigs)
  62.  
  63.     txBytes, err := rlp.EncodeToBytes(tx)
  64.     if err != nil {
  65.         return nil, err
  66.     }
  67.  
  68.     return ctx.BroadcastTx(txBytes)
  69. }
  70.  
  71. func (ctx ClientContext) GetSignature(addr common.Address, msg types.SpendMsg, dir string) (sig []byte, err error) {
  72.  
  73.     passphrase, err := ctx.GetPassphraseFromStdin(addr)
  74.     if err != nil {
  75.         return nil, err
  76.     }
  77.     ks := client.GetKeyStore(dir)
  78.     acc := accounts.Account{
  79.         Address: addr,
  80.     }
  81.  
  82.     acct, err := ks.Find(acc)
  83.     if err != nil {
  84.         return nil, err
  85.     }
  86.  
  87.     bz := msg.GetSignBytes()
  88.     hash := ethcrypto.Keccak256(bz)
  89.  
  90.     sig, err = ks.SignHashWithPassphrase(acct, passphrase, hash)
  91.     if err != nil {
  92.         return nil, err
  93.     }
  94.     return sig, nil
  95.  
  96. }
  97.  
  98. // Get the from address from the name flag
  99. func (ctx ClientContext) GetInputAddresses(dir string) (from [2]common.Address, err error) {
  100.  
  101.     ks := client.GetKeyStore(dir)
  102.  
  103.     addrsStr := ctx.InputAddresses
  104.     if addrsStr == "" {
  105.         return [2]common.Address{}, errors.Errorf("must provide an address to send from")
  106.     }
  107.  
  108.     addrs := strings.Split(addrsStr, ",")
  109.     // first input address
  110.     from[0], err = client.StrToAddress(strings.TrimSpace(addrs[0]))
  111.     if err != nil {
  112.         return [2]common.Address{}, err
  113.     }
  114.     if len(addrs) > 1 {
  115.         // second input address
  116.         from[1], err = client.StrToAddress(strings.TrimSpace(addrs[1]))
  117.         if err != nil {
  118.             return [2]common.Address{}, err
  119.         }
  120.     }
  121.  
  122.     if !ks.HasAddress(from[0]) {
  123.         return [2]common.Address{}, errors.Errorf("no account for: %s", from[0].Hex())
  124.     }
  125.     if len(from) > 1 && !utils.ZeroAddress(from[1]) && !ks.HasAddress(from[1]) {
  126.         return [2]common.Address{}, errors.Errorf("no account for: %s", from[1].Hex())
  127.     }
  128.  
  129.     return from, nil
  130. }
  131.  
  132. // Get passphrase from std input
  133. func (ctx ClientContext) GetPassphraseFromStdin(addr common.Address) (pass string, err error) {
  134.     buf := client.BufferStdin()
  135.     prompt := fmt.Sprintf("Password to sign with '%s':", addr.Hex())
  136.     return client.GetPassword(prompt, buf)
  137. }
  138.  
  139. // Prepares a simple rpc.Client
  140. func (ctx ClientContext) GetNode() (rpcclient.Client, error) {
  141.     if ctx.Client == nil {
  142.         return nil, errors.New("must define node URI")
  143.     }
  144.     return ctx.Client, nil
  145. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top