|
| 1 | +package cosmos |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/cosmos/cosmos-sdk/codec" |
| 7 | + "github.com/cosmos/cosmos-sdk/simapp" |
| 8 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 9 | + "github.com/cosmos/cosmos-sdk/x/auth" |
| 10 | + "github.com/cosmos/cosmos-sdk/x/auth/client/utils" |
| 11 | + |
| 12 | + "github.com/renproject/multichain/compat/cosmoscompat" |
| 13 | + "github.com/renproject/pack" |
| 14 | + |
| 15 | + "github.com/tendermint/tendermint/crypto/secp256k1" |
| 16 | + "github.com/tendermint/tendermint/crypto/tmhash" |
| 17 | +) |
| 18 | + |
| 19 | +// NewClient returns returns a new Client with default codec |
| 20 | +func NewClient(opts cosmoscompat.ClientOptions) cosmoscompat.Client { |
| 21 | + return cosmoscompat.NewClient(opts, simapp.MakeCodec()) |
| 22 | +} |
| 23 | + |
| 24 | +type txBuilder struct { |
| 25 | + auth.TxBuilder |
| 26 | + cdc *codec.Codec |
| 27 | +} |
| 28 | + |
| 29 | +// NewTxBuilder returns an implementation of the transaction builder interface |
| 30 | +// from the Cosmos Compat API, and exposes the functionality to build simple |
| 31 | +// Cosmos based transactions. |
| 32 | +func NewTxBuilder(options cosmoscompat.TxOptions) cosmoscompat.TxBuilder { |
| 33 | + cdc := simapp.MakeCodec() |
| 34 | + |
| 35 | + return txBuilder{ |
| 36 | + TxBuilder: auth.NewTxBuilder( |
| 37 | + utils.GetTxEncoder(cdc), |
| 38 | + options.AccountNumber.Uint64(), |
| 39 | + options.SequenceNumber.Uint64(), |
| 40 | + options.Gas.Uint64(), |
| 41 | + 0, |
| 42 | + false, |
| 43 | + options.ChainID.String(), |
| 44 | + options.Memo.String(), |
| 45 | + options.Fees.Coins(), sdk.DecCoins{}, |
| 46 | + ), |
| 47 | + cdc: simapp.MakeCodec(), |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// WithCodec replace codec with custom one |
| 52 | +func (builder txBuilder) WithCodec(cdc *codec.Codec) cosmoscompat.TxBuilder { |
| 53 | + builder.WithTxEncoder(utils.GetTxEncoder(cdc)) |
| 54 | + builder.cdc = cdc |
| 55 | + return builder |
| 56 | +} |
| 57 | + |
| 58 | +func (builder txBuilder) BuildTx(sendMsgs []cosmoscompat.MsgSend) (cosmoscompat.Tx, error) { |
| 59 | + sdkMsgs := []sdk.Msg{} |
| 60 | + for _, sendMsg := range sendMsgs { |
| 61 | + sdkMsgs = append(sdkMsgs, sendMsg.Msg()) |
| 62 | + } |
| 63 | + |
| 64 | + signMsg, err := builder.BuildSignMsg(sdkMsgs) |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + |
| 69 | + return &Tx{cdc: builder.cdc, signMsg: signMsg}, nil |
| 70 | +} |
| 71 | + |
| 72 | +// Tx represents a simple Terra transaction that implements the Cosmos Compat |
| 73 | +// API. |
| 74 | +type Tx struct { |
| 75 | + cdc *codec.Codec |
| 76 | + signMsg auth.StdSignMsg |
| 77 | + signatures []auth.StdSignature |
| 78 | +} |
| 79 | + |
| 80 | +// Hash return txhash bytes |
| 81 | +func (tx *Tx) Hash() (pack.Bytes32, error) { |
| 82 | + if len(tx.signatures) == 0 { |
| 83 | + return pack.Bytes32{}, fmt.Errorf("please do tx.Sign() first to get a hash") |
| 84 | + } |
| 85 | + |
| 86 | + txBytes, err := tx.Serialize() |
| 87 | + if err != nil { |
| 88 | + return pack.Bytes32{}, err |
| 89 | + } |
| 90 | + |
| 91 | + hashBytes := pack.Bytes32{} |
| 92 | + hashBytes.Unmarshal(tmhash.Sum(txBytes), 32) |
| 93 | + return hashBytes, nil |
| 94 | +} |
| 95 | + |
| 96 | +// SigBytes that need to be signed before this transaction can be |
| 97 | +// submitted. |
| 98 | +func (tx *Tx) SigBytes() pack.Bytes { |
| 99 | + return tx.signMsg.Bytes() |
| 100 | +} |
| 101 | + |
| 102 | +// Sign the transaction by injecting signatures and the serialized pubkey of |
| 103 | +// the signer. |
| 104 | +func (tx *Tx) Sign(signatures []cosmoscompat.StdSignature) error { |
| 105 | + var stdSignatures []auth.StdSignature |
| 106 | + for _, sig := range signatures { |
| 107 | + var pubKey secp256k1.PubKeySecp256k1 |
| 108 | + copy(pubKey[:], sig.PubKey[:secp256k1.PubKeySecp256k1Size]) |
| 109 | + |
| 110 | + stdSignatures = append(stdSignatures, auth.StdSignature{ |
| 111 | + Signature: sig.Signature, |
| 112 | + PubKey: pubKey, |
| 113 | + }) |
| 114 | + } |
| 115 | + |
| 116 | + signers := make(map[string]bool) |
| 117 | + for _, msg := range tx.signMsg.Msgs { |
| 118 | + for _, signer := range msg.GetSigners() { |
| 119 | + fmt.Println("SIBONG", signer.String()) |
| 120 | + signers[signer.String()] = true |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + for _, sig := range stdSignatures { |
| 125 | + signer := sdk.AccAddress(sig.Address()).String() |
| 126 | + if _, ok := signers[signer]; !ok { |
| 127 | + return fmt.Errorf("wrong signer: %s", signer) |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + if len(signers) != len(stdSignatures) { |
| 132 | + return fmt.Errorf("insufficient signers") |
| 133 | + } |
| 134 | + |
| 135 | + fmt.Println("SIBONG", stdSignatures) |
| 136 | + tx.signatures = stdSignatures |
| 137 | + return nil |
| 138 | +} |
| 139 | + |
| 140 | +// Serialize the transaction. |
| 141 | +func (tx *Tx) Serialize() (pack.Bytes, error) { |
| 142 | + txBytes, err := tx.cdc.MarshalBinaryLengthPrefixed(auth.NewStdTx(tx.signMsg.Msgs, tx.signMsg.Fee, tx.signatures, tx.signMsg.Memo)) |
| 143 | + if err != nil { |
| 144 | + return pack.Bytes{}, err |
| 145 | + } |
| 146 | + |
| 147 | + return txBytes, nil |
| 148 | +} |
0 commit comments