Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use crypto/rand; add function get ChainID by core.ID #466

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
Loading