Skip to content
Open
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
13 changes: 7 additions & 6 deletions dna_sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ import (
)

var (
testDnaSdk *DNASdk
testWallet *Wallet
testPasswd = []byte("123456")
testDefAcc *Account
testGasPrice = uint64(0)
testGasLimit = uint64(20000)
testDnaSdk *DNASdk
testWallet *Wallet
testPasswd = []byte("123456")
testDefAcc *Account
testGasPrice = uint64(0)
testGasLimit = uint64(20000)
testDidMethod = "tst"
)

func TestOntId_NewRegIDWithAttributesTransaction(t *testing.T) {
Expand Down
16 changes: 9 additions & 7 deletions identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

const (
SCHEME = "did"
METHOD = "ont"
VER = 0x41
)

Expand Down Expand Up @@ -180,8 +179,8 @@ type Identity struct {
scrypt *keypair.ScryptParam
}

func NewIdentity(scrypt *keypair.ScryptParam) (*Identity, error) {
id, err := GenerateID()
func NewIdentity(didMethod string, scrypt *keypair.ScryptParam) (*Identity, error) {
id, err := GenerateID(didMethod)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -355,16 +354,19 @@ type IdentityData struct {
scrypt *keypair.ScryptParam
}

func GenerateID() (string, error) {
func GenerateID(didMethod string) (string, error) {
var buf [32]byte
_, err := rand.Read(buf[:])
if err != nil {
return "", fmt.Errorf("generate ID error, %s", err)
}
return CreateID(buf[:])
return CreateID(didMethod, buf[:])
}

func CreateID(nonce []byte) (string, error) {
func CreateID(didMethod string, nonce []byte) (string, error) {
if len(didMethod) != 3 {
return "", fmt.Errorf("invalid length of didMethod")
}
hasher := ripemd160.New()
_, err := hasher.Write(nonce)
if err != nil {
Expand All @@ -379,7 +381,7 @@ func CreateID(nonce []byte) (string, error) {
return "", fmt.Errorf("create ID error, %s", err)
}

return SCHEME + ":" + METHOD + ":" + string(idstring), nil
return SCHEME + ":" + didMethod + ":" + string(idstring), nil
}

func VerifyID(id string) bool {
Expand Down
83 changes: 83 additions & 0 deletions native_auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright 2019 the DNA Dev team
// Copyright 2018 the Ontology Dev team
//
package DNA_go_sdk

import (
"fmt"
"testing"
"time"

"github.com/DNAProject/DNA-go-sdk/utils"
"github.com/DNAProject/DNA/smartcontract/service/native/common"
)

func TestNativeContract_DeployAuthContract(t *testing.T) {
// init, deploy did contract, update did method
TestNativeContract_DeployDIDContract(t)

// depoly auth contract
initParamStr, err := GenerateID(testDidMethod)
if err != nil {
t.Errorf("generate ID in deploy auth native contract: %s", err)
return
}
txHash, err := testDnaSdk.Native.DeployNativeContract(testGasPrice, testGasLimit, testDefAcc,
common.AuthContractAddress, []byte(initParamStr),
"testname", "1.0", "testAuthor", "test@example.com", "test deploying native Auth")
testDnaSdk.WaitForGenerateBlock(30*time.Second, 1)
event, err := testDnaSdk.GetSmartContractEvent(txHash.ToHexString())
if err != nil {
t.Errorf("test deploy auth native contract error: %s", err)
return
}
fmt.Printf("test deploy native contract event: %+v\n", event)
for _, ev := range event.Notify {
fmt.Printf("contract: %s, state: %v\n", ev.ContractAddress, ev.States)
authAddr, err := utils.AddressFromHexString(ev.ContractAddress)
if err != nil {
t.Errorf("failed to parse auth addr: %s", err)
}
testDnaSdk.Native.AuthContractAddr = authAddr
t.Logf("update Auth contract to %s", ev.ContractAddress)
}

TestAuth_InitAuth(t)
TestAuth_InitContractAdmin(t)
}

func TestAuth_InitAuth(t *testing.T) {
txHash, err := testDnaSdk.Native.Auth.InitAuth(testGasPrice, testGasLimit, testDefAcc)
if err != nil {
t.Errorf("TestAuth_InitAuth, init auth err: %s", err)
return
}
testDnaSdk.WaitForGenerateBlock(30*time.Second, 1)
event, err := testDnaSdk.GetSmartContractEvent(txHash.ToHexString())
if err != nil {
t.Errorf("TestAuth_InitAuth get smart contract event: %s", err)
}
fmt.Printf("TestAuth_InitAuth event: %v\n", event)
}

func TestAuth_InitContractAdmin(t *testing.T) {
// create auth admin
testAuthAdmin, err := testWallet.NewDefaultSettingIdentity(testDidMethod, testPasswd)
if err != nil {
fmt.Printf("create auth admin identity err: %s \n", err)
return
}

txHash, err := testDnaSdk.Native.Auth.InitContractAdmin(testGasPrice, testGasLimit, testDefAcc, []byte(testAuthAdmin.ID))
if err != nil {
t.Errorf("TestAuth_InitContractAdmin init contract admin %s: %s", testAuthAdmin.ID, err)
return
}
testDnaSdk.WaitForGenerateBlock(30*time.Second, 1)
event, err := testDnaSdk.GetSmartContractEvent(txHash.ToHexString())
if err != nil {
t.Errorf("TestAuth_InitContractAdmin get smart contract event: %s", err)
}
fmt.Printf("TestAuth_InitContractAdmin event: %v\n", event)
}
Loading