Skip to content

Commit

Permalink
use crypto/rand; add function get ChainID by core.ID (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilya-korotya authored Sep 16, 2024
1 parent 9c64d6a commit d2955e4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
6 changes: 5 additions & 1 deletion chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package core

import (
"bytes"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"math"
"math/big"
"math/rand"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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()
}

0 comments on commit d2955e4

Please sign in to comment.