@@ -25,6 +25,7 @@ import (
2525 "github.com/hyperledger/firefly/internal/data"
2626 "github.com/hyperledger/firefly/internal/i18n"
2727 "github.com/hyperledger/firefly/internal/identity"
28+ "github.com/hyperledger/firefly/internal/privatemessaging"
2829 "github.com/hyperledger/firefly/internal/retry"
2930 "github.com/hyperledger/firefly/internal/syncasync"
3031 "github.com/hyperledger/firefly/internal/txcommon"
@@ -42,11 +43,10 @@ type Manager interface {
4243 GetTokenTransfers (ctx context.Context , ns , typeName , poolName string , filter database.AndFilter ) ([]* fftypes.TokenTransfer , * database.FilterResult , error )
4344 MintTokens (ctx context.Context , ns , typeName , poolName string , transfer * fftypes.TokenTransfer , waitConfirm bool ) (* fftypes.TokenTransfer , error )
4445 BurnTokens (ctx context.Context , ns , typeName , poolName string , transfer * fftypes.TokenTransfer , waitConfirm bool ) (* fftypes.TokenTransfer , error )
45- TransferTokens (ctx context.Context , ns , typeName , poolName string , transfer * fftypes.TokenTransfer , waitConfirm bool ) (* fftypes.TokenTransfer , error )
46+ TransferTokens (ctx context.Context , ns , typeName , poolName string , transfer * fftypes.TokenTransferInput , waitConfirm bool ) (* fftypes.TokenTransfer , error )
4647
4748 // Bound token callbacks
4849 TokenPoolCreated (tk tokens.Plugin , pool * fftypes.TokenPool , protocolTxID string , additionalInfo fftypes.JSONObject ) error
49- TokensTransferred (tk tokens.Plugin , transfer * fftypes.TokenTransfer , protocolTxID string , additionalInfo fftypes.JSONObject ) error
5050
5151 Start () error
5252 WaitStop ()
@@ -59,13 +59,14 @@ type assetManager struct {
5959 data data.Manager
6060 syncasync syncasync.Bridge
6161 broadcast broadcast.Manager
62+ messaging privatemessaging.Manager
6263 tokens map [string ]tokens.Plugin
6364 retry retry.Retry
6465 txhelper txcommon.Helper
6566}
6667
67- func NewAssetManager (ctx context.Context , di database.Plugin , im identity.Manager , dm data.Manager , sa syncasync.Bridge , bm broadcast.Manager , ti map [string ]tokens.Plugin ) (Manager , error ) {
68- if di == nil || im == nil || sa == nil || bm == nil || ti == nil {
68+ func NewAssetManager (ctx context.Context , di database.Plugin , im identity.Manager , dm data.Manager , sa syncasync.Bridge , bm broadcast.Manager , pm privatemessaging. Manager , ti map [string ]tokens.Plugin ) (Manager , error ) {
69+ if di == nil || im == nil || sa == nil || bm == nil || pm == nil || ti == nil {
6970 return nil , i18n .NewError (ctx , i18n .MsgInitializationNilDepError )
7071 }
7172 am := & assetManager {
@@ -75,6 +76,7 @@ func NewAssetManager(ctx context.Context, di database.Plugin, im identity.Manage
7576 data : dm ,
7677 syncasync : sa ,
7778 broadcast : bm ,
79+ messaging : pm ,
7880 tokens : ti ,
7981 retry : retry.Retry {
8082 InitialDelay : config .GetDuration (config .AssetManagerRetryInitialDelay ),
@@ -119,21 +121,13 @@ func retrieveTokenPoolCreateInputs(ctx context.Context, op *fftypes.Operation, p
119121 return nil
120122}
121123
124+ // Note: the counterpart to below (retrieveTokenTransferInputs) lives in the events package
122125func addTokenTransferInputs (op * fftypes.Operation , transfer * fftypes.TokenTransfer ) {
123126 op .Input = fftypes.JSONObject {
124127 "id" : transfer .LocalID .String (),
125128 }
126129}
127130
128- func retrieveTokenTransferInputs (ctx context.Context , op * fftypes.Operation , transfer * fftypes.TokenTransfer ) (err error ) {
129- input := & op .Input
130- transfer .LocalID , err = fftypes .ParseUUID (ctx , input .GetString ("id" ))
131- if err != nil {
132- return err
133- }
134- return nil
135- }
136-
137131func (am * assetManager ) CreateTokenPool (ctx context.Context , ns string , typeName string , pool * fftypes.TokenPool , waitConfirm bool ) (* fftypes.TokenPool , error ) {
138132 return am .createTokenPoolWithID (ctx , fftypes .NewUUID (), ns , typeName , pool , waitConfirm )
139133}
@@ -291,7 +285,25 @@ func (am *assetManager) BurnTokens(ctx context.Context, ns, typeName, poolName s
291285 return am .transferTokensWithID (ctx , fftypes .NewUUID (), ns , typeName , poolName , transfer , waitConfirm )
292286}
293287
294- func (am * assetManager ) TransferTokens (ctx context.Context , ns , typeName , poolName string , transfer * fftypes.TokenTransfer , waitConfirm bool ) (* fftypes.TokenTransfer , error ) {
288+ func (am * assetManager ) sendTransferMessage (ctx context.Context , ns string , in * fftypes.MessageInOut ) (* fftypes.Message , error ) {
289+ allowedTypes := []fftypes.FFEnum {
290+ fftypes .MessageTypeTransferBroadcast ,
291+ fftypes .MessageTypeTransferPrivate ,
292+ }
293+ if in .Header .Type == "" {
294+ in .Header .Type = fftypes .MessageTypeTransferBroadcast
295+ }
296+ switch in .Header .Type {
297+ case fftypes .MessageTypeTransferBroadcast :
298+ return am .broadcast .BroadcastMessage (ctx , ns , in , false )
299+ case fftypes .MessageTypeTransferPrivate :
300+ return am .messaging .SendMessage (ctx , ns , in , false )
301+ default :
302+ return nil , i18n .NewError (ctx , i18n .MsgInvalidMessageType , allowedTypes )
303+ }
304+ }
305+
306+ func (am * assetManager ) TransferTokens (ctx context.Context , ns , typeName , poolName string , transfer * fftypes.TokenTransferInput , waitConfirm bool ) (* fftypes.TokenTransfer , error ) {
295307 transfer .Type = fftypes .TokenTransferTypeTransfer
296308 if transfer .Key == "" {
297309 org , err := am .identity .GetLocalOrganization (ctx )
@@ -309,7 +321,17 @@ func (am *assetManager) TransferTokens(ctx context.Context, ns, typeName, poolNa
309321 if transfer .From == transfer .To {
310322 return nil , i18n .NewError (ctx , i18n .MsgCannotTransferToSelf )
311323 }
312- return am .transferTokensWithID (ctx , fftypes .NewUUID (), ns , typeName , poolName , transfer , waitConfirm )
324+
325+ if transfer .Message != nil {
326+ msg , err := am .sendTransferMessage (ctx , ns , transfer .Message )
327+ if err != nil {
328+ return nil , err
329+ }
330+ transfer .MessageHash = msg .Hash
331+ }
332+
333+ result , err := am .transferTokensWithID (ctx , fftypes .NewUUID (), ns , typeName , poolName , & transfer .TokenTransfer , waitConfirm )
334+ return result , err
313335}
314336
315337func (am * assetManager ) transferTokensWithID (ctx context.Context , id * fftypes.UUID , ns , typeName , poolName string , transfer * fftypes.TokenTransfer , waitConfirm bool ) (* fftypes.TokenTransfer , error ) {
0 commit comments