-
Notifications
You must be signed in to change notification settings - Fork 6
/
escrow.go
99 lines (91 loc) · 2.56 KB
/
escrow.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
package ledger
import (
"fmt"
escrowpb "github.com/tron-us/go-btfs-common/protos/escrow"
ledgerpb "github.com/tron-us/go-btfs-common/protos/ledger"
ic "github.com/libp2p/go-libp2p-core/crypto"
)
func NewPayinRequest(payinId string, payerPubkey ic.PubKey, state *ledgerpb.SignedChannelState) (
*escrowpb.PayinRequest, error) {
raw, err := ic.RawFull(payerPubkey)
if err != nil {
return nil, err
}
return &escrowpb.PayinRequest{
PayinId: payinId,
BuyerAddress: raw,
BuyerChannelState: state,
}, nil
}
func NewSignedPayinRequest(req *escrowpb.PayinRequest, sig []byte) *escrowpb.SignedPayinRequest {
return &escrowpb.SignedPayinRequest{
Request: req,
BuyerSignature: sig,
}
}
func NewContractID(id string, key ic.PubKey) (*escrowpb.ContractID, error) {
raw, err := ic.RawFull(key)
if err != nil {
return nil, err
}
return &escrowpb.ContractID{
ContractId: id,
Address: raw,
}, nil
}
func NewSingedContractID(id *escrowpb.ContractID, sig []byte) *escrowpb.SignedContractID {
return &escrowpb.SignedContractID{
Data: id,
Signature: sig,
}
}
func NewEscrowContract(id string, payerPubKey ic.PubKey, hostPubKey ic.PubKey, authPubKey ic.PubKey,
amount int64, ps escrowpb.Schedule, period int32, contrType escrowpb.ContractType,
contingentAmount int64, storageLength int) (*escrowpb.EscrowContract, error) {
payerAddr, err := ic.RawFull(payerPubKey)
if err != nil {
return nil, err
}
var hostAddr []byte
if hostPubKey != nil {
hostAddr, err = ic.RawFull(hostPubKey)
if err != nil {
return nil, err
}
}
authAddress, err := ic.RawFull(authPubKey)
if err != nil {
return nil, err
}
numPayouts := 1
switch ps {
case escrowpb.Schedule_MONTHLY:
numPayouts = storageLength / 30
case escrowpb.Schedule_QUARTERLY:
numPayouts = storageLength / 30 / 3
case escrowpb.Schedule_ANNUALLY:
numPayouts = storageLength / 30 / 3 / 4
case escrowpb.Schedule_CUSTOMIZED:
numPayouts = storageLength / int(period)
default:
return nil, fmt.Errorf("invalide PayoutSchedule: %v", ps)
}
if numPayouts == 0 {
numPayouts = 1
}
return &escrowpb.EscrowContract{
ContractId: id,
BuyerAddress: payerAddr,
SellerAddress: hostAddr,
AuthAddress: authAddress,
Amount: amount,
CollateralAmount: 0,
WithholdAmount: 0,
TokenType: escrowpb.TokenType_BTT,
PayoutSchedule: ps,
NumPayouts: int32(numPayouts),
CustomizePayoutPeriod: period,
Type: contrType,
ContingentAmount: contingentAmount,
}, nil
}