Skip to content

Commit

Permalink
fix sender
Browse files Browse the repository at this point in the history
  • Loading branch information
klim0v committed Nov 3, 2021
1 parent 4e49744 commit 9b1e205
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 20 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
github.com/btcsuite/btcd v0.22.0-beta // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
github.com/ethereum/go-ethereum v1.10.8
github.com/ethereum/go-ethereum v1.10.9
github.com/go-openapi/analysis v0.20.1 // indirect
github.com/go-openapi/errors v0.20.1
github.com/go-openapi/jsonreference v0.19.6 // indirect
Expand Down
9 changes: 6 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m
github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.mod h1:hliV/p42l8fGbc6Y9bQ70uLwIvmJyVE5k4iMKlh8wCQ=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/ethereum/go-ethereum v1.10.8 h1:0UP5WUR8hh46ffbjJV7PK499+uGEyasRIfffS0vy06o=
github.com/ethereum/go-ethereum v1.10.8/go.mod h1:pJNuIUYfX5+JKzSD/BTdNsvJSZ1TJqmz0dVyXMAbf6M=
github.com/ethereum/go-ethereum v1.10.9 h1:uMSWt0qDhaqqCk0PWqfDFOMUExmk4Tnbma6c6oXW+Pk=
github.com/ethereum/go-ethereum v1.10.9/go.mod h1:CaTMQrv51WaAlD2eULQ3f03KiahDRO28fleQcKjWrrg=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0=
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
Expand Down Expand Up @@ -351,6 +351,7 @@ github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golangci/lint-1 v0.0.0-20181222135242-d2cdd8c08219/go.mod h1:/X8TswGSh1pIozq4ZwCfxS0WA5JGXguxk94ar/4c87Y=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
Expand Down Expand Up @@ -398,6 +399,7 @@ github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.5.0 h1:ajue7SzQMywqRjg2fK7dcpc0QhFGpTR2plWfV4EZWR4=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.5.0/go.mod h1:r1hZAcvfFXuYmcKyCJI9wlyOPIZUJl6FCB8Cpca/NLE=
github.com/hashicorp/go-bexpr v0.1.10/go.mod h1:oxlubA2vC/gFVfX1A6JGp7ls7uCDlfJn732ehYYg+g0=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
Expand Down Expand Up @@ -500,6 +502,7 @@ github.com/mitchellh/mapstructure v1.3.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR
github.com/mitchellh/mapstructure v1.4.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag=
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/pointerstructure v1.2.0/go.mod h1:BRAsLI5zgXmw97Lf6s25bs8ohIXc3tViBH44KcwB2g4=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
Expand Down Expand Up @@ -597,7 +600,7 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/syndtr/goleveldb v1.0.1-0.20210305035536-64b5b1c73954/go.mod h1:u2MKkTVTVJWe5D1rCvame8WqhBd88EuIwODJZ1VHCPM=
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
Expand Down
4 changes: 2 additions & 2 deletions transaction/create_multisig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func TestCreateMultisigData_SignGetAddress(t *testing.T) {
t.Fatal(err)
}

if address != "Mxb43154a0bc801c4b7361bf1a535b5e08e34e401c" {
t.Errorf("Address got %s, want %s", address, "Mxb43154a0bc801c4b7361bf1a535b5e08e34e401c")
if address != "Mx3e4d56e776ff42c023b1ec99a7486b592a654981" {
t.Errorf("Address got %s, want %s", address, "Mx3e4d56e776ff42c023b1ec99a7486b592a654981")
}

encode, err := signedTx.Encode()
Expand Down
8 changes: 4 additions & 4 deletions transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ func (o *object) SenderAddress() (string, error) {
return "", err
}

return single.signer(hash, SignatureTypeSingle)
return single.signer(hash)
}

if multi, ok := signature.(*SignatureMulti); o.SignatureType == SignatureTypeMulti && ok {
Expand Down Expand Up @@ -477,13 +477,13 @@ func (s *SignatureSingle) Single() ([]byte, error) {
return s.Encode()
}

func (s *SignatureSingle) signer(hash PublicKey, t SignatureType) (string, error) {
func (s *SignatureSingle) signer(hash PublicKey) (string, error) {
publicKey, err := crypto.Ecrecover(hash[:], s.toBytes())
if err != nil {
return "", err
}

address, err := wallet.AddressByPublicKey("Mp" + hex.EncodeToString(publicKey[t-1:]))
address, err := wallet.AddressByPublicKey("Mp" + hex.EncodeToString(publicKey[1:]))
if err != nil {
return "", err
}
Expand Down Expand Up @@ -575,7 +575,7 @@ func (o *object) Signers() ([]string, error) {

signers := make([]string, 0, len(multi.Signatures))
for _, signature := range multi.Signatures {
address, err := signature.signer(hash, SignatureTypeMulti)
address, err := signature.signer(hash)
if err != nil {
return nil, err
}
Expand Down
31 changes: 27 additions & 4 deletions transaction/transaction_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package transaction_test

import (
"fmt"
"github.com/MinterTeam/minter-go-sdk/v2/wallet"
"math/big"

"github.com/MinterTeam/minter-go-sdk/v2/transaction"
Expand Down Expand Up @@ -31,23 +32,45 @@ func ExampleBuilder_NewTransaction_signSingleSignature_simple() {
fmt.Println(encode)

// Output:
// Mx622e1e0e788f4b1258f7e2a196f738c6a360c3de
// Mx31e61a05adbd13c6b625262704bc305bf7725026
// Mtec2166cced36276426360a79934fbf49f29f9e48e9d1f06ef4afc4f557aa3767
// 0xf8700102010101a0df01941b685a7c1e78726c48f619c497a07ed75fe00483880de0b6b3a7640000808001b845f8431ba0fffc3f503ace8a5d0c87efe50cf33ad41e3475459120d9c6fd75bd796b192313a0243d643a799e844ad82382d41cee98137a1d0c5888ff13951919e5e241ab89e0

}

func ExampleDecode() {
tx, _ := transaction.Decode("0xf8750102010101a0df01941b685a7c1e78726c48f619c497a07ed75fe00483880de0b6b3a76400008548656c6c6f8001b845f8431ca0d2b9034178aa5eab45696bd25ced410dd21298e160c4a51c3fcfd85bb0f49352a006705dc04165b183a3448394fdd93a881eb60bb7881a05a528125e1d8259ac75")
var encode string
{
tx, _ := transaction.NewBuilder(transaction.TestNetChainID).NewTransaction(
transaction.NewSendData().
SetCoin(1).
SetValue(transaction.BipToPip(big.NewInt(1))).
MustSetTo("Mx1b685a7c1e78726c48f619c497a07ed75fe00483"),
)
w, _ := wallet.Create("suffer draft bacon typical start retire air sniff large biology mail diagram", "")
fmt.Println(w.Address)
signedTransaction, _ := tx.
SetGasPrice(1).
SetGasCoin(1).
SetNonce(1).
SetPayload([]byte("Hello")).
Sign(w.PrivateKey)
encode, _ = signedTransaction.Encode()
}
tx, _ := transaction.Decode(encode)

fmt.Println(tx.Data().Type())
fmt.Println(tx.Data().(*transaction.SendData).Coin)
fmt.Println(string(tx.GetTransaction().Payload))
address, _ := tx.SenderAddress()
fmt.Println(address)

// Output:
// Mx48f502a9fc324f2c707edc3a2595e72f00c3190c
// 0x01
// 1
// Hello
// Mx48f502a9fc324f2c707edc3a2595e72f00c3190c
}

func ExampleBuilder_NewTransaction_signMultiSignature_simultaneous_adding_private_keys() {
Expand Down Expand Up @@ -159,6 +182,6 @@ func ExampleDecode_signersOfSingleSignature() {
fmt.Println(address)

// Output:
// Mx622e1e0e788f4b1258f7e2a196f738c6a360c3de
// Mx622e1e0e788f4b1258f7e2a196f738c6a360c3de
// Mx31e61a05adbd13c6b625262704bc305bf7725026
// Mx31e61a05adbd13c6b625262704bc305bf7725026
}
22 changes: 16 additions & 6 deletions transaction/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestTransaction_Hash2(t *testing.T) {

func TestTransaction_Encode(t *testing.T) {
t.Parallel()
decode, err := Decode("0xf865010201010495d402880de0b6b3a764000001880de0b6b3a7640000808001b845f8431ca0ad334ececd68741f1f9b96e15a2b5d6a7fe6c378cdaab6c6e8947541e1af74dda038c829477eb261948598fd3dd039aba41aa5691f50d3ee2bb4125bc38b294725")
decode, err := Decode(txBuyCoin)
if err != nil {
t.Fatal(err)
}
Expand All @@ -83,7 +83,7 @@ func TestTransaction_Encode(t *testing.T) {

func TestObject_SenderAddress(t *testing.T) {
t.Parallel()
transaction, err := Decode("0xf865010201010495d402880de0b6b3a764000001880de0b6b3a7640000808001b845f8431ca0ad334ececd68741f1f9b96e15a2b5d6a7fe6c378cdaab6c6e8947541e1af74dda038c829477eb261948598fd3dd039aba41aa5691f50d3ee2bb4125bc38b294725")
transaction, err := Decode(txBuyCoin)
if err != nil {
t.Fatal(err)
}
Expand All @@ -93,8 +93,8 @@ func TestObject_SenderAddress(t *testing.T) {
t.Fatal(err)
}

if senderAddress != "Mx622e1e0e788f4b1258f7e2a196f738c6a360c3de" {
t.Errorf("SenderAddress want %s,\ngot %s", "Mx622e1e0e788f4b1258f7e2a196f738c6a360c3de", senderAddress)
if senderAddress != "Mx31e61a05adbd13c6b625262704bc305bf7725026" {
t.Errorf("SenderAddress want %s,\ngot %s", "Mx31e61a05adbd13c6b625262704bc305bf7725026", senderAddress)
}
}

Expand Down Expand Up @@ -323,16 +323,26 @@ func TestMultisigAddSignatures2(t *testing.T) {

func TestDecodeMulti(t *testing.T) {
t.Parallel()
decode, err := Decode("0xf899010201010cb848f84607c3010305f83f94ee81347211c72524338f9680072af9074433314394ee81347211c72524338f9680072af9074433314594ee81347211c72524338f9680072af90744333144808001b845f8431ca0224c6166a1f4667cb0bee9ce7ed88879285b8ffc9b4eac3f03faa1797d1f8684a0276dc68fc640924e970c3607af33988a0955e7c2dff78a16ba795da9ddffe988")
decode, err := Decode("0xf901130102010101a0df0194d82558ea00eb81d35f2654953598f5d51737d31d880de0b6b3a7640000808002b8e8f8e694db4f4b6942cb927e8d7e3a1f602d0f1fb43b5bd2f8cff8431ca0a116e33d2fea86a213577fc9dae16a7e4cadb375499f378b33cddd1d4113b6c1a021ee1e9eb61bbd24233a0967e1c745ab23001cf8816bb217d01ed4595c6cb2cdf8431ca0f7f9c7a6734ab2db210356161f2d012aa9936ee506d88d8d0cba15ad6c84f8a7a04b71b87cbbe7905942de839211daa984325a15bdeca6eea75e5d0f28f9aaeef8f8431ba0d8c640d7605034eefc8870a6a3d1c22e2f589a9319288342632b1c4e6ce35128a055fe3f93f31044033fe7b07963d547ac50bccaac38a057ce61665374c72fb454")
if err != nil {
t.Fatal(err)
}

signature, err := decode.Signature()
if err != nil {
t.Fatal(err)
}

if signature.Type() != SignatureTypeMulti {
t.Fatal(signature.Type())
}

senderAddress, err := decode.SenderAddress()
if err != nil {
t.Fatal(err)
}

validSenderAddress := "Mxb43154a0bc801c4b7361bf1a535b5e08e34e401c"
validSenderAddress := "Mxdb4f4b6942cb927e8d7e3a1f602d0f1fb43b5bd2"
if senderAddress != validSenderAddress {
t.Fatalf("SenderAddress got %s, want %s", senderAddress, validSenderAddress)
}
Expand Down

0 comments on commit 9b1e205

Please sign in to comment.