Skip to content

Commit

Permalink
fixes and optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
klim0v committed Mar 26, 2020
1 parent 056aded commit a5988cd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
10 changes: 4 additions & 6 deletions transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,14 +457,14 @@ func (o *object) Hash() (string, error) {
if err != nil {
return "", err
}
bytes := make([]byte, hex.DecodedLen(len(encode)-2))
_, err = hex.Decode(bytes, []byte(encode)[2:])
b := make([]byte, hex.DecodedLen(len(encode)-2))
_, err = hex.Decode(b, []byte(encode)[2:])
if err != nil {
return "", err
}
hash := sha256.Sum256(bytes)
hash := sha256.Sum256(b)

return "Mt" + hex.EncodeToString(hash[:])[:40], nil
return "Mt" + hex.EncodeToString(hash[:]), nil
}

func (o *object) addSignature(signatures ...*Signature) (SignedTransaction, error) {
Expand Down Expand Up @@ -505,8 +505,6 @@ func (o *object) AddSignature(signatures ...[]byte) (SignedTransaction, error) {
return nil, errors.New("multisig address not set")
}
signatureMulti := signature.(*SignatureMulti)
bytes, _ := rlp.EncodeToBytes(signatureMulti)
_ = bytes
signs := make([]*Signature, 0, len(signatures))
for _, signature := range signatures {
sig, err := decodeSignature(signature)
Expand Down
19 changes: 18 additions & 1 deletion transaction/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ import (
)

func TestTransaction_Hash(t *testing.T) {
signedTransaction, err := Decode("0xf8911602018a4d4e540000000000000008b7f6a014c93843ca40a62b9e7d02a824e7ffe83b49e3329ae963afdd7e500071ab9bfc8a4d4e54000000000000008902b5e3af16b1880000808001b845f8431ca043e12cac5d5059a60ef63e88904c6d3885cf17be25aa5bea291ea0ceb3103757a0274f26f6436db1e12c9bf2f701ff2d75285149f381785bf14c2c578510aa7d44")
if err != nil {
t.Fatal(err)
}

hash, err := signedTransaction.Hash()
if err != nil {
t.Fatal(err)
}

validHash := "Mt15ff7e4485f030c2b0e3e414b0d89615b5ecd24a5c3b4659bf561c32f75f5032"
if hash != validHash {
t.Errorf("Hash got %s, want %s", hash, validHash)
}
}

func TestTransaction_Hash2(t *testing.T) {
transaction, err := NewBuilder(TestNetChainID).NewTransaction(NewSendData().
SetCoin("MNT").
SetValue(big.NewInt(0).Mul(big.NewInt(1), big.NewInt(0).Exp(big.NewInt(10), big.NewInt(18), nil))).
Expand All @@ -30,7 +47,7 @@ func TestTransaction_Hash(t *testing.T) {
t.Fatal(err)
}

validHash := "Mt13b73500c171006613fa8e82cc8b29857af1d63a"
validHash := "Mt13b73500c171006613fa8e82cc8b29857af1d63a34ca2cada95024bacca1670c"
if hash != validHash {
t.Errorf("Hash got %s, want %s", hash, validHash)
}
Expand Down
1 change: 1 addition & 0 deletions wallet/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func AddressToHex(address string) ([]byte, error) {
if len(address) != 42 {
return nil, errors.New("address length less than 42 characters")
}
address = strings.Title(strings.ToLower(address))
if !strings.HasPrefix(address, "Mx") {
return nil, errors.New("address don't has prefix 'Mx'")
}
Expand Down

0 comments on commit a5988cd

Please sign in to comment.