-
Notifications
You must be signed in to change notification settings - Fork 13
/
swap.go
165 lines (135 loc) · 4.44 KB
/
swap.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
// SPDX-License-Identifier: ISC
// Copyright (c) 2014-2020 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package rpccalls
import (
"encoding/hex"
"golang.org/x/crypto/ed25519"
"github.com/bitmark-inc/bitmarkd/account"
"github.com/bitmark-inc/bitmarkd/command/bitmark-cli/configuration"
"github.com/bitmark-inc/bitmarkd/fault"
"github.com/bitmark-inc/bitmarkd/merkle"
"github.com/bitmark-inc/bitmarkd/pay"
"github.com/bitmark-inc/bitmarkd/rpc/share"
"github.com/bitmark-inc/bitmarkd/transactionrecord"
)
// SwapData - data for a swap request
type SwapData struct {
ShareIdOne string
QuantityOne uint64
OwnerOne *configuration.Private
ShareIdTwo string
QuantityTwo uint64
OwnerTwo *account.Account
BeforeBlock uint64
}
// SwapCountersignData - data for countersigning
type SwapCountersignData struct {
Swap string
Recipient *configuration.Private
}
// SwapReply - JSON data to output after swap completes
type SwapReply struct {
SwapId merkle.Digest `json:"swapId"`
PayId pay.PayId `json:"payId"`
Payments map[string]transactionrecord.PaymentAlternative `json:"payments"`
Commands map[string]string `json:"commands,omitempty"`
}
// SwapSingleSignedReply - result of single signature
type SwapSingleSignedReply struct {
Identity string `json:"identity"`
Swap string `json:"swap"`
}
// Swap - perform swap request
func (client *Client) Swap(swapConfig *SwapData) (*SwapSingleSignedReply, error) {
var shareIdOne merkle.Digest
err := shareIdOne.UnmarshalText([]byte(swapConfig.ShareIdOne))
if nil != err {
return nil, err
}
var shareIdTwo merkle.Digest
err = shareIdTwo.UnmarshalText([]byte(swapConfig.ShareIdTwo))
if nil != err {
return nil, err
}
if 0 == swapConfig.BeforeBlock {
info, err := client.GetBitmarkInfo()
if nil != err {
return nil, err
}
swapConfig.BeforeBlock = info.Block.Height + 100 // allow plenty of time to mine
}
packed, swap, err := makeSwapOneSignature(client.testnet, shareIdOne, swapConfig.QuantityOne, swapConfig.OwnerOne, shareIdTwo, swapConfig.QuantityTwo, swapConfig.OwnerTwo, swapConfig.BeforeBlock)
if nil != err {
return nil, err
}
if nil == swap {
return nil, fault.MakeSwapFailed
}
client.printJson("Swap Request", swap)
response := SwapSingleSignedReply{
Identity: swap.OwnerOne.String(),
Swap: hex.EncodeToString(packed),
}
return &response, nil
}
// CountersignSwap - perform countersigning
func (client *Client) CountersignSwap(swap *transactionrecord.ShareSwap) (*SwapReply, error) {
client.printJson("Swap Request", swap)
var reply share.SwapReply
err := client.client.Call("Share.Swap", swap, &reply)
if nil != err {
return nil, err
}
tpid, err := reply.PayId.MarshalText()
if nil != err {
return nil, err
}
commands := make(map[string]string)
for _, payment := range reply.Payments {
currency := payment[0].Currency
commands[currency.String()] = paymentCommand(client.testnet, currency, string(tpid), payment)
}
client.printJson("Swap Reply", reply)
// make response
response := SwapReply{
SwapId: reply.TxId,
PayId: reply.PayId,
Payments: reply.Payments,
Commands: commands,
}
return &response, nil
}
func makeSwapOneSignature(testnet bool, shareIdOne merkle.Digest, quantityOne uint64, ownerOne *configuration.Private, shareIdTwo merkle.Digest, quantityTwo uint64, ownerTwo *account.Account, beforeBlock uint64) ([]byte, *transactionrecord.ShareSwap, error) {
ownerOneAccount := ownerOne.PrivateKey.Account()
r := transactionrecord.ShareSwap{
ShareIdOne: shareIdOne,
QuantityOne: quantityOne,
OwnerOne: ownerOneAccount,
ShareIdTwo: shareIdTwo,
QuantityTwo: quantityTwo,
OwnerTwo: ownerTwo,
BeforeBlock: beforeBlock,
Signature: nil,
Countersignature: nil,
}
// pack without signature
packed, err := r.Pack(ownerOneAccount)
if nil == err {
return nil, nil, fault.MakeSwapFailed
} else if fault.InvalidSignature != err {
return nil, nil, err
}
// attach signature
signature := ed25519.Sign(ownerOne.PrivateKey.PrivateKeyBytes(), packed)
r.Signature = signature[:]
// include first signature by packing again
packed, err = r.Pack(ownerOneAccount)
if nil == err {
return nil, nil, fault.MakeSwapFailed
} else if fault.InvalidSignature != err {
return nil, nil, err
}
return packed, &r, nil
}