This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support 0x-encoded integer ChainID for web3 integration
- Moved the web3 hex and ChainID handling code to encoding to prevent import cycles Signed-off-by: Silas Davis <[email protected]>
- Loading branch information
Silas Davis
committed
Mar 18, 2021
1 parent
0e91dad
commit 8d6630d
Showing
22 changed files
with
332 additions
and
289 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package encoding | ||
|
||
import ( | ||
"math/big" | ||
"strings" | ||
|
||
"github.com/hyperledger/burrow/encoding/web3hex" | ||
) | ||
|
||
// Convert Burrow's ChainID to a *big.Int so it can be used as a nonce for Ethereum signing. | ||
// For compatibility with Ethereum tooling this function first tries to interpret the ChainID as an integer encoded | ||
// either as an eth-style 0x-prefixed hex string or a base 10 integer, falling back to interpreting the string's | ||
// raw bytes as a big-endian integer | ||
func GetEthChainID(chainID string) *big.Int { | ||
if strings.HasPrefix(chainID, "0x") { | ||
d := new(web3hex.Decoder) | ||
b := d.BigInt(chainID) | ||
if d.Err() == nil { | ||
return b | ||
} | ||
} | ||
b := new(big.Int) | ||
id, ok := b.SetString(chainID, 10) | ||
if ok { | ||
return id | ||
} | ||
return b.SetBytes([]byte(chainID)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package encoding | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetEthChainID(t *testing.T) { | ||
assert.Equal(t, big.NewInt(1234), GetEthChainID("1234")) | ||
assert.Equal(t, big.NewInt(1234), GetEthChainID("0x4d2")) | ||
chainID, ok := new(big.Int).SetString("28980219985052679991929851741845949978287371722649499714751652210", 10) | ||
require.True(t, ok) | ||
assert.Equal(t, chainID, GetEthChainID("FrogsEatApplesOnlyWhenClear")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package web3hex | ||
|
||
import ( | ||
"fmt" | ||
"math/big" | ||
"strings" | ||
|
||
"github.com/hyperledger/burrow/crypto" | ||
"github.com/tmthrgd/go-hex" | ||
) | ||
|
||
type Decoder struct { | ||
error | ||
must bool | ||
} | ||
|
||
func (d *Decoder) Must() *Decoder { | ||
return &Decoder{must: true} | ||
} | ||
|
||
func (d *Decoder) Err() error { | ||
return d.error | ||
} | ||
|
||
func (d *Decoder) pushErr(err error) { | ||
if d.must { | ||
panic(err) | ||
} | ||
if d.error == nil { | ||
d.error = err | ||
} | ||
} | ||
|
||
func (d *Decoder) Bytes(hs string) []byte { | ||
hexString := strings.TrimPrefix(hs, "0x") | ||
// Ethereum returns odd-length hexString strings when it removes leading zeros | ||
if len(hexString)%2 == 1 { | ||
hexString = "0" + hexString | ||
} | ||
bs, err := hex.DecodeString(hexString) | ||
if err != nil { | ||
d.pushErr(fmt.Errorf("could not decode bytes from '%s': %w", hs, err)) | ||
} | ||
return bs | ||
} | ||
|
||
func (d *Decoder) Address(hs string) crypto.Address { | ||
if hs == "" { | ||
return crypto.Address{} | ||
} | ||
address, err := crypto.AddressFromBytes(d.Bytes(hs)) | ||
if err != nil { | ||
d.pushErr(fmt.Errorf("could not decode address from '%s': %w", hs, err)) | ||
} | ||
return address | ||
} | ||
|
||
func (d *Decoder) BigInt(hs string) *big.Int { | ||
return new(big.Int).SetBytes(d.Bytes(hs)) | ||
} | ||
|
||
func (d *Decoder) Uint64(hs string) uint64 { | ||
bi := d.BigInt(hs) | ||
if !bi.IsUint64() { | ||
d.pushErr(fmt.Errorf("%v is not uint64", bi)) | ||
} | ||
return bi.Uint64() | ||
} | ||
|
||
func (d *Decoder) Int64(hs string) int64 { | ||
bi := d.BigInt(hs) | ||
if !bi.IsInt64() { | ||
d.pushErr(fmt.Errorf("%v is not int64", bi)) | ||
} | ||
return bi.Int64() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package web3hex | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDecoder_Bytes(t *testing.T) { | ||
d := new(Decoder) | ||
assert.Equal(t, []byte{}, d.Bytes("")) | ||
assert.Equal(t, []byte{1}, d.Bytes("0x1")) | ||
assert.Equal(t, []byte{1}, d.Bytes("0x01")) | ||
assert.Equal(t, []byte{1, 0xff}, d.Bytes("0x1ff")) | ||
require.NoError(t, d.Err()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package web3hex | ||
|
||
import ( | ||
bin "encoding/binary" | ||
"math/big" | ||
"strings" | ||
|
||
"github.com/hyperledger/burrow/crypto" | ||
"github.com/tmthrgd/go-hex" | ||
) | ||
|
||
type encoder struct { | ||
} | ||
|
||
var Encoder = new(encoder) | ||
|
||
func (e *encoder) Bytes(bs []byte) string { | ||
return "0x" + hex.EncodeToString(bs) | ||
} | ||
|
||
func (e *encoder) BytesTrim(bs []byte) string { | ||
if len(bs) == 0 { | ||
return "" | ||
} | ||
str := hex.EncodeToString(bs) | ||
// Ethereum expects leading zeros to be removed from RLP encodings (SMH) | ||
str = strings.TrimLeft(str, "0") | ||
if len(str) == 0 { | ||
// Special case for zero | ||
return "0x0" | ||
} | ||
return "0x" + str | ||
} | ||
|
||
func (e *encoder) BigInt(x *big.Int) string { | ||
return e.BytesTrim(x.Bytes()) | ||
} | ||
|
||
func (e *encoder) Uint64OmitEmpty(x uint64) string { | ||
if x == 0 { | ||
return "" | ||
} | ||
return e.Uint64(x) | ||
} | ||
|
||
func (e *encoder) Uint64(x uint64) string { | ||
bs := make([]byte, 8) | ||
bin.BigEndian.PutUint64(bs, x) | ||
return e.BytesTrim(bs) | ||
} | ||
|
||
func (e *encoder) Address(address crypto.Address) string { | ||
return e.BytesTrim(address.Bytes()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package web3hex | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEncoder_BytesTrim(t *testing.T) { | ||
assert.Equal(t, "", Encoder.BytesTrim(nil)) | ||
assert.Equal(t, "", Encoder.BytesTrim([]byte{})) | ||
assert.Equal(t, "0x0", Encoder.BytesTrim([]byte{0})) | ||
assert.Equal(t, "0x1", Encoder.BytesTrim([]byte{1})) | ||
assert.Equal(t, "0x1ff", Encoder.BytesTrim([]byte{1, 255})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.