From 0c1f3f0b9b69f24c0f82143d4af4af3954f59dfd Mon Sep 17 00:00:00 2001 From: Edmond-Honglei-Cong Date: Sat, 15 Feb 2020 18:22:46 +0800 Subject: [PATCH 1/5] add supporting of deployable-native-contract --- native_contract.go | 241 ++++++++++++++++++++++++---------------- native_contract_test.go | 65 +++++++---- utils/utils.go | 6 +- 3 files changed, 189 insertions(+), 123 deletions(-) diff --git a/native_contract.go b/native_contract.go index 6e6e395..2c88ba3 100644 --- a/native_contract.go +++ b/native_contract.go @@ -15,24 +15,22 @@ import ( "github.com/DNAProject/DNA/common/serialization" "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" + "github.com/DNAProject/DNA/core/payload" ) 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,15 +40,27 @@ 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} 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,12 @@ 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) NewRegIDWithPublicKeyTransaction(gasPrice, gasLimit uint64, ontId string, pubKey keypair.PublicKey) (*types.MutableTransaction, error) { type regIDWithPublicKey struct { OntId string PubKey []byte @@ -327,8 +372,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 +384,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 +409,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 +422,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 +488,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 +497,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 +509,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 +534,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 +547,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 +572,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 +584,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 +609,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 +621,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 +646,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 +658,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 +684,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 +696,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 +729,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 +761,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 +800,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 +833,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{ @@ -968,8 +1013,8 @@ func (this *Auth) NewAssignFuncsToRoleTransaction(gasPrice, gasLimit uint64, con return this.native.NewNativeInvokeTransaction( gasPrice, gasLimit, - AUTH_CONTRACT_VERSION, - AUTH_CONTRACT_ADDRESS, + this.native.AuthContractVersion, + this.native.AuthContractAddr, "assignFuncsToRole", []interface{}{ contractAddress, @@ -996,8 +1041,8 @@ func (this *Auth) NewDelegateTransaction(gasPrice, gasLimit uint64, contractAddr return this.native.NewNativeInvokeTransaction( gasPrice, gasLimit, - AUTH_CONTRACT_VERSION, - AUTH_CONTRACT_ADDRESS, + this.native.AuthContractVersion, + this.native.AuthContractAddr, "delegate", []interface{}{ contractAddress, @@ -1026,8 +1071,8 @@ func (this *Auth) NewWithdrawTransaction(gasPrice, gasLimit uint64, contractAddr return this.native.NewNativeInvokeTransaction( gasPrice, gasLimit, - AUTH_CONTRACT_VERSION, - AUTH_CONTRACT_ADDRESS, + this.native.AuthContractVersion, + this.native.AuthContractAddr, "withdraw", []interface{}{ contractAddress, @@ -1054,8 +1099,8 @@ func (this *Auth) NewAssignOntIDsToRoleTransaction(gasPrice, gasLimit uint64, co return this.native.NewNativeInvokeTransaction( gasPrice, gasLimit, - AUTH_CONTRACT_VERSION, - AUTH_CONTRACT_ADDRESS, + this.native.AuthContractVersion, + this.native.AuthContractAddr, "assignOntIDsToRole", []interface{}{ contractAddress, @@ -1082,8 +1127,8 @@ func (this *Auth) NewTransferTransaction(gasPrice, gasLimit uint64, contractAddr return this.native.NewNativeInvokeTransaction( gasPrice, gasLimit, - AUTH_CONTRACT_VERSION, - AUTH_CONTRACT_ADDRESS, + this.native.AuthContractVersion, + this.native.AuthContractAddr, "transfer", []interface{}{ contractAddress, @@ -1108,8 +1153,8 @@ func (this *Auth) NewVerifyTokenTransaction(gasPrice, gasLimit uint64, contractA return this.native.NewNativeInvokeTransaction( gasPrice, gasLimit, - AUTH_CONTRACT_VERSION, - AUTH_CONTRACT_ADDRESS, + this.native.AuthContractVersion, + this.native.AuthContractAddr, "verifyToken", []interface{}{ contractAddress, diff --git a/native_contract_test.go b/native_contract_test.go index ec5d5ad..23fae09 100644 --- a/native_contract_test.go +++ b/native_contract_test.go @@ -7,11 +7,32 @@ package DNA_go_sdk import ( "encoding/hex" "fmt" + "github.com/DNAProject/DNA/smartcontract/service/native/common" "github.com/ontio/ontology-crypto/keypair" "testing" "time" ) +func TestNativeContract_DeployNativeContract(t *testing.T) { + Init() + + initParamStr, err := GenerateID() + if err != nil { + t.Errorf("generate ID in deploy 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 native contract error: %s", err) + return + } + fmt.Printf("test deploy native contract event: %+v\n", event) +} + func TestOntId_RegIDWithPublicKey(t *testing.T) { testIdentity, err := testWallet.NewDefaultSettingIdentity(testPasswd) if err != nil { @@ -23,7 +44,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 +57,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 @@ -68,14 +89,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 @@ -115,7 +136,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 +149,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 +177,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 +195,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 +206,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 @@ -207,7 +228,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 +248,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 +264,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 @@ -271,19 +292,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 +320,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 +332,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 From f82b4abdd573f22f70882e5054943a1140a9a976 Mon Sep 17 00:00:00 2001 From: Edmond-Honglei-Cong Date: Sun, 16 Feb 2020 12:12:04 +0800 Subject: [PATCH 2/5] update deploy notify log --- native_contract_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/native_contract_test.go b/native_contract_test.go index 23fae09..6fb0b64 100644 --- a/native_contract_test.go +++ b/native_contract_test.go @@ -31,6 +31,9 @@ func TestNativeContract_DeployNativeContract(t *testing.T) { 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) + } } func TestOntId_RegIDWithPublicKey(t *testing.T) { From b57c185e15893c738efa8e6f217074610a487196 Mon Sep 17 00:00:00 2001 From: Edmond-Honglei-Cong Date: Sun, 16 Feb 2020 14:40:05 +0800 Subject: [PATCH 3/5] add didMethod support and testcase --- dna_sdk_test.go | 13 +++++++------ identity.go | 16 +++++++++------- native_contract.go | 28 ++++++++++++++++++++++++++-- native_contract_test.go | 41 +++++++++++++++++++++++++++++++++-------- wallet.go | 8 ++++---- 5 files changed, 79 insertions(+), 27 deletions(-) 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_contract.go b/native_contract.go index 2c88ba3..c1d57b5 100644 --- a/native_contract.go +++ b/native_contract.go @@ -13,13 +13,13 @@ 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" - "github.com/DNAProject/DNA/core/payload" ) var ( @@ -97,7 +97,7 @@ func (this *NativeContract) DeployNativeContract( ) (common.Uint256, error) { ndc := &payload.NativeDeployCode{ BaseContractAddress: baseNativeContract, - InitParam: initParam, + InitParam: initParam, } sink := common.NewZeroCopySink(nil) ndc.Serialization(sink) @@ -364,6 +364,30 @@ type DID struct { native *NativeContract } +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 diff --git a/native_contract_test.go b/native_contract_test.go index 6fb0b64..9b7e07d 100644 --- a/native_contract_test.go +++ b/native_contract_test.go @@ -7,16 +7,18 @@ package DNA_go_sdk import ( "encoding/hex" "fmt" - "github.com/DNAProject/DNA/smartcontract/service/native/common" - "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_DeployNativeContract(t *testing.T) { Init() - initParamStr, err := GenerateID() + initParamStr, err := GenerateID(testDidMethod) if err != nil { t.Errorf("generate ID in deploy native contract: %s", err) return @@ -33,11 +35,34 @@ func TestNativeContract_DeployNativeContract(t *testing.T) { 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) + didAddr, err := utils.AddressFromHexString(ev.ContractAddress) + if err != nil { + t.Errorf("failed to parse 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 @@ -69,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 @@ -129,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 @@ -221,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 @@ -285,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 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) { From 5a063944dd4af0979caf99fad153614351a681c2 Mon Sep 17 00:00:00 2001 From: Edmond-Honglei-Cong Date: Mon, 17 Feb 2020 00:22:13 +0800 Subject: [PATCH 4/5] add native auth testcases - init --- native_auth_test.go | 84 +++++++++++++++++++++++++++++++++++++++++ native_contract.go | 28 +++++++++++++- native_contract_test.go | 20 +++++----- 3 files changed, 120 insertions(+), 12 deletions(-) create mode 100644 native_auth_test.go diff --git a/native_auth_test.go b/native_auth_test.go new file mode 100644 index 0000000..cdd6fe9 --- /dev/null +++ b/native_auth_test.go @@ -0,0 +1,84 @@ +// 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() + + 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) +} + +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_AssignFuncsToRole(t *testing.T) { + +} + +func TestAuth_Delegate(t *testing.T) { + +} + +func TestAuth_Withdraw(t *testing.T) { + +} + +func TestAuth_AssignOntIDsToRole(t *testing.T) { + +} + +func TestAuth_Transfer(t *testing.T) { + +} + +func TestAuth_VerifyToken(t *testing.T) { + +} diff --git a/native_contract.go b/native_contract.go index c1d57b5..9493386 100644 --- a/native_contract.go +++ b/native_contract.go @@ -364,7 +364,7 @@ type DID struct { native *NativeContract } -func (this *DID) NewinitDIDTransaction(gasPrice, gasLimit uint64, didMethod string) (*types.MutableTransaction, error) { +func (this *DID) NewInitDIDTransaction(gasPrice, gasLimit uint64, didMethod string) (*types.MutableTransaction, error) { return this.native.NewNativeInvokeTransaction( gasPrice, gasLimit, @@ -378,7 +378,7 @@ func (this *DID) NewinitDIDTransaction(gasPrice, gasLimit uint64, didMethod stri } func (this *DID) InitDID(gasPrice, gasLimit uint64, signer *Account, didMethod string) (common.Uint256, error) { - tx, err := this.NewinitDIDTransaction(gasPrice, gasLimit, didMethod) + tx, err := this.NewInitDIDTransaction(gasPrice, gasLimit, didMethod) if err != nil { return common.UINT256_EMPTY, err } @@ -1033,6 +1033,30 @@ type Auth struct { 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.dnaSkd.SignToTransaction(tx, signer); err != nil { + return common.UINT256_EMPTY, err + } + return this.dnaSkd.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, diff --git a/native_contract_test.go b/native_contract_test.go index 9b7e07d..8182128 100644 --- a/native_contract_test.go +++ b/native_contract_test.go @@ -15,12 +15,12 @@ import ( "github.com/DNAProject/DNA-go-sdk/utils" ) -func TestNativeContract_DeployNativeContract(t *testing.T) { +func TestNativeContract_DeployDIDContract(t *testing.T) { Init() initParamStr, err := GenerateID(testDidMethod) if err != nil { - t.Errorf("generate ID in deploy native contract: %s", err) + t.Errorf("generate ID in deploy did native contract: %s", err) return } txHash, err := testDnaSdk.Native.DeployNativeContract(testGasPrice, testGasLimit, testDefAcc, @@ -29,36 +29,36 @@ func TestNativeContract_DeployNativeContract(t *testing.T) { testDnaSdk.WaitForGenerateBlock(30*time.Second, 1) event, err := testDnaSdk.GetSmartContractEvent(txHash.ToHexString()) if err != nil { - t.Errorf("test deploy native contract error: %s", err) + t.Errorf("test deploy did native contract error: %s", err) return } - fmt.Printf("test deploy native contract event: %+v\n", event) + 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 addr: %s", err) + 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) + TestDID_InitDID(t) TestOntId_RegIDWithPublicKey(t) } -func TestDID_initDID(t *testing.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) + 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) + t.Errorf("TestDID_InitDID get smart contract event: %s", err) } - fmt.Printf("TestDID_initDID event: %v\n", event) + fmt.Printf("TestDID_InitDID event: %v\n", event) } func TestOntId_RegIDWithPublicKey(t *testing.T) { From ed8202602251397a76c96653b9be64d7aadb2bcc Mon Sep 17 00:00:00 2001 From: Edmond-Honglei-Cong Date: Mon, 17 Feb 2020 13:10:28 +0800 Subject: [PATCH 5/5] update auth contract testcase --- native_auth_test.go | 45 +++++++++++++++++------------------ native_contract.go | 58 ++++++++++++++++++++++++++++++++------------- 2 files changed, 63 insertions(+), 40 deletions(-) diff --git a/native_auth_test.go b/native_auth_test.go index cdd6fe9..1006bf6 100644 --- a/native_auth_test.go +++ b/native_auth_test.go @@ -14,14 +14,15 @@ import ( ) func TestNativeContract_DeployAuthContract(t *testing.T) { - Init() + // 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") @@ -43,6 +44,7 @@ func TestNativeContract_DeployAuthContract(t *testing.T) { } TestAuth_InitAuth(t) + TestAuth_InitContractAdmin(t) } func TestAuth_InitAuth(t *testing.T) { @@ -59,26 +61,23 @@ func TestAuth_InitAuth(t *testing.T) { fmt.Printf("TestAuth_InitAuth event: %v\n", event) } -func TestAuth_AssignFuncsToRole(t *testing.T) { - -} - -func TestAuth_Delegate(t *testing.T) { - -} - -func TestAuth_Withdraw(t *testing.T) { - -} - -func TestAuth_AssignOntIDsToRole(t *testing.T) { - -} - -func TestAuth_Transfer(t *testing.T) { - -} - -func TestAuth_VerifyToken(t *testing.T) { +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 9493386..9139b2f 100644 --- a/native_contract.go +++ b/native_contract.go @@ -62,7 +62,7 @@ func newNativeContract(dnaSkd *DNASdk) *NativeContract { native.Gas = &Gas{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 } @@ -1029,7 +1029,7 @@ func (this *GlobalParam) CreateSnapshot(gasPrice, gasLimit uint64, signer *Accou } type Auth struct { - dnaSkd *DNASdk + dnaSdk *DNASdk native *NativeContract } @@ -1040,7 +1040,7 @@ func (this *Auth) NewInitAuthTransaction(gasPrice, gasLimit uint64) (*types.Muta this.native.AuthContractVersion, this.native.AuthContractAddr, "initAuth", - []interface{} { + []interface{}{ this.native.DIDContractAddr[:], }, ) @@ -1051,10 +1051,34 @@ func (this *Auth) InitAuth(gasPrice, gasLimit uint64, signer *Account) (common.U if err != nil { return common.UINT256_EMPTY, err } - if err := this.dnaSkd.SignToTransaction(tx, signer); err != nil { + if err := this.dnaSdk.SignToTransaction(tx, signer); err != nil { return common.UINT256_EMPTY, err } - return this.dnaSkd.SendTransaction(tx) + 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) { @@ -1078,11 +1102,11 @@ 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) { @@ -1108,11 +1132,11 @@ 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) { @@ -1136,11 +1160,11 @@ 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) { @@ -1164,11 +1188,11 @@ 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) { @@ -1190,11 +1214,11 @@ 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) { @@ -1217,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) }