-
Notifications
You must be signed in to change notification settings - Fork 13
/
blocktransfer.go
138 lines (112 loc) · 4.12 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
// 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/currency"
"github.com/bitmark-inc/bitmarkd/fault"
"github.com/bitmark-inc/bitmarkd/merkle"
"github.com/bitmark-inc/bitmarkd/pay"
"github.com/bitmark-inc/bitmarkd/rpc/blockowner"
"github.com/bitmark-inc/bitmarkd/transactionrecord"
)
// BlockTransferData - data for a block transfer request
type BlockTransferData struct {
Payments currency.Map
Owner *configuration.Private
NewOwner *account.Account
TxId string
}
// BlockTransferReply - 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"`
}
// BlockTransferSingleSignedReply - reply if performing a single signature transfer
type BlockTransferSingleSignedReply struct {
Identity string `json:"identity"`
BlockTransfer string `json:"blockTransfer"`
}
// SingleSignedBlockTransfer - perform a transfer
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, fault.MakeBlockTransferFailed
}
client.printJson("BlockTransfer Request", blockTransfer)
response := BlockTransferSingleSignedReply{
Identity: blockTransfer.GetOwner().String(),
BlockTransfer: hex.EncodeToString(packed),
}
return &response, nil
}
// CountersignBlockTransfer - perform a transfer
func (client *Client) CountersignBlockTransfer(blockTransfer *transactionrecord.BlockOwnerTransfer) (*BlockTransferReply, error) {
var reply blockowner.TransferReply
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 *configuration.Private, newOwner *account.Account) ([]byte, *transactionrecord.BlockOwnerTransfer, error) {
r := transactionrecord.BlockOwnerTransfer{
Link: link,
Version: 1,
Payments: payments,
Owner: newOwner,
Signature: nil,
Countersignature: nil,
}
ownerAccount := owner.PrivateKey.Account()
// pack without signature
packed, err := r.Pack(ownerAccount)
if nil == err {
return nil, nil, fault.MakeBlockTransferFailed
} else if fault.InvalidSignature != err {
return nil, nil, err
}
// attach signature
signature := ed25519.Sign(owner.PrivateKey.PrivateKeyBytes(), packed)
r.Signature = signature[:]
// include first signature by packing again
packed, err = r.Pack(ownerAccount)
if nil == err {
return nil, nil, fault.MakeBlockTransferFailed
} else if fault.InvalidSignature != err {
return nil, nil, err
}
return packed, &r, nil
}