forked from ChainSafe/chainbridge-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
85 lines (73 loc) · 2.01 KB
/
message.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
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: LGPL-3.0-only
package msg
import (
"fmt"
"math/big"
)
type ChainId uint8
type TransferType string
type ResourceId [32]byte
func (r ResourceId) Hex() string {
return fmt.Sprintf("%x", r)
}
type Nonce uint64
func (n Nonce) Big() *big.Int {
return big.NewInt(int64(n))
}
var FungibleTransfer TransferType = "FungibleTransfer"
var NonFungibleTransfer TransferType = "NonFungibleTransfer"
var GenericTransfer TransferType = "GenericTransfer"
// Message is used as a generic format to communicate between chains
type Message struct {
Source ChainId // Source where message was initiated
Destination ChainId // Destination chain of message
Type TransferType // type of bridge transfer
DepositNonce Nonce // Nonce for the deposit
ResourceId ResourceId
Payload []interface{} // data associated with event sequence
}
func NewFungibleTransfer(source, dest ChainId, nonce Nonce, amount *big.Int, resourceId ResourceId, recipient []byte) Message {
return Message{
Source: source,
Destination: dest,
Type: FungibleTransfer,
DepositNonce: nonce,
ResourceId: resourceId,
Payload: []interface{}{
amount.Bytes(),
recipient,
},
}
}
func NewNonFungibleTransfer(source, dest ChainId, nonce Nonce, resourceId ResourceId, tokenId *big.Int, recipient, metadata []byte) Message {
return Message{
Source: source,
Destination: dest,
Type: NonFungibleTransfer,
DepositNonce: nonce,
ResourceId: resourceId,
Payload: []interface{}{
tokenId.Bytes(),
recipient,
metadata,
},
}
}
func NewGenericTransfer(source, dest ChainId, nonce Nonce, resourceId ResourceId, metadata []byte) Message {
return Message{
Source: source,
Destination: dest,
Type: GenericTransfer,
DepositNonce: nonce,
ResourceId: resourceId,
Payload: []interface{}{
metadata,
},
}
}
func ResourceIdFromSlice(in []byte) ResourceId {
var res ResourceId
copy(res[:], in)
return res
}