Skip to content

Commit

Permalink
Support U256 type
Browse files Browse the repository at this point in the history
  • Loading branch information
freehere107 committed Sep 18, 2024
1 parent a19e2c1 commit 5d3fdc1
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
2 changes: 1 addition & 1 deletion source/base.go

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion source/base/definitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@
"RewardPoint": "u32",
"CodeHash": "Hash",
"gas": "u64",
"U256": "[u8; 32]",
"H1024": "[u8; 128]",
"H2048": "[u8; 256]",
"AccountId32": "[u8; 32]",
Expand Down
44 changes: 44 additions & 0 deletions types/Uint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package types
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"math/big"
"reflect"
"strings"

"github.com/huandu/xstrings"
"github.com/itering/scale.go/utiles"
Expand Down Expand Up @@ -193,3 +196,44 @@ func (u *U128) Encode(value interface{}) string {
func (u *U128) TypeStructString() string {
return "U128"
}

type U256 struct {
ScaleDecoder
}

func (u *U256) Process() {
u.Value = utiles.AddHex(utiles.BytesToHex(u.NextBytes(32)))
}

func (u *U256) Encode(value interface{}) string {
var raw string
if reflect.TypeOf(value).Kind() == reflect.String && value.(string) == "" {
return ""
}
switch reflect.TypeOf(value).String() {
case reflect.Slice.String():
s := reflect.ValueOf(value)
if s.Len() != 32 {
panic("fixed length not match")
}
for i := 0; i < s.Len(); i++ {
raw += EncodeWithOpt("U8", s.Index(i).Interface(), nil)
}
return raw
case reflect.String.String():
valueStr := value.(string)
if strings.HasPrefix(valueStr, "0x") {
return utiles.TrimHex(valueStr)
} else {
return utiles.BytesToHex([]byte(valueStr))
}
case "decimal.Decimal":
value = value.(decimal.Decimal).BigInt()
fallthrough
case "*big.Int":
bigVal := fmt.Sprintf("%064s", value.(*big.Int).Text(16))
return utiles.BytesToHex(utiles.ReverseBytes(utiles.HexToBytes(bigVal)))
default:
panic(fmt.Errorf("invalid vec input"))
}
}
1 change: 1 addition & 0 deletions types/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func regBaseType() {
&U16{},
&U32{},
&U64{},
&U256{},
&Float64{},
&Float32{},
&U128{},
Expand Down
8 changes: 8 additions & 0 deletions types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,11 @@ func TestFixedArray(t *testing.T) {
assert.Equal(t, Encode(ts[i], v), "02020202020202020202020202020202", "TestFixedArray Encode fail %s", ts[i])
}
}

func TestU256(t *testing.T) {
raw := "0x1001000000000000000000000000000000000000000000000000000000000000"
m := ScaleDecoder{}
m.Init(scaleBytes.ScaleBytes{Data: utiles.HexToBytes(raw)}, nil)
assert.Equal(t, raw, utiles.AddHex(Encode("U256", utiles.U256DecoderToBigInt(m.ProcessAndUpdateData("U256").(string)))))
assert.Equal(t, raw, utiles.AddHex(Encode("U256", "0x1001000000000000000000000000000000000000000000000000000000000000")))
}
5 changes: 5 additions & 0 deletions utiles/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,8 @@ func DecimalFromInterface(i interface{}) decimal.Decimal {
}
return decimal.Zero
}

func U256DecoderToBigInt(u256 string) *big.Int {
reverseData := ReverseBytes(HexToBytes(u256))
return U256(BytesToHex(reverseData))
}

0 comments on commit 5d3fdc1

Please sign in to comment.