-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifier.go
96 lines (81 loc) · 2.05 KB
/
notifier.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package notifier
import (
"context"
"crypto/md5"
"encoding/base64"
"encoding/json"
"io"
"math/big"
"github.com/fox-one/mixin-sdk-go"
"github.com/fox-one/mtg/core"
"github.com/fox-one/pkg/logger"
"github.com/gofrs/uuid"
)
func New(
system *core.System,
assets core.AssetStore,
messages core.MessageStore,
) core.Notifier {
return ¬ifier{
messages: messages,
system: system,
assets: assets,
}
}
type notifier struct {
system *core.System
assets core.AssetStore
messages core.MessageStore
}
func (n *notifier) Snapshot(ctx context.Context, transfer *core.Transfer, signedTx string) error {
log := logger.FromContext(ctx)
tx, err := mixin.TransactionFromRaw(signedTx)
if err != nil {
log.WithError(err).Debugln("decode transaction from raw tx failed")
return nil
}
hash, err := tx.TransactionHash()
if err != nil {
return nil
}
traceID := mixinRawTransactionTraceId(hash.String(), 0)
if len(transfer.Opponents) != 1 {
log.Debugln("transfer opponents is not 1")
return nil
}
asset, err := n.assets.Find(ctx, transfer.AssetID)
if err != nil {
return err
}
card := mixin.AppCardMessage{
AppID: n.system.ClientID,
IconURL: asset.Logo,
Title: transfer.Amount.String(),
Description: asset.Symbol,
Action: "mixin://snapshots?trace=" + traceID,
}
data, _ := json.Marshal(card)
recipientID := transfer.Opponents[0]
req := &mixin.MessageRequest{
ConversationID: mixin.UniqueConversationID(n.system.ClientID, recipientID),
RecipientID: recipientID,
MessageID: traceID,
Category: mixin.MessageCategoryAppCard,
Data: base64.StdEncoding.EncodeToString(data),
}
return n.messages.Create(ctx, []*core.Message{core.BuildMessage(req)})
}
func mixinRawTransactionTraceId(hash string, index uint8) string {
h := md5.New()
_, _ = io.WriteString(h, hash)
b := new(big.Int).SetInt64(int64(index))
h.Write(b.Bytes())
s := h.Sum(nil)
s[6] = (s[6] & 0x0f) | 0x30
s[8] = (s[8] & 0x3f) | 0x80
sid, err := uuid.FromBytes(s)
if err != nil {
panic(err)
}
return sid.String()
}