Repository: summa-tx/bitcoin-spv (language: rust)

Issue:
Remove `hex` dependency from `bitcoin_spv.go`
I don't like that we rely on string (de)serialization in bitcoin spv. we should figure out alternative elegant ways of doing this


Discussion / hints:
I've been trying to figure this one out.  In the test, we are trying to convert a very large number, but `sdk.newUint()` only accepts `uint64`.  So basically, I think I can convert it to the right number, but then I can't convert the type to sdk uint.

Fix patch:
diff --git a/golang/btcspv/bitcoin_spv.go b/golang/btcspv/bitcoin_spv.go
index cf3d127f..3bc5cfbf 100644
--- a/golang/btcspv/bitcoin_spv.go
+++ b/golang/btcspv/bitcoin_spv.go
@@ -4,7 +4,6 @@ import (
 	"bytes"
 	"crypto/sha256"
 	"encoding/binary"
-	"encoding/hex"
 	"errors"
 	"math/big"
 
@@ -24,16 +23,10 @@ func BytesToUint(b []byte) uint {
 	return total
 }
 
-// BytesToBigInt converts a bytestring to a cosmos-sdk Int
-func BytesToBigInt(b []byte) sdk.Uint {
-	ret := sdk.NewUintFromString("0x" + hex.EncodeToString(b))
-	return ret
-}
-
 // BytesToBigUint converts a bytestring of up to 32 bytes to a cosmos sdk uint
 func BytesToBigUint(b []byte) sdk.Uint {
-	ret := sdk.NewUintFromString("0x" + hex.EncodeToString(b))
-	return ret
+	myInt := new(big.Int).SetBytes(b)
+	return sdk.NewUintFromBigInt(myInt)
 }
 
 // DetermineVarIntDataLength extracts the payload length of a Bitcoin VarInt
@@ -367,7 +360,7 @@ func ExtractTarget(header RawHeader) sdk.Uint {
 	e := sdk.NewInt(int64(header[75]))
 
 	// hacks
-	mantissa := sdk.NewUintFromString("0x" + hex.EncodeToString(ReverseEndianness(m)))
+	mantissa := BytesToBigUint(ReverseEndianness(m))
 	exponent := e.SubRaw(3)
 
 	// Have to convert to underlying big.Int as the sdk does not expose exponentiation


Test patch:
diff --git a/golang/btcspv/bitcoin_spv_test.go b/golang/btcspv/bitcoin_spv_test.go
index b7d73ebf..121a5ef6 100644
--- a/golang/btcspv/bitcoin_spv_test.go
+++ b/golang/btcspv/bitcoin_spv_test.go
@@ -1,7 +1,6 @@
 package btcspv
 
 import (
-	"bytes"
 	"encoding/json"
 	"io/ioutil"
 	"log"
@@ -210,19 +209,14 @@ func (suite *UtilsSuite) TestBytesToUint() {
 	}
 }
 
-func (suite *UtilsSuite) TestBytesToBigInt() {
-	hexString := "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
+func (suite *UtilsSuite) TestBytesToBigUint() {
+	hexString := "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
 	decoded := DecodeIfHex(hexString)
 
-	buf := bytes.Buffer{}
-	buf.WriteString("0x")
-	buf.WriteString(hexString)
+	expected := sdk.NewUintFromString(hexString)
+	actual := BytesToBigUint(decoded)
 
-	expected := sdk.NewUintFromString(buf.String())
-
-	result := BytesToBigInt(decoded)
-
-	suite.True(expected.Equal(result))
+	suite.Equal(expected, actual)
 }
 
 func (suite *UtilsSuite) TestExtractSequenceWitness() {
@@ -542,7 +536,7 @@ func (suite *UtilsSuite) TestExtractTarget() {
 
 	for i := range fixture {
 		testCase := fixture[i]
-		expected := BytesToBigInt(testCase.Output.([]byte))
+		expected := BytesToBigUint(testCase.Output.([]byte))
 		actual := ExtractTarget(testCase.Input.(RawHeader))
 		suite.Equal(expected, actual)
 	}