From 505a4ee48b03b61ae7304672c14e2d9de9f7345e Mon Sep 17 00:00:00 2001 From: ilya-korotya Date: Mon, 16 Sep 2024 17:39:22 +0200 Subject: [PATCH] use crypto/rand; add function get ChainID by core.ID --- chain.go | 6 +++++- claim_test.go | 16 +++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/chain.go b/chain.go index 9348786..37c4260 100644 --- a/chain.go +++ b/chain.go @@ -40,12 +40,16 @@ var chainIDs = map[chainIDKey]ChainID{ // ChainIDfromDID returns chain name from w3c.DID func ChainIDfromDID(did w3c.DID) (ChainID, error) { - id, err := IDFromDID(did) if err != nil { return 0, err } + return ChainIDfromID(id) +} + +// ChainIDfromID(id ID) returns chain name from ID +func ChainIDfromID(id ID) (ChainID, error) { blockchain, err := BlockchainFromID(id) if err != nil { return 0, err diff --git a/claim_test.go b/claim_test.go index 1ee64cd..7dcf1b0 100644 --- a/claim_test.go +++ b/claim_test.go @@ -2,12 +2,12 @@ package core import ( "bytes" + "crypto/rand" "encoding/hex" "encoding/json" "fmt" "math" "math/big" - "math/rand" "strings" "testing" "time" @@ -109,24 +109,24 @@ func TestClaim_GetFlagUpdatable(t *testing.T) { func TestClaim_GetVersion(t *testing.T) { var sc SchemaHash - ver := uint32(rand.Int63n(math.MaxUint32)) + ver := uint32(getRandomNumber(t, big.NewInt(math.MaxUint32))) claim, err := NewClaim(sc, WithVersion(ver)) require.NoError(t, err) require.Equal(t, ver, claim.GetVersion()) - ver2 := uint32(rand.Int63n(math.MaxUint32)) + ver2 := uint32(getRandomNumber(t, big.NewInt(math.MaxUint32))) claim.SetVersion(ver2) require.Equal(t, ver2, claim.GetVersion()) } func TestClaim_GetRevocationNonce(t *testing.T) { var sc SchemaHash - nonce := uint64(rand.Int63()) + nonce := uint64(getRandomNumber(t, big.NewInt(math.MaxInt64))) claim, err := NewClaim(sc, WithRevocationNonce(nonce)) require.NoError(t, err) require.Equal(t, nonce, claim.GetRevocationNonce()) - nonce2 := uint64(rand.Int63()) + nonce2 := uint64(getRandomNumber(t, big.NewInt(math.MaxInt64))) claim.SetRevocationNonce(nonce2) require.Equal(t, nonce2, claim.GetRevocationNonce()) } @@ -731,3 +731,9 @@ func TestNewSchemaHashFromInt(t *testing.T) { }) } } + +func getRandomNumber(t *testing.T, max *big.Int) int64 { + n, err := rand.Int(rand.Reader, max) + require.NoError(t, err) + return n.Int64() +}