Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add counter retry #194

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
d00085a
make PubKey public
guillaumeduhamel Sep 21, 2021
448ff26
fix import
guillaumeduhamel Sep 21, 2021
d4e10b9
add FromBase58Pk function
guillaumeduhamel Sep 21, 2021
8a39a50
add sign data function from key
guillaumeduhamel Sep 22, 2021
92a4575
fix forgeInt with big.Int
guillaumeduhamel Sep 22, 2021
565ae1e
add consumed_milligas for preapply operation
guillaumeduhamel Sep 22, 2021
1a83fd3
add id field on Operations struct
guillaumeduhamel Sep 24, 2021
d912b6e
fix 0 for ForgeInt
guillaumeduhamel Sep 24, 2021
24f6f2c
make BlakeHash public
guillaumeduhamel Sep 26, 2021
1264126
add milli gas field for internal operation result
guillaumeduhamel Sep 26, 2021
8fb52cb
fix InternalOperationResults parameters
guillaumeduhamel Sep 26, 2021
be1400e
add GetPkhFromBytes function
guillaumeduhamel Sep 27, 2021
d50bac2
up
guillaumeduhamel Sep 27, 2021
16e9af6
up
guillaumeduhamel Sep 27, 2021
061da89
check signature on tz1
guillaumeduhamel Sep 27, 2021
d36f3b8
add tz2 check signature
guillaumeduhamel Sep 28, 2021
83f5464
add validation functions
guillaumeduhamel Nov 4, 2021
67b6433
fix key test
guillaumeduhamel Nov 5, 2021
05d70cf
add paid storage diff field for internal operation result
sitratel Nov 17, 2021
fda92e8
Merge pull request #2 from sitratel/internal-ops
guillaumeduhamel Nov 18, 2021
439bf0d
Merge branch 'master' of https://github.com/completium/go-tezos
guillaumeduhamel Nov 22, 2021
b80f344
handle error on B58cdecode function
guillaumeduhamel Nov 25, 2021
bad6e6a
clean tests
guillaumeduhamel Nov 25, 2021
302dd44
add tests
guillaumeduhamel Nov 25, 2021
a410e15
fix forgeNat
guillaumeduhamel Dec 7, 2021
e739439
fix secp256k1 getPublicKey panic
jeanschmitt Dec 7, 2021
230e947
backtrack
guillaumeduhamel Dec 8, 2021
e127854
Merge pull request #3 from jeanschmitt/master
guillaumeduhamel Dec 8, 2021
e2b6aba
fix operation signature
guillaumeduhamel Dec 8, 2021
4233008
add SetRetryCount and improve getPublicKey for nistP256
guillaumeduhamel Dec 9, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.idea
.idea
.history
vendor
.vscode
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PROJECT_NAME := "go-tezos"
VERSION := "v3.0.0"
PKG := "github.com/goat-systems/$(PROJECT_NAME)"
PKG := "github.com/completium/$(PROJECT_NAME)"
PKG_LIST := $(shell go list ${PKG}/... | grep -v /vendor/)
GO_FILES := $(shell find . -name '*.go' | grep -v /vendor/ | grep -v _test.go)

Expand Down
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
[![GoDoc](https://godoc.org/github.com/golang/gddo?status.svg)](https://godoc.org/github.com/goat-systems/go-tezos/v4)
[![GoDoc](https://godoc.org/github.com/golang/gddo?status.svg)](https://godoc.org/github.com/completium/go-tezos/v4)

# Foreword

This package is a fork from https://github.com/completium/go-tezos

# A Tezos Go Library

Go Tezos is a GoLang driven library for your Tezos node. This library has received a grant from the Tezos Foundation to ensure it's continuous development through 2020.

## Installation

Get GoTezos
Get GoTezos
```
go get github.com/goat-systems/go-tezos/v4
go get github.com/completium/go-tezos/v4
```

### Getting A Block
Expand All @@ -17,7 +22,7 @@ package main

import (
"fmt"
goTezos "github.com/goat-systems/go-tezos/v4/rpc"
goTezos "github.com/completium/go-tezos/v4/rpc"
)

func main() {
Expand Down Expand Up @@ -47,12 +52,12 @@ func main() {

### More Examples
You can find more examples by looking through the unit tests and integration tests in each package. [Here](example/transaction/transaction.go) is an example on
how to forge and inject an operation.
how to forge and inject an operation.

## Contributing

### The Makefile
The makefile is there as a helper to run quality code checks. To run vet and staticchecks please run:
The makefile is there as a helper to run quality code checks. To run vet and staticchecks please run:
```
make checks
```
Expand Down
8 changes: 4 additions & 4 deletions example/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"os"
"strconv"

"github.com/goat-systems/go-tezos/v4/forge"
"github.com/goat-systems/go-tezos/v4/keys"
"github.com/goat-systems/go-tezos/v4/rpc"
"github.com/completium/go-tezos/v4/forge"
"github.com/completium/go-tezos/v4/keys"
"github.com/completium/go-tezos/v4/rpc"
)

func main() {
Expand Down Expand Up @@ -57,7 +57,7 @@ func main() {
os.Exit(1)
}

signature, err := key.SignHex(op)
signature, err := key.SignGeneric(op)
if err != nil {
fmt.Printf("failed to sign operation: %s\n", err.Error())
os.Exit(1)
Expand Down
123 changes: 74 additions & 49 deletions forge/forge.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import (
"fmt"
"math"
"math/big"
"strconv"
"strings"
"time"

"github.com/btcsuite/btcutil/base58"
"github.com/completium/go-tezos/v4/internal/crypto"
"github.com/completium/go-tezos/v4/rpc"
validator "github.com/go-playground/validator/v10"
"github.com/goat-systems/go-tezos/v4/internal/crypto"
"github.com/goat-systems/go-tezos/v4/rpc"
"github.com/pkg/errors"
"github.com/valyala/fastjson"
"golang.org/x/crypto/blake2b"
Expand Down Expand Up @@ -173,6 +172,15 @@ func primTags(prim string) byte {
return tags[prim]
}

func StringToBigInt(input string) (*big.Int, error) {
i := new(big.Int)
i, ok := i.SetString(input, 10)
if !ok {
return nil, fmt.Errorf("cannot StringToBigInt: %s", input)
}
return i, nil
}

/*
Encode forges an operation locally. GoTezos does not use the RPC or a trusted source to forge operations.
All operations are supported:
Expand Down Expand Up @@ -840,6 +848,17 @@ func forgeInt32(value int, l int) []byte {
return bigE
}

// func forgeNat(value string) ([]byte, error) {
// b, err := StringToBigInt(value)
// if b.Sign() < 0 {
// return nil, fmt.Errorf("nat value (%s) cannot be negative", value)
// }
// if err != nil {
// return nil, err
// }
// return forgeInt(b), nil
// }

func forgeNat(value string) ([]byte, error) {
var z big.Int
_, ok := z.SetString(string(value), 10)
Expand Down Expand Up @@ -965,47 +984,53 @@ func forgeArray(value []byte, l int) []byte {
return bytes
}

func forgeInt(value int) []byte {
binary := strconv.FormatInt(int64(math.Abs(float64(value))), 2)
lenBin := len(binary)

pad := 6
if (lenBin-6)%7 == 0 {
pad = lenBin
} else if lenBin > 6 {
pad = lenBin + 7 - (lenBin-6)%7
}
func forgeInt(value *big.Int) []byte {
if value.Cmp(big.NewInt(0)) == 0 {
return []byte{00}
} else {
sign := value.Sign()
binary := fmt.Sprintf("%b", value.Abs(value))
lenBin := len(binary)

pad := 6
if (lenBin-6)%7 == 0 {
pad = lenBin
} else if lenBin > 6 {
pad = lenBin + 7 - (lenBin-6)%7
}

binary = fmt.Sprintf("%0*s", pad, binary)
septets := []string{}
binary = fmt.Sprintf("%0*s", pad, binary)
septets := []string{}

for i := 0; i <= pad/7; i++ {
index := 7 * i
length := int(math.Min(7, float64(pad-7*i)))
septets = append(septets, binary[index:(index+length)])
}
for i := 0; i <= pad/7; i++ {
index := 7 * i
length := int(math.Min(7, float64(pad-7*i)))
septets = append(septets, binary[index:(index+length)])
}

septets = reverseStrings(septets)
if value >= 0 {
septets[0] = fmt.Sprintf("0%s", septets[0])
} else {
septets[0] = fmt.Sprintf("1%s", septets[0])
}
septets = reverseStrings(septets)
if sign >= 0 {
septets[0] = fmt.Sprintf("0%s", septets[0])
} else {
septets[0] = fmt.Sprintf("1%s", septets[0])
}

buf := bytes.NewBuffer([]byte{})
buf := bytes.NewBuffer([]byte{})

for i := 0; i < len(septets); i++ {
for i := 0; i < len(septets); i++ {

prefix := "1"
if i == len(septets)-1 {
prefix = "0"
prefix := "1"
if i == len(septets)-1 {
prefix = "0"
}
n := new(big.Int)
n.SetString(prefix+septets[i], 2)
buf.Write(n.Bytes())
}
n := new(big.Int)
n.SetString(prefix+septets[i], 2)
buf.Write(n.Bytes())

return buf.Bytes()
}

return buf.Bytes()
}

func forgePublicKey(value string) ([]byte, error) {
Expand Down Expand Up @@ -1180,9 +1205,9 @@ func forgeMicheline(micheline *fastjson.Value) ([]byte, error) {
} else if obj.Get("int") != nil {
buf.WriteByte(0x00)

i, err := strconv.Atoi(strings.Trim(obj.Get("int").String(), "\""))
i, err := StringToBigInt(strings.Trim(obj.Get("int").String(), "\""))
if err != nil {
return []byte{}, errors.New("failed to forge \"int\"")
return []byte{}, errors.New(fmt.Sprintf("failed to forge \"int\": %s", err))
}

buf.Write(forgeInt(i))
Expand All @@ -1204,8 +1229,8 @@ func prefixAndBase58Encode(hexPayload string, prefix []byte) (string, error) {
}

// IntExpression will pack and encode an integer to a script_expr
func IntExpression(i int) (string, error) {
v, err := blakeHash(fmt.Sprintf("0500%s", hex.EncodeToString(forgeInt(i))))
func IntExpression(i *big.Int) (string, error) {
v, err := BlakeHash(fmt.Sprintf("0500%s", hex.EncodeToString(forgeInt(i))))
if err != nil {
return "", errors.Wrap(err, "failed to pack int")
}
Expand All @@ -1214,17 +1239,17 @@ func IntExpression(i int) (string, error) {
}

// NatExpression will pack and encode a nat to a script_expr
func NatExpression(i int) (string, error) {
if i < 0 {
func NatExpression(i *big.Int) (string, error) {
if i.Sign() < 0 {
return "", errors.New("failed to pack nat: nat must be positive")
}

v, err := forgeNat(strconv.Itoa(i))
v, err := forgeNat(i.String())
if err != nil {
return "", errors.Wrap(err, "failed to pack nat")
}

v, err = blakeHash(fmt.Sprintf("0500%s", hex.EncodeToString(v)))
v, err = BlakeHash(fmt.Sprintf("0500%s", hex.EncodeToString(v)))
if err != nil {
return "", errors.Wrap(err, "failed to pack nat")
}
Expand All @@ -1234,7 +1259,7 @@ func NatExpression(i int) (string, error) {

// StringExpression will pack and encode a string to a script_expr
func StringExpression(value string) (string, error) {
v, err := blakeHash(fmt.Sprintf("0501%s%s", dataLength(len(value)), hex.EncodeToString([]byte(value))))
v, err := BlakeHash(fmt.Sprintf("0501%s%s", dataLength(len(value)), hex.EncodeToString([]byte(value))))
if err != nil {
return "", errors.Wrap(err, "failed to pack string")
}
Expand All @@ -1250,7 +1275,7 @@ func KeyHashExpression(hash string) (string, error) {
}
hash = hex.EncodeToString(v)

v, err = blakeHash(fmt.Sprintf("050a%s%s", dataLength(len(hash)/2), hex.EncodeToString(v)))
v, err = BlakeHash(fmt.Sprintf("050a%s%s", dataLength(len(hash)/2), hex.EncodeToString(v)))
if err != nil {
return "", errors.Wrap(err, "failed to pack key hash")
}
Expand All @@ -1265,7 +1290,7 @@ func AddressExpression(address string) (string, error) {
return "", errors.Wrap(err, "failed to pack address")
}

v, err = blakeHash(fmt.Sprintf("050a%s%s", dataLength(len(address)/2), hex.EncodeToString(v)))
v, err = BlakeHash(fmt.Sprintf("050a%s%s", dataLength(len(address)/2), hex.EncodeToString(v)))
if err != nil {
return "", errors.Wrap(err, "failed to pack address")
}
Expand All @@ -1276,7 +1301,7 @@ func AddressExpression(address string) (string, error) {
// BytesExpression will pack and encode bytes to a script_expr
func BytesExpression(v []byte) (string, error) {
h := hex.EncodeToString(v)
v, err := blakeHash(fmt.Sprintf("050a%s%s", dataLength(len(h)/2), h))
v, err := BlakeHash(fmt.Sprintf("050a%s%s", dataLength(len(h)/2), h))
if err != nil {
return "", errors.Wrap(err, "failed to pack bytes")
}
Expand All @@ -1295,7 +1320,7 @@ func MichelineExpression(micheline string) (string, error) {
return "", errors.Wrap(err, "failed to pack micheline")
}

hash, err := blakeHash(fmt.Sprintf("05%s", hex.EncodeToString(x)))
hash, err := BlakeHash(fmt.Sprintf("05%s", hex.EncodeToString(x)))
if err != nil {
return "", errors.Wrap(err, "failed to pack micheline")
}
Expand All @@ -1312,7 +1337,7 @@ func dataLength(val int) string {
return x
}

func blakeHash(hexStr string) ([]byte, error) {
func BlakeHash(hexStr string) ([]byte, error) {
v := []byte{}
for i := 0; i < len(hexStr); i += 2 {
elem, err := hex.DecodeString(hexStr[i:(i + 2)])
Expand Down
41 changes: 36 additions & 5 deletions forge/forge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package forge
import (
"encoding/hex"
"encoding/json"
"math/big"
"testing"

"github.com/goat-systems/go-tezos/v4/internal/testutils"
"github.com/goat-systems/go-tezos/v4/rpc"
"github.com/completium/go-tezos/v4/internal/testutils"
"github.com/completium/go-tezos/v4/rpc"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -748,17 +749,17 @@ func Test_Forge_Reveal(t *testing.T) {
}

func Test_IntExpression(t *testing.T) {
val, err := IntExpression(9)
val, err := IntExpression(big.NewInt(9))
testutils.CheckErr(t, false, "", err)
assert.Equal(t, "exprtvAzqNE9zfpBLL9nKEaY1Dd2rznyG9iTFtECJvDkuub1bj3XvW", val)

val, err = IntExpression(-9)
val, err = IntExpression(big.NewInt(-9))
testutils.CheckErr(t, false, "", err)
assert.Equal(t, "exprvH9jru3NJN4ZTNwwkCdC1PPLkWLWCoe6JxhcJ3a39mD5Bd4NH4", val)
}

func Test_NatExpression(t *testing.T) {
val, err := NatExpression(9)
val, err := NatExpression(big.NewInt(9))
testutils.CheckErr(t, false, "", err)
assert.Equal(t, "exprtvAzqNE9zfpBLL9nKEaY1Dd2rznyG9iTFtECJvDkuub1bj3XvW", val)
}
Expand Down Expand Up @@ -794,3 +795,33 @@ func Test_MichelineExpression(t *testing.T) {
testutils.CheckErr(t, false, "", err)
assert.Equal(t, "exprupozG51AtT7yZUy5sg6VbJQ4b9omAE1PKD2PXvqi2YBuZqoKG3", val)
}

func Test_ForgeNat2(t *testing.T) {
v64, _ := forgeNat("63")
v := hex.EncodeToString(v64)
assert.Equal(t, "3f", v)
}

func Test_ForgeNat3(t *testing.T) {
v64, _ := forgeNat("64")
v := hex.EncodeToString(v64)
assert.Equal(t, "40", v)
}

func Test_NatExpression0(t *testing.T) {
val, err := NatExpression(big.NewInt(9))
testutils.CheckErr(t, false, "", err)
assert.Equal(t, "exprtvAzqNE9zfpBLL9nKEaY1Dd2rznyG9iTFtECJvDkuub1bj3XvW", val)
}

func Test_NatExpression00(t *testing.T) {
val, err := IntExpression(big.NewInt(0))
testutils.CheckErr(t, false, "", err)
assert.Equal(t, "exprtZBwZUeYYYfUs9B9Rg2ywHezVHnCCnmF9WsDQVrs582dSK63dC", val)
}

func Test_Blake2B(t *testing.T) {
val, err := BlakeHash("050000")
testutils.CheckErr(t, false, "", err)
assert.Equal(t, "053f610929e2b6ea458c54dfd8b29716d379c13f5c8fd82d5c793a9e31271743", hex.EncodeToString(val))
}
Loading