-
Notifications
You must be signed in to change notification settings - Fork 13
/
blocktransfer.go
138 lines (111 loc) · 3.94 KB
/
blocktransfer.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
// Copyright (c) 2014-2018 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/currency"
"github.com/bitmark-inc/bitmarkd/fault"
"github.com/bitmark-inc/bitmarkd/keypair"
"github.com/bitmark-inc/bitmarkd/merkle"
"github.com/bitmark-inc/bitmarkd/pay"
"github.com/bitmark-inc/bitmarkd/rpc"
"github.com/bitmark-inc/bitmarkd/transactionrecord"
)
var (
ErrMakeBlockTransferFail = fault.ProcessError("make block transfer failed")
ErrNotBlockTransferRecord = fault.InvalidError("not block transfer record")
)
type BlockTransferData struct {
Payments currency.Map
Owner *keypair.KeyPair
NewOwner *keypair.KeyPair
TxId string
}
// JSON data to output after blockTransfer completes
type BlockTransferReply struct {
BlockTransferId merkle.Digest `json:"blockTransferId"`
PayId pay.PayId `json:"payId"`
Payments map[string]transactionrecord.PaymentAlternative `json:"payments"`
Commands map[string]string `json:"commands,omitempty"`
}
type BlockTransferSingleSignedReply struct {
Identity string `json:"identity"`
BlockTransfer string `json:"blockTransfer"`
}
func (client *Client) SingleSignedBlockTransfer(blockTransferConfig *BlockTransferData) (*BlockTransferSingleSignedReply, error) {
var link merkle.Digest
err := link.UnmarshalText([]byte(blockTransferConfig.TxId))
if nil != err {
return nil, err
}
packed, blockTransfer, err := makeBlockTransferOneSignature(client.testnet, link, blockTransferConfig.Payments, blockTransferConfig.Owner, blockTransferConfig.NewOwner)
if nil != err {
return nil, err
}
if nil == blockTransfer {
return nil, ErrMakeBlockTransferFail
}
client.printJson("BlockTransfer Request", blockTransfer)
response := BlockTransferSingleSignedReply{
Identity: blockTransfer.GetOwner().String(),
BlockTransfer: hex.EncodeToString(packed),
}
return &response, nil
}
func (client *Client) CountersignBlockTransfer(blockTransfer *transactionrecord.BlockOwnerTransfer) (*BlockTransferReply, error) {
var reply rpc.BlockOwnerTransferReply
err := client.client.Call("BlockOwner.Transfer", blockTransfer, &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("BlockTransfer Reply", reply)
// make response
response := BlockTransferReply{
BlockTransferId: reply.TxId,
PayId: reply.PayId,
Payments: reply.Payments,
Commands: commands,
}
return &response, nil
}
func makeBlockTransferOneSignature(testnet bool, link merkle.Digest, payments currency.Map, owner *keypair.KeyPair, newOwner *keypair.KeyPair) ([]byte, *transactionrecord.BlockOwnerTransfer, error) {
newOwnerAddress := makeAddress(newOwner, testnet)
r := transactionrecord.BlockOwnerTransfer{
Link: link,
Version: 1,
Payments: payments,
Owner: newOwnerAddress,
Signature: nil,
Countersignature: nil,
}
ownerAddress := makeAddress(owner, testnet)
// pack without signature
packed, err := r.Pack(ownerAddress)
if nil == err {
return nil, nil, ErrMakeBlockTransferFail
} else if fault.ErrInvalidSignature != err {
return nil, nil, err
}
// attach signature
signature := ed25519.Sign(owner.PrivateKey, packed)
r.Signature = signature[:]
// include first signature by packing again
packed, err = r.Pack(ownerAddress)
if nil == err {
return nil, nil, ErrMakeBlockTransferFail
} else if fault.ErrInvalidSignature != err {
return nil, nil, err
}
return packed, &r, nil
}