-
Notifications
You must be signed in to change notification settings - Fork 11
/
cashu.go
170 lines (148 loc) · 3.84 KB
/
cashu.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
package cashu
import (
"encoding/hex"
"github.com/cashubtc/cashu-feni/lightning"
"github.com/cashubtc/cashu-feni/lightning/lnbits"
cashuLog "github.com/cashubtc/cashu-feni/log"
"github.com/google/uuid"
"strings"
"time"
)
type ProofsUsed struct {
Amount uint64 `json:"amount"`
Secret string `json:"secret" gorm:"primaryKey"`
C string `json:"C"`
TimeUsed time.Time
}
type ProofStatus int
const (
ProofStatusSpent ProofStatus = iota
ProofStatusPending
ProofStatusReserved
)
type Proof struct {
Id string `json:"id"`
Amount uint64 `json:"amount"`
Secret string `json:"secret" gorm:"primaryKey"`
C string `json:"C"`
Status ProofStatus `json:"-"`
Reserved bool `json:"-,omitempty"`
Script *P2SHScript `gorm:"-" json:"script,omitempty" structs:"Script,omitempty"`
SendId uuid.UUID `json:"-,omitempty" structs:"SendId,omitempty"`
TimeCreated time.Time `json:"-,omitempty" structs:"TimeCreated,omitempty"`
TimeReserved time.Time `json:"-,omitempty" structs:"TimeReserved,omitempty"`
}
func IsPay2ScriptHash(s string) bool {
return len(strings.Split(s, "P2SH:")) == 2
}
func (p Proof) Log() map[string]interface{} {
return cashuLog.ToMap(p)
}
type P2SHScript struct {
Script string `json:"script"`
Signature string `json:"signature"`
Address string `json:"address"`
}
func (p Proof) Decode() ([]byte, error) {
return hex.DecodeString(p.C)
}
type Proofs []Proof
type Promise struct {
B_b string `json:"C_b" gorm:"primaryKey"`
C_c string `json:"C_c"`
Amount uint64 `json:"amount"`
}
func (p Promise) Log() map[string]interface{} {
return cashuLog.ToMap(p)
}
type BlindedMessages []BlindedMessage
type BlindedMessage struct {
Amount uint64 `json:"amount"`
B_ string `json:"B_"`
}
type BlindedSignature struct {
Id string `json:"id"`
Amount uint64 `json:"amount"`
C_ string `json:"C_"`
}
type ErrorResponse struct {
Err string `json:"error"`
Code int `json:"code"`
}
type ErrorOptions func(err *ErrorResponse)
func WithCode(code int) ErrorOptions {
return func(err *ErrorResponse) {
err.Code = code
}
}
func NewErrorResponse(err error, options ...ErrorOptions) ErrorResponse {
e := ErrorResponse{
Err: err.Error(),
}
for _, o := range options {
o(&e)
}
return e
}
func (e ErrorResponse) String() string {
return cashuLog.ToJson(e)
}
func (e ErrorResponse) Error() string {
return e.Err
}
// CreateInvoice will generate a blank invoice
func CreateInvoice() lightning.Invoicer {
if lightning.Config.Lightning.Enabled {
return lnbits.NewInvoice()
}
return nil
}
type Mint struct {
Url string `json:"url"`
KeySets []string `json:"ks"`
}
type MintResponse struct {
Promises []BlindedSignature `json:"promises"`
}
type MintRequest struct {
Outputs BlindedMessages `json:"outputs"`
}
type MeltResponse struct {
Paid bool `json:"paid"`
Preimage string `json:"preimage"`
Change []BlindedSignature `json:"change,omitempty"`
}
type GetKeysResponse map[int]string
type SplitResponse struct {
Fst []BlindedSignature `json:"fst"`
Snd []BlindedSignature `json:"snd"`
}
type GetKeySetsResponse struct {
KeySets []string `json:"keysets"`
}
type GetMintResponse struct {
Pr string `json:"pr"`
Hash string `json:"hash"`
}
type MeltRequest struct {
Proofs Proofs `json:"proofs"`
Pr string `json:"pr"`
Outputs []BlindedMessage `json:"outputs,omitempty"`
}
type CheckSpendableRequest struct {
Proofs Proofs `json:"proofs"`
}
type CheckSpendableResponse struct {
Spendable []bool `json:"spendable"`
}
type CheckFeesResponse struct {
Fee uint64 `json:"fee"`
}
type CheckFeesRequest struct {
Pr string `json:"pr"`
}
type SplitRequest struct {
Proofs Proofs `json:"proofs"`
Amount uint64 `json:"amount"`
Outputs []BlindedMessage `json:"outputs"`
}