Skip to content

Commit

Permalink
complete rebase
Browse files Browse the repository at this point in the history
Signed-off-by: Angelo De Caro <[email protected]>
  • Loading branch information
adecaro committed Nov 18, 2024
1 parent ac77efb commit 78baa5f
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 55 deletions.
2 changes: 1 addition & 1 deletion integration/token/interop/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
5 changes: 1 addition & 4 deletions token/core/common/interop/pledge/validator_pledge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
2 changes: 2 additions & 0 deletions token/core/fabtoken/driver/interop/state/fabric/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -84,6 +85,7 @@ func NewStateDriver(in struct {
in.RelayProvider,
in.VaultStore,
&Validator{},
&keys.Translator{},
),
}
}
3 changes: 0 additions & 3 deletions token/core/fabtoken/validator_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ 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"
)

// 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)
Expand Down
1 change: 0 additions & 1 deletion token/core/zkatdlog/crypto/validator/validator_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions token/core/zkatdlog/nogh/driver/interop/state/fabric/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -79,6 +80,7 @@ func NewStateDriver(in struct {
in.RelayProvider,
in.VaultStore,
&Validator{},
&keys.Translator{},
),
}
}
21 changes: 17 additions & 4 deletions token/services/interop/state/fabric/core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -178,6 +184,7 @@ type StateVerifier struct {
PledgeVault PledgeVault
GetFabricNetworkService GetFabricNetworkServiceFunc
validator Validator
keyTranslator KeyTranslator
}

func NewStateVerifier(
Expand All @@ -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
Expand All @@ -200,6 +208,7 @@ func NewStateVerifier(
PledgeVault: PledgeVault,
GetFabricNetworkService: GetFabricNetworkService,
validator: validator,
keyTranslator: keyTranslator,
}, nil
}

Expand All @@ -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)
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -396,6 +405,7 @@ type StateDriver struct {
RelayProvider fabric3.RelayProvider
VaultStore *pledge.VaultStore
Validator Validator
KeyTranslator KeyTranslator
}

func NewStateDriver(
Expand All @@ -404,13 +414,15 @@ func NewStateDriver(
relayProvider fabric3.RelayProvider,
vaultStore *pledge.VaultStore,
validator Validator,
keyTranslator KeyTranslator,
) *StateDriver {
return &StateDriver{
Logger: logger,
FNSProvider: FNSProvider,
RelayProvider: relayProvider,
VaultStore: vaultStore,
Validator: validator,
KeyTranslator: keyTranslator,
}
}

Expand Down Expand Up @@ -438,5 +450,6 @@ func (d *StateDriver) NewStateVerifier(url string) (driver.StateVerifier, error)
url,
fns,
d.Validator,
d.KeyTranslator,
)
}
40 changes: 20 additions & 20 deletions token/services/network/common/rws/keys/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
})
}
25 changes: 12 additions & 13 deletions token/services/network/common/rws/translator/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
20 changes: 20 additions & 0 deletions token/services/network/common/rws/translator/rwset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit 78baa5f

Please sign in to comment.