Skip to content

Commit

Permalink
Merge pull request #8 from iden3/iden-148/field-as-big-int
Browse files Browse the repository at this point in the history
Support big integers (as strings)
  • Loading branch information
vmidyllic authored Jan 14, 2022
2 parents 7825ac8 + 86012f1 commit 9fc18ab
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 35 deletions.
32 changes: 16 additions & 16 deletions json/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ func TestGetSerializedData(t *testing.T) {
indexValues: []uint32{1, 2, 3, 4, 5, 6, 7, 8},
expected: processor.ParsedSlots{
IndexA: []byte{
1, 0, 0, 0,
2, 0, 0, 0,
3, 0, 0, 0,
4, 0, 0, 0,
5, 0, 0, 0,
6, 0, 0, 0,
7, 0, 0, 0,
8, 0, 0, 0,
1,
2,
3,
4,
5,
6,
7,
8,
},
IndexB: []byte{},
ValueA: []byte{},
Expand Down Expand Up @@ -82,14 +82,14 @@ func TestGetSerializedData(t *testing.T) {
IndexA: []byte{},
IndexB: []byte{},
ValueA: []byte{
1, 0, 0, 0,
2, 0, 0, 0,
3, 0, 0, 0,
4, 0, 0, 0,
5, 0, 0, 0,
6, 0, 0, 0,
7, 0, 0, 0,
8, 0, 0, 0,
1,
2,
3,
4,
5,
6,
7,
8,
},
ValueB: []byte{},
},
Expand Down
49 changes: 46 additions & 3 deletions processor/json-ld/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package jsonld

import (
commonJSON "encoding/json"
"github.com/iden3/go-iden3-crypto/utils"
"github.com/iden3/go-schema-processor/json"
jsonld "github.com/iden3/go-schema-processor/json-ld"
"github.com/iden3/go-schema-processor/loaders"
"github.com/iden3/go-schema-processor/processor"
"github.com/stretchr/testify/assert"
"math/big"
"testing"
)

Expand Down Expand Up @@ -36,7 +38,7 @@ func TestParserWithSimpleData(t *testing.T) {
assert.Nil(t, err)

parsedData, err := jsonLdProcessor.ParseSlots(dataBytes, schema)
expetctedSlotA := []uint8{24, 0, 0, 0, 4, 0, 0, 0, 204, 7, 0, 0, 1, 0, 0, 0}
expetctedSlotA := []uint8{24, 4, 204, 7, 1}

t.Log(parsedData.IndexA)
assert.Nil(t, err)
Expand Down Expand Up @@ -73,7 +75,7 @@ func TestParserWithPositionedData(t *testing.T) {

parsedData, err := jsonLdProcessor.ParseSlots(dataBytes, schema)

exptectedSlotA := []uint8{24, 0, 0, 0, 4, 0, 0, 0, 204, 7, 0, 0, 1, 0, 0, 0}
exptectedSlotA := []uint8{24, 4, 204, 7, 1}

assert.Nil(t, err)
assert.NotEmpty(t, parsedData.IndexA)
Expand Down Expand Up @@ -195,7 +197,7 @@ func TestParserWithSlotsTypes(t *testing.T) {
assert.Nil(t, err)
t.Log(parsedData.IndexA)
expetctedSlotA := []uint8{101, 63, 98, 49}
expetctedSlotB := []uint8{1, 0, 0, 0}
expetctedSlotB := []uint8{1}

t.Log(parsedData.IndexA)
assert.Nil(t, err)
Expand Down Expand Up @@ -236,3 +238,44 @@ func TestGetFieldIndexWithSlotsTypes(t *testing.T) {
assert.Equal(t, 3, slot3)

}

func TestParserForBigIntegers(t *testing.T) {

url = "https://raw.githubusercontent.com/iden3/claim-schema-vocab/main/schemas/json-ld/auth.json-ld"

loader := loaders.HTTP{}
validator := json.Validator{}
parser := jsonld.Parser{ClaimType: "AuthBJJCredential", ParsingStrategy: processor.OneFieldPerSlotStrategy}

jsonLdProcessor := New(processor.WithValidator(validator), processor.WithParser(parser), processor.WithSchemaLoader(loader))
schema, ext, err := jsonLdProcessor.Load(url)

assert.Nil(t, err)
assert.Equal(t, ext, "json-ld")
assert.NotEmpty(t, schema)

data := make(map[string]interface{})

data["x"] = "12747559771369266961976321746772881814229091957322087014312756428846389160887"
data["y"] = "7732074634595480184356588475330446395691728690271550550016720788712795268212"

dataBytes, err := commonJSON.Marshal(data)
assert.Nil(t, err)

parsedData, err := jsonLdProcessor.ParseSlots(dataBytes, schema)
assert.Nil(t, err)

x, _ := new(big.Int).SetString(data["x"].(string), 10)
y, _ := new(big.Int).SetString(data["y"].(string), 10)

expetctedSlotA := utils.SwapEndianness(x.Bytes())
expetctedSlotB := utils.SwapEndianness(y.Bytes())

assert.Nil(t, err)
assert.NotEmpty(t, parsedData.IndexA)
assert.Equal(t, expetctedSlotA, parsedData.IndexA)
assert.Equal(t, expetctedSlotB, parsedData.IndexB)

assert.Empty(t, parsedData.ValueA)
assert.Empty(t, parsedData.ValueB)
}
31 changes: 15 additions & 16 deletions utils/claims.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package utils

import (
"encoding/binary"
"encoding/json"
"fmt"
core "github.com/iden3/go-iden3-core"
"github.com/iden3/go-schema-processor/processor"
"github.com/iden3/go-schema-processor/verifiable"
"github.com/pkg/errors"
"math/big"
"strconv"
)

var q *big.Int
Expand All @@ -27,24 +25,25 @@ func init() {
// FieldToByteArray convert fields to byte representation based on type
func FieldToByteArray(field interface{}) ([]byte, error) {

var bigIntField *big.Int
var ok bool

switch v := field.(type) {
case uint32:
bs := make([]byte, 4)
binary.LittleEndian.PutUint32(bs, v)
return bs, nil
case string:
bigIntField, ok = new(big.Int).SetString(v, 10)
if !ok {
return nil, errors.New("can't convert string to big int")
}
case float64:
s := fmt.Sprintf("%.0f", v)
intValue, err := strconv.Atoi(s)
if err != nil {
return nil, fmt.Errorf("can not convert field %v to uint32", field)
stringField := fmt.Sprintf("%.0f", v)
bigIntField, ok = new(big.Int).SetString(stringField, 10)
if !ok {
return nil, errors.New("can't convert string to big int")
}

bs := make([]byte, 4)
binary.LittleEndian.PutUint32(bs, uint32(intValue))
return bs, nil
default:
return nil, errors.New("field type is not supported")
}

return nil, fmt.Errorf("not supported field type %T", field)
return swapEndianness(bigIntField.Bytes()), nil
}

// DataFillsSlot checks if newData fills into slot capacity ()
Expand Down

0 comments on commit 9fc18ab

Please sign in to comment.