diff --git a/dna_sdk_test.go b/dna_sdk_test.go index 09cc9b5..8e58e59 100644 --- a/dna_sdk_test.go +++ b/dna_sdk_test.go @@ -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) { diff --git a/identity.go b/identity.go index 5ae0308..77e2ff4 100644 --- a/identity.go +++ b/identity.go @@ -20,7 +20,6 @@ import ( const ( SCHEME = "did" - METHOD = "ont" VER = 0x41 ) @@ -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 } @@ -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 { @@ -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 { diff --git a/native_auth_test.go b/native_auth_test.go new file mode 100644 index 0000000..1006bf6 --- /dev/null +++ b/native_auth_test.go @@ -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) +} diff --git a/native_contract.go b/native_contract.go index 6e6e395..9139b2f 100644 --- a/native_contract.go +++ b/native_contract.go @@ -13,8 +13,10 @@ import ( "github.com/DNAProject/DNA-go-sdk/utils" "github.com/DNAProject/DNA/common" "github.com/DNAProject/DNA/common/serialization" + "github.com/DNAProject/DNA/core/payload" "github.com/DNAProject/DNA/core/types" cutils "github.com/DNAProject/DNA/core/utils" + common2 "github.com/DNAProject/DNA/smartcontract/service/native/common" "github.com/DNAProject/DNA/smartcontract/service/native/gas" "github.com/DNAProject/DNA/smartcontract/service/native/global_params" "github.com/ontio/ontology-crypto/keypair" @@ -22,17 +24,13 @@ import ( var ( GAS_CONTRACT_ADDRESS, _ = utils.AddressFromHexString("0200000000000000000000000000000000000000") - ONT_ID_CONTRACT_ADDRESS, _ = utils.AddressFromHexString("0300000000000000000000000000000000000000") GLOABL_PARAMS_CONTRACT_ADDRESS, _ = utils.AddressFromHexString("0400000000000000000000000000000000000000") - AUTH_CONTRACT_ADDRESS, _ = utils.AddressFromHexString("0600000000000000000000000000000000000000") GOVERNANCE_CONTRACT_ADDRESS, _ = utils.AddressFromHexString("0700000000000000000000000000000000000000") ) var ( GAS_CONTRACT_VERSION = byte(0) - ONT_ID_CONTRACT_VERSION = byte(0) GLOBAL_PARAMS_CONTRACT_VERSION = byte(0) - AUTH_CONTRACT_VERSION = byte(0) GOVERNANCE_CONTRACT_VERSION = byte(0) ) @@ -42,17 +40,29 @@ var OPCODE_IN_PAYLOAD = map[byte]bool{0xc6: true, 0x6b: true, 0x6a: true, 0xc8: type NativeContract struct { dnaSdk *DNASdk Gas *Gas - OntId *OntId + DID *DID GlobalParams *GlobalParam Auth *Auth + + // deployable native contracts + DIDContractAddr common.Address + AuthContractAddr common.Address + DIDContractVersion byte + AuthContractVersion byte } func newNativeContract(dnaSkd *DNASdk) *NativeContract { - native := &NativeContract{dnaSdk: dnaSkd} + native := &NativeContract{ + dnaSdk: dnaSkd, + DIDContractAddr: common2.DIDContractAddress, + AuthContractAddr: common2.AuthContractAddress, + DIDContractVersion: byte(0), + AuthContractVersion: byte(0), + } native.Gas = &Gas{native: native, dnaSkd: dnaSkd} - native.OntId = &OntId{native: native, dnaSkd: dnaSkd} + native.DID = &DID{native: native, dnaSdk: dnaSkd} native.GlobalParams = &GlobalParam{native: native, dnaSkd: dnaSkd} - native.Auth = &Auth{native: native, dnaSkd: dnaSkd} + native.Auth = &Auth{native: native, dnaSdk: dnaSkd} return native } @@ -78,10 +88,35 @@ func (this *NativeContract) NewNativeInvokeTransaction( return this.dnaSdk.NewInvokeTransaction(gasPrice, gasLimit, invokeCode), nil } +func (this *NativeContract) DeployNativeContract( + gasPrice, gasLimit uint64, + signer *Account, + baseNativeContract common.Address, + initParam []byte, + name, version, author, email, desp string, +) (common.Uint256, error) { + ndc := &payload.NativeDeployCode{ + BaseContractAddress: baseNativeContract, + InitParam: initParam, + } + sink := common.NewZeroCopySink(nil) + ndc.Serialization(sink) + + tx, err := cutils.NewDeployTransaction(sink.Bytes(), name, version, author, email, desp, payload.NATIVE_TYPE) + if err != nil { + return common.UINT256_EMPTY, err + } + if err := this.dnaSdk.SignToTransaction(tx, signer); err != nil { + return common.UINT256_EMPTY, err + } + + return this.dnaSdk.SendTransaction(tx) +} + func (this *NativeContract) InvokeNativeContract( gasPrice, gasLimit uint64, - singer *Account, + signer *Account, version byte, contractAddress common.Address, method string, @@ -91,7 +126,7 @@ func (this *NativeContract) InvokeNativeContract( if err != nil { return common.UINT256_EMPTY, err } - err = this.dnaSdk.SignToTransaction(tx, singer) + err = this.dnaSdk.SignToTransaction(tx, signer) if err != nil { return common.UINT256_EMPTY, err } @@ -111,6 +146,16 @@ func (this *NativeContract) PreExecInvokeNativeContract( return this.dnaSdk.PreExecTransaction(tx) } +func (this *NativeContract) SetDIDContract(addr common.Address, version byte) { + this.DIDContractAddr = addr + this.DIDContractVersion = version +} + +func (this *NativeContract) SetAuthContract(addr common.Address, version byte) { + this.AuthContractAddr = addr + this.AuthContractVersion = version +} + type Gas struct { dnaSkd *DNASdk native *NativeContract @@ -314,12 +359,36 @@ func (this *Gas) TotalSupply() (uint64, error) { return balance.Uint64(), nil } -type OntId struct { - dnaSkd *DNASdk +type DID struct { + dnaSdk *DNASdk native *NativeContract } -func (this *OntId) NewRegIDWithPublicKeyTransaction(gasPrice, gasLimit uint64, ontId string, pubKey keypair.PublicKey) (*types.MutableTransaction, error) { +func (this *DID) NewInitDIDTransaction(gasPrice, gasLimit uint64, didMethod string) (*types.MutableTransaction, error) { + return this.native.NewNativeInvokeTransaction( + gasPrice, + gasLimit, + this.native.DIDContractVersion, + this.native.DIDContractAddr, + "initDID", + []interface{}{ + []byte(didMethod), + }, + ) +} + +func (this *DID) InitDID(gasPrice, gasLimit uint64, signer *Account, didMethod string) (common.Uint256, error) { + tx, err := this.NewInitDIDTransaction(gasPrice, gasLimit, didMethod) + if err != nil { + return common.UINT256_EMPTY, err + } + if err := this.dnaSdk.SignToTransaction(tx, signer); err != nil { + return common.UINT256_EMPTY, err + } + return this.dnaSdk.SendTransaction(tx) +} + +func (this *DID) NewRegIDWithPublicKeyTransaction(gasPrice, gasLimit uint64, ontId string, pubKey keypair.PublicKey) (*types.MutableTransaction, error) { type regIDWithPublicKey struct { OntId string PubKey []byte @@ -327,8 +396,8 @@ func (this *OntId) NewRegIDWithPublicKeyTransaction(gasPrice, gasLimit uint64, o return this.native.NewNativeInvokeTransaction( gasPrice, gasLimit, - ONT_ID_CONTRACT_VERSION, - ONT_ID_CONTRACT_ADDRESS, + this.native.DIDContractVersion, + this.native.DIDContractAddr, "regIDWithPublicKey", []interface{}{ ®IDWithPublicKey{ @@ -339,23 +408,23 @@ func (this *OntId) NewRegIDWithPublicKeyTransaction(gasPrice, gasLimit uint64, o ) } -func (this *OntId) RegIDWithPublicKey(gasPrice, gasLimit uint64, signer *Account, ontId string, controller *Controller) (common.Uint256, error) { +func (this *DID) RegIDWithPublicKey(gasPrice, gasLimit uint64, signer *Account, ontId string, controller *Controller) (common.Uint256, error) { tx, err := this.NewRegIDWithPublicKeyTransaction(gasPrice, gasLimit, ontId, controller.PublicKey) if err != nil { return common.UINT256_EMPTY, err } - err = this.dnaSkd.SignToTransaction(tx, signer) + err = this.dnaSdk.SignToTransaction(tx, signer) if err != nil { return common.UINT256_EMPTY, err } - err = this.dnaSkd.SignToTransaction(tx, controller) + err = this.dnaSdk.SignToTransaction(tx, controller) if err != nil { return common.UINT256_EMPTY, err } - return this.dnaSkd.SendTransaction(tx) + return this.dnaSdk.SendTransaction(tx) } -func (this *OntId) NewRegIDWithAttributesTransaction(gasPrice, gasLimit uint64, ontId string, pubKey keypair.PublicKey, attributes []*DDOAttribute) (*types.MutableTransaction, error) { +func (this *DID) NewRegIDWithAttributesTransaction(gasPrice, gasLimit uint64, ontId string, pubKey keypair.PublicKey, attributes []*DDOAttribute) (*types.MutableTransaction, error) { type regIDWithAttribute struct { OntId string PubKey []byte @@ -364,8 +433,8 @@ func (this *OntId) NewRegIDWithAttributesTransaction(gasPrice, gasLimit uint64, return this.native.NewNativeInvokeTransaction( gasPrice, gasLimit, - ONT_ID_CONTRACT_VERSION, - ONT_ID_CONTRACT_ADDRESS, + this.native.DIDContractVersion, + this.native.DIDContractAddr, "regIDWithAttributes", []interface{}{ ®IDWithAttribute{ @@ -377,26 +446,26 @@ func (this *OntId) NewRegIDWithAttributesTransaction(gasPrice, gasLimit uint64, ) } -func (this *OntId) RegIDWithAttributes(gasPrice, gasLimit uint64, signer *Account, ontId string, controller *Controller, attributes []*DDOAttribute) (common.Uint256, error) { +func (this *DID) RegIDWithAttributes(gasPrice, gasLimit uint64, signer *Account, ontId string, controller *Controller, attributes []*DDOAttribute) (common.Uint256, error) { tx, err := this.NewRegIDWithAttributesTransaction(gasPrice, gasLimit, ontId, controller.PublicKey, attributes) if err != nil { return common.UINT256_EMPTY, err } - err = this.dnaSkd.SignToTransaction(tx, signer) + err = this.dnaSdk.SignToTransaction(tx, signer) if err != nil { return common.UINT256_EMPTY, err } - err = this.dnaSkd.SignToTransaction(tx, controller) + err = this.dnaSdk.SignToTransaction(tx, controller) if err != nil { return common.UINT256_EMPTY, err } - return this.dnaSkd.SendTransaction(tx) + return this.dnaSdk.SendTransaction(tx) } -func (this *OntId) GetDDO(ontId string) (*DDO, error) { +func (this *DID) GetDDO(ontId string) (*DDO, error) { result, err := this.native.PreExecInvokeNativeContract( - ONT_ID_CONTRACT_ADDRESS, - ONT_ID_CONTRACT_VERSION, + this.native.DIDContractAddr, + this.native.DIDContractVersion, "getDDO", []interface{}{ontId}, ) @@ -443,7 +512,7 @@ func (this *OntId) GetDDO(ontId string) (*DDO, error) { return ddo, nil } -func (this *OntId) NewAddKeyTransaction(gasPrice, gasLimit uint64, ontId string, newPubKey, pubKey keypair.PublicKey) (*types.MutableTransaction, error) { +func (this *DID) NewAddKeyTransaction(gasPrice, gasLimit uint64, ontId string, newPubKey, pubKey keypair.PublicKey) (*types.MutableTransaction, error) { type addKey struct { OntId string NewPubKey []byte @@ -452,8 +521,8 @@ func (this *OntId) NewAddKeyTransaction(gasPrice, gasLimit uint64, ontId string, return this.native.NewNativeInvokeTransaction( gasPrice, gasLimit, - ONT_ID_CONTRACT_VERSION, - ONT_ID_CONTRACT_ADDRESS, + this.native.DIDContractVersion, + this.native.DIDContractAddr, "addKey", []interface{}{ &addKey{ @@ -464,23 +533,23 @@ func (this *OntId) NewAddKeyTransaction(gasPrice, gasLimit uint64, ontId string, }) } -func (this *OntId) AddKey(gasPrice, gasLimit uint64, ontId string, signer *Account, newPubKey keypair.PublicKey, controller *Controller) (common.Uint256, error) { +func (this *DID) AddKey(gasPrice, gasLimit uint64, ontId string, signer *Account, newPubKey keypair.PublicKey, controller *Controller) (common.Uint256, error) { tx, err := this.NewAddKeyTransaction(gasPrice, gasLimit, ontId, newPubKey, controller.PublicKey) if err != nil { return common.UINT256_EMPTY, err } - err = this.dnaSkd.SignToTransaction(tx, signer) + err = this.dnaSdk.SignToTransaction(tx, signer) if err != nil { return common.UINT256_EMPTY, err } - err = this.dnaSkd.SignToTransaction(tx, controller) + err = this.dnaSdk.SignToTransaction(tx, controller) if err != nil { return common.UINT256_EMPTY, err } - return this.dnaSkd.SendTransaction(tx) + return this.dnaSdk.SendTransaction(tx) } -func (this *OntId) NewRevokeKeyTransaction(gasPrice, gasLimit uint64, ontId string, removedPubKey, pubKey keypair.PublicKey) (*types.MutableTransaction, error) { +func (this *DID) NewRevokeKeyTransaction(gasPrice, gasLimit uint64, ontId string, removedPubKey, pubKey keypair.PublicKey) (*types.MutableTransaction, error) { type removeKey struct { OntId string RemovedKey []byte @@ -489,8 +558,8 @@ func (this *OntId) NewRevokeKeyTransaction(gasPrice, gasLimit uint64, ontId stri return this.native.NewNativeInvokeTransaction( gasPrice, gasLimit, - ONT_ID_CONTRACT_VERSION, - ONT_ID_CONTRACT_ADDRESS, + this.native.DIDContractVersion, + this.native.DIDContractAddr, "removeKey", []interface{}{ &removeKey{ @@ -502,23 +571,23 @@ func (this *OntId) NewRevokeKeyTransaction(gasPrice, gasLimit uint64, ontId stri ) } -func (this *OntId) RevokeKey(gasPrice, gasLimit uint64, ontId string, signer *Account, removedPubKey keypair.PublicKey, controller *Controller) (common.Uint256, error) { +func (this *DID) RevokeKey(gasPrice, gasLimit uint64, ontId string, signer *Account, removedPubKey keypair.PublicKey, controller *Controller) (common.Uint256, error) { tx, err := this.NewRevokeKeyTransaction(gasPrice, gasLimit, ontId, removedPubKey, controller.PublicKey) if err != nil { return common.UINT256_EMPTY, err } - err = this.dnaSkd.SignToTransaction(tx, signer) + err = this.dnaSdk.SignToTransaction(tx, signer) if err != nil { return common.UINT256_EMPTY, err } - err = this.dnaSkd.SignToTransaction(tx, controller) + err = this.dnaSdk.SignToTransaction(tx, controller) if err != nil { return common.UINT256_EMPTY, err } - return this.dnaSkd.SendTransaction(tx) + return this.dnaSdk.SendTransaction(tx) } -func (this *OntId) NewSetRecoveryTransaction(gasPrice, gasLimit uint64, ontId string, recovery common.Address, pubKey keypair.PublicKey) (*types.MutableTransaction, error) { +func (this *DID) NewSetRecoveryTransaction(gasPrice, gasLimit uint64, ontId string, recovery common.Address, pubKey keypair.PublicKey) (*types.MutableTransaction, error) { type addRecovery struct { OntId string Recovery common.Address @@ -527,8 +596,8 @@ func (this *OntId) NewSetRecoveryTransaction(gasPrice, gasLimit uint64, ontId st return this.native.NewNativeInvokeTransaction( gasPrice, gasLimit, - ONT_ID_CONTRACT_VERSION, - ONT_ID_CONTRACT_ADDRESS, + this.native.DIDContractVersion, + this.native.DIDContractAddr, "addRecovery", []interface{}{ &addRecovery{ @@ -539,23 +608,23 @@ func (this *OntId) NewSetRecoveryTransaction(gasPrice, gasLimit uint64, ontId st }) } -func (this *OntId) SetRecovery(gasPrice, gasLimit uint64, signer *Account, ontId string, recovery common.Address, controller *Controller) (common.Uint256, error) { +func (this *DID) SetRecovery(gasPrice, gasLimit uint64, signer *Account, ontId string, recovery common.Address, controller *Controller) (common.Uint256, error) { tx, err := this.NewSetRecoveryTransaction(gasPrice, gasLimit, ontId, recovery, controller.PublicKey) if err != nil { return common.UINT256_EMPTY, err } - err = this.dnaSkd.SignToTransaction(tx, signer) + err = this.dnaSdk.SignToTransaction(tx, signer) if err != nil { return common.UINT256_EMPTY, err } - err = this.dnaSkd.SignToTransaction(tx, controller) + err = this.dnaSdk.SignToTransaction(tx, controller) if err != nil { return common.UINT256_EMPTY, err } - return this.dnaSkd.SendTransaction(tx) + return this.dnaSdk.SendTransaction(tx) } -func (this *OntId) NewChangeRecoveryTransaction(gasPrice, gasLimit uint64, ontId string, newRecovery, oldRecovery common.Address) (*types.MutableTransaction, error) { +func (this *DID) NewChangeRecoveryTransaction(gasPrice, gasLimit uint64, ontId string, newRecovery, oldRecovery common.Address) (*types.MutableTransaction, error) { type changeRecovery struct { OntId string NewRecovery common.Address @@ -564,8 +633,8 @@ func (this *OntId) NewChangeRecoveryTransaction(gasPrice, gasLimit uint64, ontId return this.native.NewNativeInvokeTransaction( gasPrice, gasLimit, - ONT_ID_CONTRACT_VERSION, - ONT_ID_CONTRACT_ADDRESS, + this.native.DIDContractVersion, + this.native.DIDContractAddr, "changeRecovery", []interface{}{ &changeRecovery{ @@ -576,23 +645,23 @@ func (this *OntId) NewChangeRecoveryTransaction(gasPrice, gasLimit uint64, ontId }) } -func (this *OntId) ChangeRecovery(gasPrice, gasLimit uint64, signer *Account, ontId string, newRecovery, oldRecovery common.Address, controller *Controller) (common.Uint256, error) { +func (this *DID) ChangeRecovery(gasPrice, gasLimit uint64, signer *Account, ontId string, newRecovery, oldRecovery common.Address, controller *Controller) (common.Uint256, error) { tx, err := this.NewChangeRecoveryTransaction(gasPrice, gasLimit, ontId, newRecovery, oldRecovery) if err != nil { return common.UINT256_EMPTY, err } - err = this.dnaSkd.SignToTransaction(tx, signer) + err = this.dnaSdk.SignToTransaction(tx, signer) if err != nil { return common.UINT256_EMPTY, err } - err = this.dnaSkd.SignToTransaction(tx, controller) + err = this.dnaSdk.SignToTransaction(tx, controller) if err != nil { return common.UINT256_EMPTY, err } - return this.dnaSkd.SendTransaction(tx) + return this.dnaSdk.SendTransaction(tx) } -func (this *OntId) NewAddAttributesTransaction(gasPrice, gasLimit uint64, ontId string, attributes []*DDOAttribute, pubKey keypair.PublicKey) (*types.MutableTransaction, error) { +func (this *DID) NewAddAttributesTransaction(gasPrice, gasLimit uint64, ontId string, attributes []*DDOAttribute, pubKey keypair.PublicKey) (*types.MutableTransaction, error) { type addAttributes struct { OntId string Attributes []*DDOAttribute @@ -601,8 +670,8 @@ func (this *OntId) NewAddAttributesTransaction(gasPrice, gasLimit uint64, ontId return this.native.NewNativeInvokeTransaction( gasPrice, gasLimit, - ONT_ID_CONTRACT_VERSION, - ONT_ID_CONTRACT_ADDRESS, + this.native.DIDContractVersion, + this.native.DIDContractAddr, "addAttributes", []interface{}{ &addAttributes{ @@ -613,24 +682,24 @@ func (this *OntId) NewAddAttributesTransaction(gasPrice, gasLimit uint64, ontId }) } -func (this *OntId) AddAttributes(gasPrice, gasLimit uint64, signer *Account, ontId string, attributes []*DDOAttribute, controller *Controller) (common.Uint256, error) { +func (this *DID) AddAttributes(gasPrice, gasLimit uint64, signer *Account, ontId string, attributes []*DDOAttribute, controller *Controller) (common.Uint256, error) { tx, err := this.NewAddAttributesTransaction(gasPrice, gasLimit, ontId, attributes, controller.PublicKey) if err != nil { return common.UINT256_EMPTY, err } - err = this.dnaSkd.SignToTransaction(tx, signer) + err = this.dnaSdk.SignToTransaction(tx, signer) if err != nil { return common.UINT256_EMPTY, err } - err = this.dnaSkd.SignToTransaction(tx, controller) + err = this.dnaSdk.SignToTransaction(tx, controller) if err != nil { return common.UINT256_EMPTY, err } - return this.dnaSkd.SendTransaction(tx) + return this.dnaSdk.SendTransaction(tx) } -func (this *OntId) NewRemoveAttributeTransaction(gasPrice, gasLimit uint64, ontId string, key []byte, pubKey keypair.PublicKey) (*types.MutableTransaction, error) { +func (this *DID) NewRemoveAttributeTransaction(gasPrice, gasLimit uint64, ontId string, key []byte, pubKey keypair.PublicKey) (*types.MutableTransaction, error) { type removeAttribute struct { OntId string Key []byte @@ -639,8 +708,8 @@ func (this *OntId) NewRemoveAttributeTransaction(gasPrice, gasLimit uint64, ontI return this.native.NewNativeInvokeTransaction( gasPrice, gasLimit, - ONT_ID_CONTRACT_VERSION, - ONT_ID_CONTRACT_ADDRESS, + this.native.DIDContractVersion, + this.native.DIDContractAddr, "removeAttribute", []interface{}{ &removeAttribute{ @@ -651,27 +720,27 @@ func (this *OntId) NewRemoveAttributeTransaction(gasPrice, gasLimit uint64, ontI }) } -func (this *OntId) RemoveAttribute(gasPrice, gasLimit uint64, signer *Account, ontId string, removeKey []byte, controller *Controller) (common.Uint256, error) { +func (this *DID) RemoveAttribute(gasPrice, gasLimit uint64, signer *Account, ontId string, removeKey []byte, controller *Controller) (common.Uint256, error) { tx, err := this.NewRemoveAttributeTransaction(gasPrice, gasLimit, ontId, removeKey, controller.PublicKey) if err != nil { return common.UINT256_EMPTY, err } - err = this.dnaSkd.SignToTransaction(tx, signer) + err = this.dnaSdk.SignToTransaction(tx, signer) if err != nil { return common.UINT256_EMPTY, err } - err = this.dnaSkd.SignToTransaction(tx, controller) + err = this.dnaSdk.SignToTransaction(tx, controller) if err != nil { return common.UINT256_EMPTY, err } - return this.dnaSkd.SendTransaction(tx) + return this.dnaSdk.SendTransaction(tx) } -func (this *OntId) GetAttributes(ontId string) ([]*DDOAttribute, error) { +func (this *DID) GetAttributes(ontId string) ([]*DDOAttribute, error) { preResult, err := this.native.PreExecInvokeNativeContract( - ONT_ID_CONTRACT_ADDRESS, - ONT_ID_CONTRACT_VERSION, + this.native.DIDContractAddr, + this.native.DIDContractVersion, "getAttributes", []interface{}{ontId}) if err != nil { @@ -684,7 +753,7 @@ func (this *OntId) GetAttributes(ontId string) ([]*DDOAttribute, error) { return this.getAttributes(ontId, data) } -func (this *OntId) getAttributes(ontId string, data []byte) ([]*DDOAttribute, error) { +func (this *DID) getAttributes(ontId string, data []byte) ([]*DDOAttribute, error) { buf := bytes.NewBuffer(data) attributes := make([]*DDOAttribute, 0) for { @@ -716,31 +785,31 @@ func (this *OntId) getAttributes(ontId string, data []byte) ([]*DDOAttribute, er return attributes, nil } -func (this *OntId) VerifySignature(ontId string, keyIndex int, controller *Controller) (bool, error) { +func (this *DID) VerifySignature(ontId string, keyIndex int, controller *Controller) (bool, error) { tx, err := this.native.NewNativeInvokeTransaction( 0, 0, - ONT_ID_CONTRACT_VERSION, - ONT_ID_CONTRACT_ADDRESS, + this.native.DIDContractVersion, + this.native.DIDContractAddr, "verifySignature", []interface{}{ontId, keyIndex}) if err != nil { return false, err } - err = this.dnaSkd.SignToTransaction(tx, controller) + err = this.dnaSdk.SignToTransaction(tx, controller) if err != nil { return false, err } - preResult, err := this.dnaSkd.PreExecTransaction(tx) + preResult, err := this.dnaSdk.PreExecTransaction(tx) if err != nil { return false, err } return preResult.Result.ToBool() } -func (this *OntId) GetPublicKeys(ontId string) ([]*DDOOwner, error) { +func (this *DID) GetPublicKeys(ontId string) ([]*DDOOwner, error) { preResult, err := this.native.PreExecInvokeNativeContract( - ONT_ID_CONTRACT_ADDRESS, - ONT_ID_CONTRACT_VERSION, + this.native.DIDContractAddr, + this.native.DIDContractVersion, "getPublicKeys", []interface{}{ ontId, @@ -755,7 +824,7 @@ func (this *OntId) GetPublicKeys(ontId string) ([]*DDOOwner, error) { return this.getPublicKeys(ontId, data) } -func (this *OntId) getPublicKeys(ontId string, data []byte) ([]*DDOOwner, error) { +func (this *DID) getPublicKeys(ontId string, data []byte) ([]*DDOOwner, error) { buf := bytes.NewBuffer(data) owners := make([]*DDOOwner, 0) for { @@ -788,14 +857,14 @@ func (this *OntId) getPublicKeys(ontId string, data []byte) ([]*DDOOwner, error) return owners, nil } -func (this *OntId) GetKeyState(ontId string, keyIndex int) (string, error) { +func (this *DID) GetKeyState(ontId string, keyIndex int) (string, error) { type keyState struct { OntId string KeyIndex int } preResult, err := this.native.PreExecInvokeNativeContract( - ONT_ID_CONTRACT_ADDRESS, - ONT_ID_CONTRACT_VERSION, + this.native.DIDContractAddr, + this.native.DIDContractVersion, "getKeyState", []interface{}{ &keyState{ @@ -960,16 +1029,64 @@ func (this *GlobalParam) CreateSnapshot(gasPrice, gasLimit uint64, signer *Accou } type Auth struct { - dnaSkd *DNASdk + dnaSdk *DNASdk native *NativeContract } +func (this *Auth) NewInitAuthTransaction(gasPrice, gasLimit uint64) (*types.MutableTransaction, error) { + return this.native.NewNativeInvokeTransaction( + gasPrice, + gasLimit, + this.native.AuthContractVersion, + this.native.AuthContractAddr, + "initAuth", + []interface{}{ + this.native.DIDContractAddr[:], + }, + ) +} + +func (this *Auth) InitAuth(gasPrice, gasLimit uint64, signer *Account) (common.Uint256, error) { + tx, err := this.NewInitAuthTransaction(gasPrice, gasLimit) + if err != nil { + return common.UINT256_EMPTY, err + } + if err := this.dnaSdk.SignToTransaction(tx, signer); err != nil { + return common.UINT256_EMPTY, err + } + return this.dnaSdk.SendTransaction(tx) +} + +func (this *Auth) NewInitContractAdminTransaction(gasPrice, gasLimit uint64, adminId []byte) (*types.MutableTransaction, error) { + return this.native.NewNativeInvokeTransaction( + gasPrice, + gasLimit, + this.native.AuthContractVersion, + this.native.AuthContractAddr, + "initContractAdmin", + []interface{}{ + adminId, + }, + ) +} + +func (this *Auth) InitContractAdmin(gasPrice, gasLimit uint64, signer *Account, adminId []byte) (common.Uint256, error) { + tx, err := this.NewInitContractAdminTransaction(gasPrice, gasLimit, adminId) + if err != nil { + return common.UINT256_EMPTY, err + } + if err := this.dnaSdk.SignToTransaction(tx, signer); err != nil { + return common.UINT256_EMPTY, err + } + return this.dnaSdk.SendTransaction(tx) +} + func (this *Auth) NewAssignFuncsToRoleTransaction(gasPrice, gasLimit uint64, contractAddress common.Address, adminId, role []byte, funcNames []string, keyIndex int) (*types.MutableTransaction, error) { return this.native.NewNativeInvokeTransaction( gasPrice, gasLimit, - AUTH_CONTRACT_VERSION, - AUTH_CONTRACT_ADDRESS, + this.native.AuthContractVersion, + this.native.AuthContractAddr, "assignFuncsToRole", []interface{}{ contractAddress, @@ -985,19 +1102,19 @@ func (this *Auth) AssignFuncsToRole(gasPrice, gasLimit uint64, contractAddress c if err != nil { return common.UINT256_EMPTY, err } - err = this.dnaSkd.SignToTransaction(tx, signer) + err = this.dnaSdk.SignToTransaction(tx, signer) if err != nil { return common.UINT256_EMPTY, err } - return this.dnaSkd.SendTransaction(tx) + return this.dnaSdk.SendTransaction(tx) } func (this *Auth) NewDelegateTransaction(gasPrice, gasLimit uint64, contractAddress common.Address, from, to, role []byte, period, level, keyIndex int) (*types.MutableTransaction, error) { return this.native.NewNativeInvokeTransaction( gasPrice, gasLimit, - AUTH_CONTRACT_VERSION, - AUTH_CONTRACT_ADDRESS, + this.native.AuthContractVersion, + this.native.AuthContractAddr, "delegate", []interface{}{ contractAddress, @@ -1015,19 +1132,19 @@ func (this *Auth) Delegate(gasPrice, gasLimit uint64, signer *Account, contractA if err != nil { return common.UINT256_EMPTY, err } - err = this.dnaSkd.SignToTransaction(tx, signer) + err = this.dnaSdk.SignToTransaction(tx, signer) if err != nil { return common.UINT256_EMPTY, err } - return this.dnaSkd.SendTransaction(tx) + return this.dnaSdk.SendTransaction(tx) } func (this *Auth) NewWithdrawTransaction(gasPrice, gasLimit uint64, contractAddress common.Address, initiator, delegate, role []byte, keyIndex int) (*types.MutableTransaction, error) { return this.native.NewNativeInvokeTransaction( gasPrice, gasLimit, - AUTH_CONTRACT_VERSION, - AUTH_CONTRACT_ADDRESS, + this.native.AuthContractVersion, + this.native.AuthContractAddr, "withdraw", []interface{}{ contractAddress, @@ -1043,19 +1160,19 @@ func (this *Auth) Withdraw(gasPrice, gasLimit uint64, signer *Account, contractA if err != nil { return common.UINT256_EMPTY, err } - err = this.dnaSkd.SignToTransaction(tx, signer) + err = this.dnaSdk.SignToTransaction(tx, signer) if err != nil { return common.UINT256_EMPTY, err } - return this.dnaSkd.SendTransaction(tx) + return this.dnaSdk.SendTransaction(tx) } func (this *Auth) NewAssignOntIDsToRoleTransaction(gasPrice, gasLimit uint64, contractAddress common.Address, admontId, role []byte, persons [][]byte, keyIndex int) (*types.MutableTransaction, error) { return this.native.NewNativeInvokeTransaction( gasPrice, gasLimit, - AUTH_CONTRACT_VERSION, - AUTH_CONTRACT_ADDRESS, + this.native.AuthContractVersion, + this.native.AuthContractAddr, "assignOntIDsToRole", []interface{}{ contractAddress, @@ -1071,19 +1188,19 @@ func (this *Auth) AssignOntIDsToRole(gasPrice, gasLimit uint64, signer *Account, if err != nil { return common.UINT256_EMPTY, err } - err = this.dnaSkd.SignToTransaction(tx, signer) + err = this.dnaSdk.SignToTransaction(tx, signer) if err != nil { return common.UINT256_EMPTY, err } - return this.dnaSkd.SendTransaction(tx) + return this.dnaSdk.SendTransaction(tx) } func (this *Auth) NewTransferTransaction(gasPrice, gasLimit uint64, contractAddress common.Address, newAdminId []byte, keyIndex int) (*types.MutableTransaction, error) { return this.native.NewNativeInvokeTransaction( gasPrice, gasLimit, - AUTH_CONTRACT_VERSION, - AUTH_CONTRACT_ADDRESS, + this.native.AuthContractVersion, + this.native.AuthContractAddr, "transfer", []interface{}{ contractAddress, @@ -1097,19 +1214,19 @@ func (this *Auth) Transfer(gasPrice, gasLimit uint64, signer *Account, contractA if err != nil { return common.UINT256_EMPTY, err } - err = this.dnaSkd.SignToTransaction(tx, signer) + err = this.dnaSdk.SignToTransaction(tx, signer) if err != nil { return common.UINT256_EMPTY, err } - return this.dnaSkd.SendTransaction(tx) + return this.dnaSdk.SendTransaction(tx) } func (this *Auth) NewVerifyTokenTransaction(gasPrice, gasLimit uint64, contractAddress common.Address, caller []byte, funcName string, keyIndex int) (*types.MutableTransaction, error) { return this.native.NewNativeInvokeTransaction( gasPrice, gasLimit, - AUTH_CONTRACT_VERSION, - AUTH_CONTRACT_ADDRESS, + this.native.AuthContractVersion, + this.native.AuthContractAddr, "verifyToken", []interface{}{ contractAddress, @@ -1124,9 +1241,9 @@ func (this *Auth) VerifyToken(gasPrice, gasLimit uint64, signer *Account, contra if err != nil { return common.UINT256_EMPTY, err } - err = this.dnaSkd.SignToTransaction(tx, signer) + err = this.dnaSdk.SignToTransaction(tx, signer) if err != nil { return common.UINT256_EMPTY, err } - return this.dnaSkd.SendTransaction(tx) + return this.dnaSdk.SendTransaction(tx) } diff --git a/native_contract_test.go b/native_contract_test.go index ec5d5ad..8182128 100644 --- a/native_contract_test.go +++ b/native_contract_test.go @@ -7,13 +7,62 @@ package DNA_go_sdk import ( "encoding/hex" "fmt" - "github.com/ontio/ontology-crypto/keypair" "testing" "time" + + "github.com/DNAProject/DNA/smartcontract/service/native/common" + "github.com/ontio/ontology-crypto/keypair" + "github.com/DNAProject/DNA-go-sdk/utils" ) +func TestNativeContract_DeployDIDContract(t *testing.T) { + Init() + + initParamStr, err := GenerateID(testDidMethod) + if err != nil { + t.Errorf("generate ID in deploy did native contract: %s", err) + return + } + txHash, err := testDnaSdk.Native.DeployNativeContract(testGasPrice, testGasLimit, testDefAcc, + common.DIDContractAddress, []byte(initParamStr), + "testname", "1.0", "testAuthor", "test@example.com", "test deploying native DID") + testDnaSdk.WaitForGenerateBlock(30*time.Second, 1) + event, err := testDnaSdk.GetSmartContractEvent(txHash.ToHexString()) + if err != nil { + t.Errorf("test deploy did native contract error: %s", err) + return + } + fmt.Printf("test deploy did native contract event: %+v\n", event) + for _, ev := range event.Notify { + fmt.Printf("contract: %s, state: %v\n", ev.ContractAddress, ev.States) + didAddr, err := utils.AddressFromHexString(ev.ContractAddress) + if err != nil { + t.Errorf("failed to parse did addr: %s", err) + } + testDnaSdk.Native.DIDContractAddr = didAddr + t.Logf("update DID contract to %s", ev.ContractAddress) + } + + TestDID_InitDID(t) + TestOntId_RegIDWithPublicKey(t) +} + +func TestDID_InitDID(t *testing.T) { + txHash, err := testDnaSdk.Native.DID.InitDID(testGasPrice, testGasLimit, testDefAcc, testDidMethod) + if err != nil { + t.Errorf("TestDID_InitDID init did err: %s", err) + return + } + testDnaSdk.WaitForGenerateBlock(30*time.Second, 1) + event, err := testDnaSdk.GetSmartContractEvent(txHash.ToHexString()) + if err != nil { + t.Errorf("TestDID_InitDID get smart contract event: %s", err) + } + fmt.Printf("TestDID_InitDID event: %v\n", event) +} + func TestOntId_RegIDWithPublicKey(t *testing.T) { - testIdentity, err := testWallet.NewDefaultSettingIdentity(testPasswd) + testIdentity, err := testWallet.NewDefaultSettingIdentity(testDidMethod, testPasswd) if err != nil { t.Errorf("TestOntId_RegIDWithPublicKey NewDefaultSettingIdentity error:%s", err) return @@ -23,7 +72,7 @@ func TestOntId_RegIDWithPublicKey(t *testing.T) { t.Errorf("TestOntId_RegIDWithPublicKey GetControllerByIndex error:%s", err) return } - txHash, err := testDnaSdk.Native.OntId.RegIDWithPublicKey(testGasPrice, testGasLimit, testDefAcc, testIdentity.ID, testDefController) + txHash, err := testDnaSdk.Native.DID.RegIDWithPublicKey(testGasPrice, testGasLimit, testDefAcc, testIdentity.ID, testDefController) if err != nil { t.Errorf("TestOntId_RegIDWithPublicKey RegIDWithPublicKey error:%s", err) return @@ -36,7 +85,7 @@ func TestOntId_RegIDWithPublicKey(t *testing.T) { } fmt.Printf("TestOntId_RegIDWithPublicKey Event: %+v\n", event) - ddo, err := testDnaSdk.Native.OntId.GetDDO(testIdentity.ID) + ddo, err := testDnaSdk.Native.DID.GetDDO(testIdentity.ID) if err != nil { t.Errorf("TestOntId_RegIDWithPublicKey GetDDO error:%s", err) return @@ -45,7 +94,7 @@ func TestOntId_RegIDWithPublicKey(t *testing.T) { } func TestOntId_RegIDWithAttributes(t *testing.T) { - testIdentity, err := testWallet.NewDefaultSettingIdentity(testPasswd) + testIdentity, err := testWallet.NewDefaultSettingIdentity(testDidMethod, testPasswd) if err != nil { t.Errorf("TestOntId_RegIDWithPublicKey NewDefaultSettingIdentity error:%s", err) return @@ -68,14 +117,14 @@ func TestOntId_RegIDWithAttributes(t *testing.T) { ValueType: []byte("string"), } attributes = append(attributes, attr2) - _, err = testDnaSdk.Native.OntId.RegIDWithAttributes(testGasPrice, testGasLimit, testDefAcc, testIdentity.ID, testDefController, attributes) + _, err = testDnaSdk.Native.DID.RegIDWithAttributes(testGasPrice, testGasLimit, testDefAcc, testIdentity.ID, testDefController, attributes) if err != nil { t.Errorf("TestOntId_RegIDWithPublicKey RegIDWithAttributes error:%s", err) return } testDnaSdk.WaitForGenerateBlock(30*time.Second, 1) - ddo, err := testDnaSdk.Native.OntId.GetDDO(testIdentity.ID) + ddo, err := testDnaSdk.Native.DID.GetDDO(testIdentity.ID) if err != nil { t.Errorf("GetDDO error:%s", err) return @@ -105,7 +154,7 @@ func TestOntId_RegIDWithAttributes(t *testing.T) { } func TestOntId_Key(t *testing.T) { - testIdentity, err := testWallet.NewDefaultSettingIdentity(testPasswd) + testIdentity, err := testWallet.NewDefaultSettingIdentity(testDidMethod, testPasswd) if err != nil { t.Errorf("TestOntId_Key NewDefaultSettingIdentity error:%s", err) return @@ -115,7 +164,7 @@ func TestOntId_Key(t *testing.T) { t.Errorf("TestOntId_Key GetControllerByIndex error:%s", err) return } - _, err = testDnaSdk.Native.OntId.RegIDWithPublicKey(testGasPrice, testGasLimit, testDefAcc, testIdentity.ID, testDefController) + _, err = testDnaSdk.Native.DID.RegIDWithPublicKey(testGasPrice, testGasLimit, testDefAcc, testIdentity.ID, testDefController) if err != nil { t.Errorf("TestOntId_Key RegIDWithPublicKey error:%s", err) return @@ -128,14 +177,14 @@ func TestOntId_Key(t *testing.T) { return } - _, err = testDnaSdk.Native.OntId.AddKey(testGasPrice, testGasLimit, testIdentity.ID, testDefAcc, controller1.PublicKey, testDefController) + _, err = testDnaSdk.Native.DID.AddKey(testGasPrice, testGasLimit, testIdentity.ID, testDefAcc, controller1.PublicKey, testDefController) if err != nil { t.Errorf("TestOntId_Key AddKey error:%s", err) return } testDnaSdk.WaitForGenerateBlock(30*time.Second, 1) - owners, err := testDnaSdk.Native.OntId.GetPublicKeys(testIdentity.ID) + owners, err := testDnaSdk.Native.DID.GetPublicKeys(testIdentity.ID) if err != nil { t.Errorf("TestOntId_Key GetPublicKeys error:%s", err) return @@ -156,14 +205,14 @@ func TestOntId_Key(t *testing.T) { return } - _, err = testDnaSdk.Native.OntId.RevokeKey(testGasPrice, testGasLimit, testIdentity.ID, testDefAcc, testDefController.PublicKey, controller1) + _, err = testDnaSdk.Native.DID.RevokeKey(testGasPrice, testGasLimit, testIdentity.ID, testDefAcc, testDefController.PublicKey, controller1) if err != nil { t.Errorf("TestOntId_Key RevokeKey error:%s", err) return } testDnaSdk.WaitForGenerateBlock(30*time.Second, 1) - owners, err = testDnaSdk.Native.OntId.GetPublicKeys(testIdentity.ID) + owners, err = testDnaSdk.Native.DID.GetPublicKeys(testIdentity.ID) if err != nil { t.Errorf("TestOntId_Key GetPublicKeys error:%s", err) return @@ -174,7 +223,7 @@ func TestOntId_Key(t *testing.T) { return } - state, err := testDnaSdk.Native.OntId.GetKeyState(testIdentity.ID, 1) + state, err := testDnaSdk.Native.DID.GetKeyState(testIdentity.ID, 1) if err != nil { t.Errorf("TestOntId_Key GetKeyState error:%s", err) return @@ -185,7 +234,7 @@ func TestOntId_Key(t *testing.T) { return } - state, err = testDnaSdk.Native.OntId.GetKeyState(testIdentity.ID, 2) + state, err = testDnaSdk.Native.DID.GetKeyState(testIdentity.ID, 2) if err != nil { t.Errorf("TestOntId_Key GetKeyState error:%s", err) return @@ -197,7 +246,7 @@ func TestOntId_Key(t *testing.T) { } func TestOntId_Attribute(t *testing.T) { - testIdentity, err := testWallet.NewDefaultSettingIdentity(testPasswd) + testIdentity, err := testWallet.NewDefaultSettingIdentity(testDidMethod, testPasswd) if err != nil { t.Errorf("TestOntId_Attribute NewDefaultSettingIdentity error:%s", err) return @@ -207,7 +256,7 @@ func TestOntId_Attribute(t *testing.T) { t.Errorf("TestOntId_Attribute GetControllerByIndex error:%s", err) return } - _, err = testDnaSdk.Native.OntId.RegIDWithPublicKey(testGasPrice, testGasLimit, testDefAcc, testIdentity.ID, testDefController) + _, err = testDnaSdk.Native.DID.RegIDWithPublicKey(testGasPrice, testGasLimit, testDefAcc, testIdentity.ID, testDefController) if err != nil { t.Errorf("TestOntId_Attribute RegIDWithPublicKey error:%s", err) return @@ -227,13 +276,13 @@ func TestOntId_Attribute(t *testing.T) { ValueType: []byte("string"), } attributes = append(attributes, attr2) - _, err = testDnaSdk.Native.OntId.AddAttributes(testGasPrice, testGasLimit, testDefAcc, testIdentity.ID, attributes, testDefController) + _, err = testDnaSdk.Native.DID.AddAttributes(testGasPrice, testGasLimit, testDefAcc, testIdentity.ID, attributes, testDefController) if err != nil { t.Errorf("TestOntId_Attribute AddAttributes error:%s", err) return } testDnaSdk.WaitForGenerateBlock(30*time.Second, 1) - attrs, err := testDnaSdk.Native.OntId.GetAttributes(testIdentity.ID) + attrs, err := testDnaSdk.Native.DID.GetAttributes(testIdentity.ID) if len(attributes) != len(attrs) { t.Errorf("TestOntId_Attribute GetAttributes len:%d != %d", len(attrs), len(attributes)) return @@ -243,13 +292,13 @@ func TestOntId_Attribute(t *testing.T) { return } - _, err = testDnaSdk.Native.OntId.RemoveAttribute(testGasPrice, testGasLimit, testDefAcc, testIdentity.ID, attr1.Key, testDefController) + _, err = testDnaSdk.Native.DID.RemoveAttribute(testGasPrice, testGasLimit, testDefAcc, testIdentity.ID, attr1.Key, testDefController) if err != nil { t.Errorf("TestOntId_Attribute RemoveAttribute error:%s", err) return } testDnaSdk.WaitForGenerateBlock(30*time.Second, 1) - attrs, err = testDnaSdk.Native.OntId.GetAttributes(testIdentity.ID) + attrs, err = testDnaSdk.Native.DID.GetAttributes(testIdentity.ID) if len(attrs) != 1 { t.Errorf("TestOntId_Attribute GetAttributes len:%d != 1", len(attrs)) return @@ -261,7 +310,7 @@ func TestOntId_Attribute(t *testing.T) { } func TestOntId_Recovery(t *testing.T) { - testIdentity, err := testWallet.NewDefaultSettingIdentity(testPasswd) + testIdentity, err := testWallet.NewDefaultSettingIdentity(testDidMethod, testPasswd) if err != nil { t.Errorf("TestOntId_Recovery NewDefaultSettingIdentity error:%s", err) return @@ -271,19 +320,19 @@ func TestOntId_Recovery(t *testing.T) { t.Errorf("TestOntId_Recovery GetControllerByIndex error:%s", err) return } - _, err = testDnaSdk.Native.OntId.RegIDWithPublicKey(testGasPrice, testGasLimit, testDefAcc, testIdentity.ID, testDefController) + _, err = testDnaSdk.Native.DID.RegIDWithPublicKey(testGasPrice, testGasLimit, testDefAcc, testIdentity.ID, testDefController) if err != nil { t.Errorf("TestOntId_Recovery RegIDWithPublicKey error:%s", err) return } testDnaSdk.WaitForGenerateBlock(30*time.Second, 1) - _, err = testDnaSdk.Native.OntId.SetRecovery(testGasPrice, testGasLimit, testDefAcc, testIdentity.ID, testDefAcc.Address, testDefController) + _, err = testDnaSdk.Native.DID.SetRecovery(testGasPrice, testGasLimit, testDefAcc, testIdentity.ID, testDefAcc.Address, testDefController) if err != nil { t.Errorf("TestOntId_Recovery SetRecovery error:%s", err) return } testDnaSdk.WaitForGenerateBlock(30*time.Second, 1) - ddo, err := testDnaSdk.Native.OntId.GetDDO(testIdentity.ID) + ddo, err := testDnaSdk.Native.DID.GetDDO(testIdentity.ID) if err != nil { t.Errorf("TestOntId_Recovery GetDDO error:%s", err) return @@ -299,7 +348,7 @@ func TestOntId_Recovery(t *testing.T) { return } - txHash, err := testDnaSdk.Native.OntId.SetRecovery(testGasPrice, testGasLimit, testDefAcc, testIdentity.ID, acc1.Address, testDefController) + txHash, err := testDnaSdk.Native.DID.SetRecovery(testGasPrice, testGasLimit, testDefAcc, testIdentity.ID, acc1.Address, testDefController) testDnaSdk.WaitForGenerateBlock(30*time.Second, 1) evt, err := testDnaSdk.GetSmartContractEvent(txHash.ToHexString()) @@ -311,13 +360,13 @@ func TestOntId_Recovery(t *testing.T) { t.Errorf("TestOntId_Recovery duplicate add recovery should failed") return } - _, err = testDnaSdk.Native.OntId.ChangeRecovery(testGasPrice, testGasLimit, testDefAcc, testIdentity.ID, acc1.Address, testDefAcc.Address, testDefController) + _, err = testDnaSdk.Native.DID.ChangeRecovery(testGasPrice, testGasLimit, testDefAcc, testIdentity.ID, acc1.Address, testDefAcc.Address, testDefController) if err != nil { t.Errorf("TestOntId_Recovery ChangeRecovery error:%s", err) return } testDnaSdk.WaitForGenerateBlock(30*time.Second, 1) - ddo, err = testDnaSdk.Native.OntId.GetDDO(testIdentity.ID) + ddo, err = testDnaSdk.Native.DID.GetDDO(testIdentity.ID) if err != nil { t.Errorf("TestOntId_Recovery GetDDO error:%s", err) return diff --git a/utils/utils.go b/utils/utils.go index 1c1f861..e3df936 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -27,7 +27,7 @@ import ( "github.com/DNAProject/DNA/common" "github.com/DNAProject/DNA/core/signature" "github.com/DNAProject/DNA/core/types" - nvutils "github.com/DNAProject/DNA/smartcontract/service/native/utils" + common2 "github.com/DNAProject/DNA/smartcontract/service/native/common" "github.com/ontio/ontology-crypto/keypair" "os" "sort" @@ -74,7 +74,7 @@ func GetAssetAddress(asset string) (common.Address, error) { var contractAddress common.Address switch strings.ToUpper(asset) { case "GAS": - contractAddress = nvutils.GasContractAddress + contractAddress = common2.GasContractAddress default: return common.ADDRESS_EMPTY, fmt.Errorf("asset:%s not equal gas", asset) } @@ -128,7 +128,7 @@ func IsEmptyJsonArray(data []byte) bool { return true } str := string(data[:2]) - if str == "\"\"" || str == "[]" || str == "{}"{ + if str == "\"\"" || str == "[]" || str == "{}" { return true } return false diff --git a/wallet.go b/wallet.go index a4b2cdc..220b3ea 100644 --- a/wallet.go +++ b/wallet.go @@ -451,8 +451,8 @@ func (this *Wallet) ExportAccounts(path string, accountDatas []*AccountData, pas return newWallet, nil } -func (this *Wallet) NewIdentity(keyType keypair.KeyType, curveCode byte, sigScheme s.SignatureScheme, passwd []byte) (*Identity, error) { - identity, err := NewIdentity(this.Scrypt) +func (this *Wallet) NewIdentity(didMethod string, keyType keypair.KeyType, curveCode byte, sigScheme s.SignatureScheme, passwd []byte) (*Identity, error) { + identity, err := NewIdentity(didMethod, this.Scrypt) if err != nil { return nil, err } @@ -473,8 +473,8 @@ func (this *Wallet) NewIdentity(keyType keypair.KeyType, curveCode byte, sigSche return identity, nil } -func (this *Wallet) NewDefaultSettingIdentity(passwd []byte) (*Identity, error) { - return this.NewIdentity(keypair.PK_ECDSA, keypair.P256, s.SHA256withECDSA, passwd) +func (this *Wallet) NewDefaultSettingIdentity(didMethod string, passwd []byte) (*Identity, error) { + return this.NewIdentity(didMethod, keypair.PK_ECDSA, keypair.P256, s.SHA256withECDSA, passwd) } func (this *Wallet) GetDefaultIdentity() (*Identity, error) {