-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathmessages.go
399 lines (363 loc) · 11.2 KB
/
messages.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package swap
import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"github.com/elementsproject/peerswap/messages"
)
var (
AlreadyExistsError = errors.New("Message already exists")
InvalidLengthError = errors.New("Hex string is of invalid length")
InvalidNetworkError = errors.New("Invalid network")
InvalidScidError = errors.New("Invalid Scid")
AssetOrNetworkSetError = errors.New("Either asset or network must be set")
)
func NewInvalidLengthError(paramName string, expected, actual int) error {
return fmt.Errorf("Param %s is of invalid length expected: %v, actual %v", paramName, expected, actual)
}
type SwapInRequestMessage struct {
// ProtocolVersion is the version of the PeerSwap peer protocol the sending
// node uses.
ProtocolVersion uint8 `json:"protocol_version"`
// SwapId is a randomly generated 32 byte string that must be kept the same
// through the whole process of a swap and serves as an identifier for a
// specific swap.
SwapId *SwapId `json:"swap_id"`
// Network is the desired on-chain network to use. This can be:
// Bitcoin: mainnet, testnet, signet, regtest
// Liquid: The field is left blank as the asset id also defines the bitcoinNetwork.
Network string `json:"network"`
// Asset is the desired on-chain asset to use. This can be:
// Bitcoin: The field is left blank.
// Liquid: The asset id of the networks Bitcoin asset.
Asset string `json:"asset"`
// Scid is the short channel id in human readable format, defined by BOLT#7
// with x as separator, e.g. 539268x845x1.
Scid string `json:"scid"`
// Amount is The amount in Sats that is asked for.
Amount uint64 `json:"amount"`
Pubkey string `json:"pubkey"`
PremiumLimit int64 `json:"acceptable_premium"`
}
func (s SwapInRequestMessage) MessageType() messages.MessageType {
return messages.MESSAGETYPE_SWAPINREQUEST
}
func (s SwapInRequestMessage) Validate(swap *SwapData) error {
err := validateHexString("pubkey", s.Pubkey, 33)
if err != nil {
return err
}
err = validateAssetAndNetwork(s.Asset, s.Network)
if err != nil {
return err
}
if s.Asset != "" {
err = validateHexString("asset", s.Asset, 33)
if err != nil {
return err
}
}
if s.Network != "" {
err = validateNetwork(s.Network)
if err != nil {
return err
}
}
err = validateScid(s.Scid)
if err != nil {
return err
}
return nil
}
func validateScid(scid string) error {
var prefix string
if strings.Contains(scid, "x") {
prefix = "x"
} else if strings.Contains(scid, ":") {
prefix = ":"
} else {
return InvalidScidError
}
parts := strings.Split(scid, prefix)
if len(parts) != 3 {
return InvalidScidError
}
_, err := strconv.Atoi(parts[0])
_, err = strconv.Atoi(parts[1])
_, err = strconv.Atoi(parts[2])
if err != nil {
return InvalidScidError
}
return nil
}
func validateAssetAndNetwork(asset string, network string) error {
if (asset == "" && network == "") || (asset != "" && network != "") {
return AssetOrNetworkSetError
}
if asset != "" {
err := validateHexString("asset", asset, 33)
if err != nil {
return err
}
}
if network != "" {
err := validateNetwork(network)
if err != nil {
return err
}
}
return nil
}
func validateNetwork(network string) error {
switch network {
case "mainnet":
fallthrough
case "testnet":
fallthrough
case "testnet3":
fallthrough
case "signet":
fallthrough
case "regtest":
return nil
}
return InvalidNetworkError
}
func validateHexString(paramName, hexString string, expectedLength int) error {
data, err := hex.DecodeString(hexString)
if err != nil {
return err
}
if len(data) != expectedLength {
return NewInvalidLengthError(paramName, expectedLength, len(data))
}
return nil
}
func (s SwapInRequestMessage) ApplyToSwapData(swap *SwapData) error {
if swap.SwapInRequest != nil {
return AlreadyExistsError
}
swap.SwapInRequest = &s
return nil
}
// SwapInAgreementMessage is the response by the swap-in peer if he accepts the
// swap.
type SwapInAgreementMessage struct {
// ProtocolVersion is the version of the PeerSwap peer protocol the sending
// node uses.
ProtocolVersion uint8 `json:"protocol_version"`
// SwapId is a randomly generated 32 byte string that must be kept the same
// through the whole process of a swap and serves as an identifier for a
// specific swap.
SwapId *SwapId `json:"swap_id"`
// Pubkey is a 33 byte compressed public key used for the spending paths in
// the opening_transaction.
Pubkey string `json:"pubkey"`
// Premium is a compensation in Sats that the swap partner wants to be payed
// in order to participate in the swap.
Premium int64 `json:"premium"`
}
func (s SwapInAgreementMessage) Validate(swap *SwapData) error {
err := validateHexString("pubkey", s.Pubkey, 33)
if err != nil {
return err
}
return nil
}
func (s SwapInAgreementMessage) MessageType() messages.MessageType {
return messages.MESSAGETYPE_SWAPINAGREEMENT
}
func (s SwapInAgreementMessage) ApplyToSwapData(swap *SwapData) error {
if swap.SwapInAgreement != nil {
return AlreadyExistsError
}
swap.SwapInAgreement = &s
return nil
}
// SwapOutRequestMessage gets send when a peer wants to start a new swap.
type SwapOutRequestMessage struct {
// ProtocolVersion is the version of the PeerSwap peer protocol the sending
// node uses.
ProtocolVersion uint8 `json:"protocol_version"`
// SwapId is a randomly generated 32 byte string that must be kept the same
// through the whole process of a swap and serves as an identifier for a
// specific swap.
SwapId *SwapId `json:"swap_id"`
// Asset is the desired on-chain asset to use. This can be:
// Bitcoin: The field is left blank.
// Liquid: The asset id of the networks Bitcoin asset.
Asset string `json:"asset"`
// Network is the desired on-chain network to use. This can be:
// Bitcoin: mainnet, testnet, signet, regtest
// Liquid: The field is left blank as the asset id also defines the bitcoinNetwork.
Network string `json:"network"`
// Scid is the short channel id in human readable format, defined by BOLT#7
// with x as separator, e.g. 539268x845x1.
Scid string `json:"scid"`
// Amount is The amount in Sats that is asked for.
Amount uint64 `json:"amount"`
// Pubkey is a 33 byte compressed public key used for the spending paths in
// the opening_transaction.
Pubkey string `json:"pubkey"`
PremiumLimit int64 `json:"acceptable_premium"`
}
func (s SwapOutRequestMessage) Validate(swap *SwapData) error {
err := validateHexString("pubkey", s.Pubkey, 33)
if err != nil {
return err
}
err = validateAssetAndNetwork(s.Asset, s.Network)
if err != nil {
return err
}
err = validateScid(s.Scid)
if err != nil {
return err
}
return nil
}
func (s SwapOutRequestMessage) MessageType() messages.MessageType {
return messages.MESSAGETYPE_SWAPOUTREQUEST
}
func (s SwapOutRequestMessage) ApplyToSwapData(swap *SwapData) error {
if swap.SwapOutRequest != nil {
return AlreadyExistsError
}
swap.SwapOutRequest = &s
return nil
}
// SwapOutAgreementMessage is the response by the swap-out peer if he accepts
// the swap it contains an Invoice that the swap-out initiator must pay.
type SwapOutAgreementMessage struct {
// ProtocolVersion is the version of the PeerSwap peer protocol the sending
// node uses.
ProtocolVersion uint8 `json:"protocol_version"`
// SwapId is a randomly generated 32 byte string that must be kept the same
// through the whole process of a swap and serves as an identifier for a
// specific swap.
SwapId *SwapId `json:"swap_id"`
// Pubkey is a 33 byte compressed public key used for the spending paths in
// the opening_transaction.
Pubkey string `json:"pubkey"`
// Payreq is a BOLT#11 invoice with an amount that covers the fee expenses
// for the on-chain transactions.
Payreq string
// Premium is a compensation in Sats that the swap partner wants to be payed
// in order to participate in the swap.
Premium int64 `json:"premium"`
}
func (s SwapOutAgreementMessage) Validate(swap *SwapData) error {
err := validateHexString("pubkey", s.Pubkey, 33)
if err != nil {
return err
}
return nil
}
func (s SwapOutAgreementMessage) MessageType() messages.MessageType {
return messages.MESSAGETYPE_SWAPOUTAGREEMENT
}
func (s SwapOutAgreementMessage) ApplyToSwapData(swap *SwapData) error {
if swap.SwapOutAgreement != nil {
return AlreadyExistsError
}
swap.SwapOutAgreement = &s
return nil
}
// OpeningTxBroadcastedMessage is the message sent by the creator of the opening
// tx.
type OpeningTxBroadcastedMessage struct {
// SwapId is the unique identifier of the swap.
SwapId *SwapId `json:"swap_id"`
// Payreq is the invoice as described in BOLT#11 that the responder is
// requested to pay.
Payreq string `json:"payreq"`
// TxId is the transaction id of the opening_transaction broadcasted by the
// initiator.
TxId string `json:"tx_id"`
// ScriptOut is the transaction output that contains the opening_transaction
// output script for the swap.
ScriptOut uint32 `json:"script_out"`
// BlindingKey:
// Bitcoin: Blank.
// Liquid BitcoinNetwork: Is the 32 byte blinding key to un-blind the outputs of
//the opening_transaction.
BlindingKey string `json:"blinding_key"`
}
func (s OpeningTxBroadcastedMessage) Validate(swap *SwapData) error {
if swap.GetChain() == l_btc_chain {
err := validateHexString("blinding_key", s.BlindingKey, 32)
if err != nil {
return err
}
}
err := validateHexString("txId", s.TxId, 32)
if err != nil {
return err
}
return nil
}
func (t OpeningTxBroadcastedMessage) MessageType() messages.MessageType {
return messages.MESSAGETYPE_OPENINGTXBROADCASTED
}
func (m OpeningTxBroadcastedMessage) ApplyToSwapData(swap *SwapData) error {
if swap.OpeningTxBroadcasted != nil {
return AlreadyExistsError
}
swap.OpeningTxBroadcasted = &m
return nil
}
// CancelMessage is the message sent by a peer if he wants to / has to cancel
// the swap
type CancelMessage struct {
// SwapId is the unique identifier of the swap.
SwapId *SwapId `json:"swap_id"`
// Message is a hint to why the swap was canceled.
Message string `json:"message"`
}
func (e CancelMessage) MessageType() messages.MessageType {
return messages.MESSAGETYPE_CANCELED
}
func (s CancelMessage) Validate(swap *SwapData) error {
return nil
}
func (m CancelMessage) ApplyToSwapData(swap *SwapData) error {
swap.Cancel = &m
return nil
}
// CoopCloseMessage is the message sent by the transaction taker if he wants to
// cancel the swap, but allow the maker a quick close
type CoopCloseMessage struct {
// SwapId is the unique identifier of the swap.
SwapId *SwapId `json:"swap_id"`
// Message is a hint to why the swap was canceled.
Message string `json:"message"`
// privkey is the private key to the pubkey that is used to build the opening_transaction.
Privkey string `json:"privkey"`
}
func (c CoopCloseMessage) MessageType() messages.MessageType {
return messages.MESSAGETYPE_COOPCLOSE
}
func (s CoopCloseMessage) Validate(swap *SwapData) error {
err := validateHexString("privkey", s.Privkey, 32)
if err != nil {
return err
}
return nil
}
func (m CoopCloseMessage) ApplyToSwapData(swap *SwapData) error {
if swap.CoopClose != nil {
return AlreadyExistsError
}
swap.CoopClose = &m
return nil
}
func MarshalPeerswapMessage(msg PeerMessage) ([]byte, int, error) {
msgBytes, err := json.Marshal(msg)
if err != nil {
return nil, 0, err
}
return msgBytes, int(msg.MessageType()), nil
}