From 78baa5fff4786b99f5ae42070955d697562ba628 Mon Sep 17 00:00:00 2001 From: Angelo De Caro Date: Mon, 18 Nov 2024 12:40:30 +0100 Subject: [PATCH] complete rebase Signed-off-by: Angelo De Caro --- integration/token/interop/topology.go | 2 +- .../common/interop/pledge/validator_pledge.go | 5 +-- .../driver/interop/state/fabric/state.go | 2 + token/core/fabtoken/validator_transfer.go | 3 -- .../crypto/validator/validator_transfer.go | 1 - .../nogh/driver/interop/state/fabric/state.go | 2 + .../interop/state/fabric/core/state.go | 21 ++++++++-- .../services/network/common/rws/keys/keys.go | 40 +++++++++---------- .../network/common/rws/translator/prover.go | 25 ++++++------ .../network/common/rws/translator/rwset.go | 20 ++++++++++ token/services/network/fabric/tcc/tcc.go | 15 +++---- 11 files changed, 81 insertions(+), 55 deletions(-) diff --git a/integration/token/interop/topology.go b/integration/token/interop/topology.go index 60b425bf4..92f6250cf 100644 --- a/integration/token/interop/topology.go +++ b/integration/token/interop/topology.go @@ -180,7 +180,7 @@ func HTLCNoCrossClaimTopology(opts common.Opts) []api.Topology { // FSC fscTopology := fsc.NewTopology() - fscTopology.SetLogging(opts.FSCLogSpec, "") + fscTopology.SetLogging(opts.FSCLogSpec, "") fscTopology.P2PCommunicationType = opts.CommType addIssuer(fscTopology). diff --git a/token/core/common/interop/pledge/validator_pledge.go b/token/core/common/interop/pledge/validator_pledge.go index ff0c5eba3..c04724b79 100644 --- a/token/core/common/interop/pledge/validator_pledge.go +++ b/token/core/common/interop/pledge/validator_pledge.go @@ -114,10 +114,7 @@ func TransferPledgeValidate[P driver.PublicParameters, T driver.Output, TA drive } func constructMetadataKey(action driver.TransferAction) (string, error) { - inputs, err := action.GetInputs() - if err != nil { - return "", errors.Wrap(err, "failed to retrieve input IDs from action") - } + inputs := action.GetInputs() if len(inputs) != 1 { return "", errors.New("invalid transfer action, does not carry a single input") } diff --git a/token/core/fabtoken/driver/interop/state/fabric/state.go b/token/core/fabtoken/driver/interop/state/fabric/state.go index eee64577f..45437fd9b 100644 --- a/token/core/fabtoken/driver/interop/state/fabric/state.go +++ b/token/core/fabtoken/driver/interop/state/fabric/state.go @@ -17,6 +17,7 @@ import ( "github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge" fabric3 "github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/state/fabric" "github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/state/fabric/core" + "github.com/hyperledger-labs/fabric-token-sdk/token/services/network/common/rws/keys" "github.com/hyperledger-labs/fabric-token-sdk/token/token" "github.com/pkg/errors" "go.uber.org/dig" @@ -84,6 +85,7 @@ func NewStateDriver(in struct { in.RelayProvider, in.VaultStore, &Validator{}, + &keys.Translator{}, ), } } diff --git a/token/core/fabtoken/validator_transfer.go b/token/core/fabtoken/validator_transfer.go index 1c0b0f7ff..232799a59 100644 --- a/token/core/fabtoken/validator_transfer.go +++ b/token/core/fabtoken/validator_transfer.go @@ -7,8 +7,6 @@ SPDX-License-Identifier: Apache-2.0 package fabtoken import ( - "encoding/json" - "github.com/hyperledger-labs/fabric-token-sdk/token/driver" "github.com/hyperledger-labs/fabric-token-sdk/token/token" "github.com/pkg/errors" @@ -16,7 +14,6 @@ import ( // TransferSignatureValidate validates the signatures for the inputs spent by an action func TransferSignatureValidate(ctx *Context) error { - ctx.InputTokens = ctx.TransferAction.InputTokens for _, tok := range ctx.InputTokens { ctx.Logger.Debugf("check sender [%s]", driver.Identity(tok.Owner.Raw).UniqueID()) verifier, err := ctx.Deserializer.GetOwnerVerifier(tok.Owner.Raw) diff --git a/token/core/zkatdlog/crypto/validator/validator_transfer.go b/token/core/zkatdlog/crypto/validator/validator_transfer.go index 29ec03469..9462d58bb 100644 --- a/token/core/zkatdlog/crypto/validator/validator_transfer.go +++ b/token/core/zkatdlog/crypto/validator/validator_transfer.go @@ -8,7 +8,6 @@ package validator import ( math "github.com/IBM/mathlib" - "github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/crypto/token" "github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/crypto/transfer" "github.com/hyperledger-labs/fabric-token-sdk/token/driver" "github.com/pkg/errors" diff --git a/token/core/zkatdlog/nogh/driver/interop/state/fabric/state.go b/token/core/zkatdlog/nogh/driver/interop/state/fabric/state.go index e76c949e8..d2c45da5c 100644 --- a/token/core/zkatdlog/nogh/driver/interop/state/fabric/state.go +++ b/token/core/zkatdlog/nogh/driver/interop/state/fabric/state.go @@ -13,6 +13,7 @@ import ( "github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge" fabric3 "github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/state/fabric" "github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/state/fabric/core" + "github.com/hyperledger-labs/fabric-token-sdk/token/services/network/common/rws/keys" "go.uber.org/dig" ) @@ -79,6 +80,7 @@ func NewStateDriver(in struct { in.RelayProvider, in.VaultStore, &Validator{}, + &keys.Translator{}, ), } } diff --git a/token/services/interop/state/fabric/core/state.go b/token/services/interop/state/fabric/core/state.go index 83b0d4aeb..85d621551 100644 --- a/token/services/interop/state/fabric/core/state.go +++ b/token/services/interop/state/fabric/core/state.go @@ -18,13 +18,19 @@ import ( "github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/state/driver" fabric3 "github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/state/fabric" "github.com/hyperledger-labs/fabric-token-sdk/token/services/logging" - "github.com/hyperledger-labs/fabric-token-sdk/token/services/network/common/rws/keys" "github.com/hyperledger-labs/fabric-token-sdk/token/services/network/common/rws/translator" "github.com/hyperledger-labs/fabric-token-sdk/token/services/network/fabric/tcc" "github.com/hyperledger-labs/fabric-token-sdk/token/token" "github.com/pkg/errors" ) +// KeyTranslator is used to translate tokens' concepts into backend's keys. +type KeyTranslator interface { + CreateProofOfExistenceKey(tokenId *token.ID) (string, error) + CreateProofOfNonExistenceKey(tokenID *token.ID, origin string) (string, error) + CreateProofOfMetadataExistenceKey(tokenID *token.ID, origin string) (string, error) +} + type Validator interface { Validate(tok []byte, info *pledge.Info) error } @@ -178,6 +184,7 @@ type StateVerifier struct { PledgeVault PledgeVault GetFabricNetworkService GetFabricNetworkServiceFunc validator Validator + keyTranslator KeyTranslator } func NewStateVerifier( @@ -188,6 +195,7 @@ func NewStateVerifier( networkURL string, relaySelector *fabric.NetworkService, validator Validator, + keyTranslator KeyTranslator, ) (*StateVerifier, error) { if err := fabric3.CheckFabricScheme(networkURL); err != nil { return nil, err @@ -200,6 +208,7 @@ func NewStateVerifier( PledgeVault: PledgeVault, GetFabricNetworkService: GetFabricNetworkService, validator: validator, + keyTranslator: keyTranslator, }, nil } @@ -223,7 +232,7 @@ func (v *StateVerifier) VerifyProofExistence(proofRaw []byte, tokenID *token.ID, return errors.Wrapf(err, "failed to unmarshal claim proof") } - key, err := keys.CreateProofOfExistenceKey(tokenID) + key, err := v.keyTranslator.CreateProofOfExistenceKey(tokenID) if err != nil { return errors.Wrapf(err, "failed to create proof of existence key from token [%s]", tokenID) } @@ -284,7 +293,7 @@ func (v *StateVerifier) VerifyProofNonExistence(proofRaw []byte, tokenID *token. return errors.Wrapf(err, "failed to retrieve RWset") } - key, err := keys.CreateProofOfNonExistenceKey(tokenID, origin) + key, err := v.keyTranslator.CreateProofOfNonExistenceKey(tokenID, origin) if err != nil { return errors.Wrapf(err, "failed creating key for proof of non-existence") } @@ -352,7 +361,7 @@ func (v *StateVerifier) VerifyProofTokenWithMetadataExistence(proofRaw []byte, t return errors.Wrapf(err, "failed to retrieve RWset") } - key, err := keys.CreateProofOfMetadataExistenceKey(tokenID, origin) + key, err := v.keyTranslator.CreateProofOfMetadataExistenceKey(tokenID, origin) if err != nil { return errors.Wrapf(err, "failed creating key for proof of token existence") } @@ -396,6 +405,7 @@ type StateDriver struct { RelayProvider fabric3.RelayProvider VaultStore *pledge.VaultStore Validator Validator + KeyTranslator KeyTranslator } func NewStateDriver( @@ -404,6 +414,7 @@ func NewStateDriver( relayProvider fabric3.RelayProvider, vaultStore *pledge.VaultStore, validator Validator, + keyTranslator KeyTranslator, ) *StateDriver { return &StateDriver{ Logger: logger, @@ -411,6 +422,7 @@ func NewStateDriver( RelayProvider: relayProvider, VaultStore: vaultStore, Validator: validator, + KeyTranslator: keyTranslator, } } @@ -438,5 +450,6 @@ func (d *StateDriver) NewStateVerifier(url string) (driver.StateVerifier, error) url, fns, d.Validator, + d.KeyTranslator, ) } diff --git a/token/services/network/common/rws/keys/keys.go b/token/services/network/common/rws/keys/keys.go index 83c57062f..968025318 100644 --- a/token/services/network/common/rws/keys/keys.go +++ b/token/services/network/common/rws/keys/keys.go @@ -14,8 +14,8 @@ import ( "unicode/utf8" token2 "github.com/hyperledger-labs/fabric-token-sdk/token" - "github.com/hyperledger-labs/fabric-token-sdk/token/token" "github.com/hyperledger-labs/fabric-token-sdk/token/services/network/common/rws/translator" + "github.com/hyperledger-labs/fabric-token-sdk/token/token" "github.com/pkg/errors" ) @@ -94,6 +94,25 @@ func (t *Translator) CreateTransferActionMetadataKey(key string) (translator.Key return createCompositeKey(TransferActionMetadataPrefix, []string{key}) } +func (t *Translator) CreateProofOfExistenceKey(tokenId *token.ID) (string, error) { + id := token2.Hashable(tokenId.String()).String() + return createCompositeKey(ProofOfExistencePrefix, []string{id}) +} + +func (t *Translator) CreateProofOfNonExistenceKey(tokenID *token.ID, origin string) (string, error) { + return createCompositeKey(ProofOfNonExistencePrefix, []string{ + token2.Hashable(tokenID.String()).String(), + token2.Hashable(origin).String(), + }) +} + +func (t *Translator) CreateProofOfMetadataExistenceKey(tokenID *token.ID, origin string) (string, error) { + return createCompositeKey(ProofOfMetadataExistencePrefix, []string{ + token2.Hashable(tokenID.String()).String(), + token2.Hashable(origin).String(), + }) +} + // createCompositeKey and its related functions and consts copied from core/chaincode/shim/chaincode.go func createCompositeKey(objectType string, attributes []string) (translator.Key, error) { if err := validateCompositeKeyAttribute(objectType); err != nil { @@ -137,22 +156,3 @@ func splitCompositeKey(compositeKey string) (translator.Key, []string, error) { } return components[0], components[1:], nil } - -func CreateProofOfExistenceKey(tokenId *token.ID) (string, error) { - id := token2.Hashable(tokenId.String()).String() - return CreateCompositeKey(ProofOfExistencePrefix, []string{id}) -} - -func CreateProofOfNonExistenceKey(tokenID *token.ID, origin string) (string, error) { - return CreateCompositeKey(ProofOfNonExistencePrefix, []string{ - token2.Hashable(tokenID.String()).String(), - token2.Hashable(origin).String(), - }) -} - -func CreateProofOfMetadataExistenceKey(tokenID *token.ID, origin string) (string, error) { - return CreateCompositeKey(ProofOfMetadataExistencePrefix, []string{ - token2.Hashable(tokenID.String()).String(), - token2.Hashable(origin).String(), - }) -} diff --git a/token/services/network/common/rws/translator/prover.go b/token/services/network/common/rws/translator/prover.go index d618b6dd6..bae2bf512 100644 --- a/token/services/network/common/rws/translator/prover.go +++ b/token/services/network/common/rws/translator/prover.go @@ -11,7 +11,6 @@ import ( "time" "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/hash" - "github.com/hyperledger-labs/fabric-token-sdk/token/services/network/common/rws/keys" "github.com/hyperledger-labs/fabric-token-sdk/token/token" "github.com/pkg/errors" ) @@ -36,22 +35,22 @@ type ProofOfTokenMetadataExistence struct { // ProveTokenExists queries whether a token with the given token ID exists func (w *Translator) ProveTokenExists(tokenId *token.ID) error { - key, err := keys.CreateTokenKey(tokenId.TxId, tokenId.Index) + key, err := w.KeyTranslator.CreateOutputKey(tokenId.TxId, tokenId.Index) if err != nil { return err } - tok, err := w.RWSet.GetState(w.namespace, key) + tok, err := w.RWSet.GetState(key) if err != nil { return err } if tok == nil { return errors.Errorf("value at key [%s] is empty", tokenId) } - key, err = keys.CreateProofOfExistenceKey(tokenId) + key, err = w.KeyTranslator.CreateProofOfExistenceKey(tokenId) if err != nil { return err } - err = w.RWSet.SetState(w.namespace, key, tok) + err = w.RWSet.SetState(key, tok) if err != nil { return err } @@ -67,11 +66,11 @@ func (w *Translator) ProveTokenDoesNotExist(tokenID *token.ID, origin string, de if err != nil { return errors.Errorf("failed to marshal token metadata") } - key, err := keys.CreateIssueActionMetadataKey(hash.Hashable(metadata).String()) + key, err := w.KeyTranslator.CreateIssueActionMetadataKey(hash.Hashable(metadata).String()) if err != nil { return err } - tok, err := w.RWSet.GetState(w.namespace, key) + tok, err := w.RWSet.GetState(key) if err != nil { return err } @@ -83,11 +82,11 @@ func (w *Translator) ProveTokenDoesNotExist(tokenID *token.ID, origin string, de if err != nil { return err } - key, err = keys.CreateProofOfNonExistenceKey(tokenID, origin) + key, err = w.KeyTranslator.CreateProofOfNonExistenceKey(tokenID, origin) if err != nil { return err } - err = w.RWSet.SetState(w.namespace, key, raw) + err = w.RWSet.SetState(key, raw) if err != nil { return err } @@ -100,11 +99,11 @@ func (w *Translator) ProveTokenWithMetadataExists(tokenID *token.ID, origin stri if err != nil { return errors.Errorf("failed to marshal token metadata") } - key, err := keys.CreateIssueActionMetadataKey(hash.Hashable(metadata).String()) + key, err := w.KeyTranslator.CreateIssueActionMetadataKey(hash.Hashable(metadata).String()) if err != nil { return err } - tok, err := w.RWSet.GetState(w.namespace, key) + tok, err := w.RWSet.GetState(key) if err != nil { return err } @@ -116,11 +115,11 @@ func (w *Translator) ProveTokenWithMetadataExists(tokenID *token.ID, origin stri if err != nil { return err } - key, err = keys.CreateProofOfMetadataExistenceKey(tokenID, origin) + key, err = w.KeyTranslator.CreateProofOfMetadataExistenceKey(tokenID, origin) if err != nil { return err } - err = w.RWSet.SetState(w.namespace, key, raw) + err = w.RWSet.SetState(key, raw) if err != nil { return err } diff --git a/token/services/network/common/rws/translator/rwset.go b/token/services/network/common/rws/translator/rwset.go index b4efabad7..34be999da 100644 --- a/token/services/network/common/rws/translator/rwset.go +++ b/token/services/network/common/rws/translator/rwset.go @@ -11,6 +11,7 @@ import ( "github.com/gobuffalo/packr/v2/file/resolver/encoding/hex" "github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors" + "github.com/hyperledger-labs/fabric-token-sdk/token/token" ) type ( @@ -42,6 +43,10 @@ type KeyTranslator interface { CreateTransferActionMetadataKey(subkey string) (Key, error) // GetTransferMetadataSubKey returns the subkey in the given transfer action metadata key GetTransferMetadataSubKey(k string) (Key, error) + + CreateProofOfExistenceKey(tokenId *token.ID) (string, error) + CreateProofOfNonExistenceKey(tokenID *token.ID, origin string) (string, error) + CreateProofOfMetadataExistenceKey(tokenID *token.ID, origin string) (string, error) } // RWSet interface, used to read from, and write to, a rwset. @@ -123,6 +128,21 @@ type HashedKeyTranslator struct { KT KeyTranslator } +func (h *HashedKeyTranslator) CreateProofOfExistenceKey(tokenId *token.ID) (string, error) { + // TODO implement me + panic("implement me") +} + +func (h *HashedKeyTranslator) CreateProofOfNonExistenceKey(tokenID *token.ID, origin string) (string, error) { + // TODO implement me + panic("implement me") +} + +func (h *HashedKeyTranslator) CreateProofOfMetadataExistenceKey(tokenID *token.ID, origin string) (string, error) { + // TODO implement me + panic("implement me") +} + func (h *HashedKeyTranslator) CreateTokenRequestKey(id string) (Key, error) { k, err := h.KT.CreateTokenRequestKey(id) if err != nil { diff --git a/token/services/network/fabric/tcc/tcc.go b/token/services/network/fabric/tcc/tcc.go index d98c79370..23f302030 100644 --- a/token/services/network/fabric/tcc/tcc.go +++ b/token/services/network/fabric/tcc/tcc.go @@ -364,9 +364,8 @@ func (cc *TokenChaincode) ProofOfTokenExistenceQuery(idRaw []byte, stub shim.Cha func (cc *TokenChaincode) proveTokenExists(tokenId *token2.ID, stub shim.ChaincodeStubInterface) pb.Response { logger.Infof("proof of existence [%s]", tokenId.String()) logger.Infof("generate proof of existence...") - rwset := &rwsWrapper{stub: stub} - p := translator.New("", rwset, "") - if err := p.ProveTokenExists(tokenId); err != nil { + w := translator.New(stub.GetTxID(), translator.NewRWSetWrapper(&rwsWrapper{stub: stub}, "", stub.GetTxID()), &keys.Translator{}) + if err := w.ProveTokenExists(tokenId); err != nil { return shim.Error(fmt.Sprintf("failed to confirm if token with ID [%s] exists", tokenId)) } logger.Infof("proof of existence...done.") @@ -388,9 +387,8 @@ func (cc *TokenChaincode) ProofOfTokenNonExistenceQuery(reqRaw []byte, stub shim func (cc *TokenChaincode) proveTokenDoesNotExist(tokenID *token2.ID, origin string, deadline time.Time, stub shim.ChaincodeStubInterface) pb.Response { logger.Infof("proof of non existence of token [%s] from network [%s]", tokenID.String(), origin) logger.Infof("generate proof of non-existence...") - rwset := &rwsWrapper{stub: stub} - p := translator.New("", rwset, "") - if err := p.ProveTokenDoesNotExist(tokenID, origin, deadline); err != nil { + w := translator.New(stub.GetTxID(), translator.NewRWSetWrapper(&rwsWrapper{stub: stub}, "", stub.GetTxID()), &keys.Translator{}) + if err := w.ProveTokenDoesNotExist(tokenID, origin, deadline); err != nil { return shim.Error(fmt.Sprintf("failed to confirm if token from network [%s] and with key [%s] does not exist", origin, tokenID.String())) } logger.Infof("proof of non existence...done.") @@ -412,9 +410,8 @@ func (cc *TokenChaincode) ProofOfTokenMetadataExistenceQuery(reqRaw []byte, stub func (cc *TokenChaincode) proveTokenWithMetadataExist(tokenID *token2.ID, origin string, stub shim.ChaincodeStubInterface) pb.Response { logger.Infof("proof of existence of token with metadata [%s] and network [%s]", tokenID.String(), origin) logger.Infof("generate proof of existence...") - rwset := &rwsWrapper{stub: stub} - p := translator.New("", rwset, "") - if err := p.ProveTokenWithMetadataExists(tokenID, origin); err != nil { + w := translator.New(stub.GetTxID(), translator.NewRWSetWrapper(&rwsWrapper{stub: stub}, "", stub.GetTxID()), &keys.Translator{}) + if err := w.ProveTokenWithMetadataExists(tokenID, origin); err != nil { fmt.Println(err.Error()) return shim.Error(fmt.Sprintf("failed to confirm if token from network [%s] and with key [%s] exist", origin, tokenID.String())) }