Skip to content

Commit

Permalink
fixup! asset transfer
Browse files Browse the repository at this point in the history
Signed-off-by: Angelo De Caro <[email protected]>
  • Loading branch information
adecaro committed May 28, 2024
1 parent 8b376c7 commit 312865e
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 2 deletions.
4 changes: 2 additions & 2 deletions token/core/common/interop/pledge/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package pledge
import (
"encoding/json"

"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/hash"
"github.com/hyperledger-labs/fabric-token-sdk/token/core/common"
"github.com/hyperledger-labs/fabric-token-sdk/token/driver"
"github.com/hyperledger-labs/fabric-token-sdk/token/token"
"github.com/pkg/errors"
Expand Down Expand Up @@ -41,10 +41,10 @@ func IssueActionMetadata(attributes map[string][]byte, opts *driver.IssueOptions
}
if metadata != nil {
marshalled, err := json.Marshal(metadata)
key := hash.Hashable(marshalled).String()
if err != nil {
return nil, errors.Wrapf(err, "failed marshaling metadata; origin network [%s]; origin tokenID [%s]", metadata.OriginNetwork, metadata.OriginTokenID)
}
key := common.Hashable(marshalled).String()
attributes[key] = marshalled
attributes[key+"proof_of_claim"] = proof
}
Expand Down
148 changes: 148 additions & 0 deletions token/core/common/interop/pledge/metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package pledge

import (
"encoding/json"
"testing"

"github.com/hyperledger-labs/fabric-token-sdk/token/core/common"
"github.com/hyperledger-labs/fabric-token-sdk/token/driver"
"github.com/hyperledger-labs/fabric-token-sdk/token/token"
"github.com/stretchr/testify/assert"
)

func TestIssueActionMetadata(t *testing.T) {
tests := []struct {
name string
attributes map[string][]byte
opts *driver.IssueOptions
expected map[string][]byte
errExpected bool
}{
{
name: "empty attributes",
attributes: map[string][]byte{},
opts: &driver.IssueOptions{
Attributes: map[interface{}]interface{}{},
},
expected: map[string][]byte{},
errExpected: false,
},
{
name: "missing tokenID",
attributes: map[string][]byte{
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/network": []byte("network"),
},
opts: &driver.IssueOptions{
Attributes: map[interface{}]interface{}{
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/network": "network",
},
},
expected: map[string][]byte{
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/network": []byte("network"),
},
errExpected: false,
},
{
name: "missing network",
attributes: map[string][]byte{
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/tokenID": []byte("tokenID"),
},
opts: &driver.IssueOptions{
Attributes: map[interface{}]interface{}{
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/tokenID": "tokenID",
},
},
expected: map[string][]byte{
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/tokenID": []byte("tokenID"),
},
errExpected: false,
},
{
name: "valid attributes",
attributes: map[string][]byte{
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/network": []byte("network"),
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/tokenID": []byte("tokenID"),
},
opts: &driver.IssueOptions{
Attributes: map[interface{}]interface{}{
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/network": "network",
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/tokenID": &token.ID{
TxId: "a_transaction",
Index: 2,
},
},
},
expected: func() map[string][]byte {
metadata := &IssueMetadata{
OriginTokenID: &token.ID{
TxId: "a_transaction",
Index: 2,
},
OriginNetwork: "network",
}
marshalled, err := json.Marshal(metadata)
assert.NoError(t, err)
key := common.Hashable(marshalled).String()
res := map[string][]byte{
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/network": []byte("network"),
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/tokenID": []byte("tokenID"),
key: marshalled,
key + "proof_of_claim": nil,
}
return res
}(),
errExpected: false,
},
{
name: "valid attributes with proof",
attributes: map[string][]byte{},
opts: &driver.IssueOptions{
Attributes: map[interface{}]interface{}{
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/network": "network",
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/tokenID": &token.ID{
TxId: "a_transaction",
Index: 2,
},
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/proof": []byte("proof"),
},
},
expected: func() map[string][]byte {
metadata := &IssueMetadata{
OriginTokenID: &token.ID{
TxId: "a_transaction",
Index: 2,
},
OriginNetwork: "network",
}
marshalled, err := json.Marshal(metadata)
assert.NoError(t, err)
key := common.Hashable(marshalled).String()
res := map[string][]byte{
key: marshalled,
key + "proof_of_claim": []byte("proof"),
}
return res
}(),
errExpected: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := IssueActionMetadata(test.attributes, test.opts)
if test.errExpected && err == nil {
t.Errorf("expected error but got none")
}
if !test.errExpected && err != nil {
t.Errorf("got error %v", err)
}
assert.Equal(t, test.expected, result)
})
}
}

0 comments on commit 312865e

Please sign in to comment.