Skip to content

Commit e3270a7

Browse files
committed
add application public metadata to token transfer
Signed-off-by: Arne Rutjes <[email protected]>
1 parent e220864 commit e3270a7

File tree

6 files changed

+49
-3
lines changed

6 files changed

+49
-3
lines changed

integration/token/fungible/support.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,15 @@ func TransferCash(network *integration.Infrastructure, sender *token3.NodeRefere
387387
return TransferCashForTMSID(network, sender, wallet, typ, amount, receiver, auditor, nil, expectedErrorMsgs...)
388388
}
389389

390+
func TransferCashWithOpts(network *integration.Infrastructure, sender *token3.NodeReference, wallet string, typ token.Type, amount uint64, receiver *token3.NodeReference, auditor *token3.NodeReference, opts []token2.TransferOption, expectedErrorMsgs ...string) string {
391+
return TransferCashForTMSIDWithOpts(network, sender, wallet, typ, amount, receiver, auditor, nil, opts, expectedErrorMsgs...)
392+
}
393+
390394
func TransferCashForTMSID(network *integration.Infrastructure, sender *token3.NodeReference, wallet string, typ token.Type, amount uint64, receiver *token3.NodeReference, auditor *token3.NodeReference, tmsId *token2.TMSID, expectedErrorMsgs ...string) string {
395+
return TransferCashForTMSIDWithOpts(network, sender, wallet, typ, amount, receiver, auditor, nil, nil, expectedErrorMsgs...)
396+
}
397+
398+
func TransferCashForTMSIDWithOpts(network *integration.Infrastructure, sender *token3.NodeReference, wallet string, typ token.Type, amount uint64, receiver *token3.NodeReference, auditor *token3.NodeReference, tmsId *token2.TMSID, opts []token2.TransferOption, expectedErrorMsgs ...string) string {
391399
txidBoxed, err := network.Client(sender.ReplicaName()).CallView("transfer", common.JSONMarshall(&views.Transfer{
392400
Auditor: auditor.Id(),
393401
Wallet: wallet,
@@ -396,6 +404,7 @@ func TransferCashForTMSID(network *integration.Infrastructure, sender *token3.No
396404
Recipient: network.Identity(receiver.Id()),
397405
RecipientEID: receiver.Id(),
398406
TMSID: tmsId,
407+
TransferOpts: opts,
399408
}))
400409
if len(expectedErrorMsgs) == 0 {
401410
Expect(err).NotTo(HaveOccurred())

integration/token/fungible/tests.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
common2 "github.com/hyperledger-labs/fabric-token-sdk/integration/token/common"
2727
dlog "github.com/hyperledger-labs/fabric-token-sdk/integration/token/fungible/dlogstress/support"
2828
"github.com/hyperledger-labs/fabric-token-sdk/integration/token/fungible/views"
29+
token4 "github.com/hyperledger-labs/fabric-token-sdk/token"
2930
dlognoghv1 "github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/nogh/v1/setup"
3031
"github.com/hyperledger-labs/fabric-token-sdk/token/services/ttx"
3132
"github.com/hyperledger-labs/fabric-token-sdk/token/services/ttxdb"
@@ -401,7 +402,11 @@ func TestAll(network *integration.Infrastructure, auditorId string, onRestart On
401402
Restart(network, false, onRestart, alice)
402403

403404
t8 := time.Now()
404-
TransferCash(network, alice, "", "USD", 111, bob, auditor)
405+
key := fmt.Sprintf("%d", t8.UnixMilli())
406+
val := []byte("public ledger data")
407+
opts := []token4.TransferOption{token4.WithPublicMetadata(key, val)}
408+
BobAcceptedTransactions[3:4][0].PublicMetadata = map[string][]byte{key: val}
409+
TransferCashWithOpts(network, alice, "", "USD", 111, bob, auditor, opts)
405410
t9 := time.Now()
406411
CheckAuditedTransactions(network, auditor, AuditedTransactions[5:7], &t8, &t9)
407412
CheckSpending(network, alice, "", "USD", auditor, 111)

integration/token/fungible/views/transfer.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ type Transfer struct {
6969
NotAnonymous bool
7070
// Metadata contains application metadata to append to the transaction
7171
Metadata map[string][]byte
72+
// TransferOpts contains additional options to use on the Transfer action
73+
TransferOpts []token2.TransferOption
7274
}
7375

7476
type TransferView struct {
@@ -140,13 +142,20 @@ func (t *TransferView) Call(context view.Context) (txID interface{}, err error)
140142
// It is also possible to pass a custom token selector to the Transfer function by using the relative opt:
141143
// token2.WithTokenSelector(selector).
142144
span.AddEvent("append_transfer")
145+
146+
opts := []token2.TransferOption{
147+
token2.WithTokenIDs(t.TokenIDs...),
148+
token2.WithRestRecipientIdentity(t.SenderChangeRecipientData),
149+
}
150+
if len(t.TransferOpts) > 0 {
151+
opts = append(opts, t.TransferOpts...)
152+
}
143153
err = tx.Transfer(
144154
senderWallet,
145155
t.Type,
146156
[]uint64{t.Amount},
147157
[]view.Identity{recipient},
148-
token2.WithTokenIDs(t.TokenIDs...),
149-
token2.WithRestRecipientIdentity(t.SenderChangeRecipientData),
158+
opts...,
150159
)
151160
assert.NoError(err, "failed adding transfer action [%d:%s]", t.Amount, t.Recipient)
152161

token/core/zkatdlog/nogh/v1/validator/validator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func New(
6464
TransferUpgradeWitnessValidate,
6565
TransferZKProofValidate,
6666
TransferHTLCValidate,
67+
TransferApplicationDataValidate,
6768
}
6869
transferValidators = append(transferValidators, extraValidators...)
6970

token/core/zkatdlog/nogh/v1/validator/validator_transfer.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package validator
88

99
import (
1010
"bytes"
11+
"strings"
1112
"time"
1213

1314
math "github.com/IBM/mathlib"
@@ -173,3 +174,15 @@ func TransferHTLCValidate(ctx *Context) error {
173174
}
174175
return nil
175176
}
177+
178+
// TransferApplicationDataValidate accepts any metadata in the "application" namespace.
179+
// This gives the user of the Token SDK the option to attach public data to the token
180+
// transaction.
181+
func TransferApplicationDataValidate(ctx *Context) error {
182+
for key := range ctx.TransferAction.Metadata {
183+
if strings.HasPrefix(key, "application.") {
184+
ctx.CountMetadataKey(key)
185+
}
186+
}
187+
return nil
188+
}

token/request.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
const (
2424
TransferMetadataPrefix = meta.TransferMetadataPrefix
25+
PublicMetadataPrefix = "application."
2526
)
2627

2728
type Binder interface {
@@ -104,6 +105,14 @@ func WithTokenSelector(selector Selector) TransferOption {
104105
// WithTransferMetadata sets transfer action metadata
105106
func WithTransferMetadata(key string, value []byte) TransferOption {
106107
return WithTransferAttribute(TransferMetadataPrefix+key, value)
108+
109+
}
110+
111+
// WithPublicMetadata adds any data to the public ledger that may be relevant to the application.
112+
// It is also made available to the participants as part of the TransactionRecord.
113+
// The transaction fails if the key already exists on the ledger. The value is not validated.
114+
func WithPublicMetadata(key string, value []byte) TransferOption {
115+
return WithTransferAttribute(PublicMetadataPrefix+key, value)
107116
}
108117

109118
// WithTokenIDs sets the tokens ids to transfer

0 commit comments

Comments
 (0)