-
Notifications
You must be signed in to change notification settings - Fork 15
/
rune.go
205 lines (178 loc) · 5.15 KB
/
rune.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2024 The BxELab studyzy Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package runestone
import (
"errors"
"fmt"
"math/big"
"strings"
"github.com/btcsuite/btcd/wire"
"lukechampine.com/uint128"
)
type Rune struct {
Value uint128.Uint128
}
func Uint128FromString(s string) uint128.Uint128 {
i, _ := uint128.FromString(s)
return i
}
var RESERVED = Uint128FromString("6402364363415443603228541259936211926")
var STEPS = []uint128.Uint128{
Uint128FromString("0"),
Uint128FromString("26"),
Uint128FromString("702"),
Uint128FromString("18278"),
Uint128FromString("475254"),
Uint128FromString("12356630"),
Uint128FromString("321272406"),
Uint128FromString("8353082582"),
Uint128FromString("217180147158"),
Uint128FromString("5646683826134"),
Uint128FromString("146813779479510"),
Uint128FromString("3817158266467286"),
Uint128FromString("99246114928149462"),
Uint128FromString("2580398988131886038"),
Uint128FromString("67090373691429037014"),
Uint128FromString("1744349715977154962390"),
Uint128FromString("45353092615406029022166"),
Uint128FromString("1179180408000556754576342"),
Uint128FromString("30658690608014475618984918"),
Uint128FromString("797125955808376366093607894"),
Uint128FromString("20725274851017785518433805270"),
Uint128FromString("538857146126462423479278937046"),
Uint128FromString("14010285799288023010461252363222"),
Uint128FromString("364267430781488598271992561443798"),
Uint128FromString("9470953200318703555071806597538774"),
Uint128FromString("246244783208286292431866971536008150"),
Uint128FromString("6402364363415443603228541259936211926"),
Uint128FromString("166461473448801533683942072758341510102"),
}
func NewRune(value uint128.Uint128) Rune {
return Rune{Value: value}
}
func (r Rune) N() uint128.Uint128 {
return r.Value
}
const SUBSIDY_HALVING_INTERVAL uint32 = 210_000
func FirstRuneHeight(network wire.BitcoinNet) uint32 {
var multiplier uint32
switch network {
case wire.MainNet:
multiplier = 4
case wire.TestNet, wire.SimNet:
multiplier = 0
case wire.TestNet3:
multiplier = 12
default:
multiplier = 0
}
return SUBSIDY_HALVING_INTERVAL * multiplier
}
func MinimumAtHeight(chain wire.BitcoinNet, height uint64) Rune {
offset := height + 1
const interval uint32 = SUBSIDY_HALVING_INTERVAL / 12
start := FirstRuneHeight(chain)
end := start + SUBSIDY_HALVING_INTERVAL
if offset < uint64(start) {
return Rune{STEPS[12]}
}
if offset >= uint64(end) {
return Rune{}
}
progress := offset - uint64(start)
length := 12 - progress/uint64(interval)
endStep := STEPS[length-1]
startStep := STEPS[length]
remainder := progress % uint64(interval)
//val := startStep - ((startStep - endStep) * remainder / uint64(interval))
val := startStep.Sub(startStep.Sub(endStep).Mul(uint128.From64(remainder)).Div(uint128.From64(uint64(interval))))
return Rune{val}
}
func (r Rune) IsReserved() bool {
return r.Value.Cmp(RESERVED) >= 0
}
func Reserved(block uint64, tx uint32) Rune {
v := RESERVED.Add(uint128.From64(block).Lsh(32).Or(uint128.From64(uint64(tx))))
return Rune{
Value: v,
}
}
func (r Rune) Commitment() []byte {
bytes := r.Value.Big().Bytes()
// Reverse bytes to get little-endian representation
for i, j := 0, len(bytes)-1; i < j; i, j = i+1, j-1 {
bytes[i], bytes[j] = bytes[j], bytes[i]
}
end := len(bytes)
for end > 0 && bytes[end-1] == 0 {
end--
}
return bytes[:end]
}
func (r Rune) String() string {
n := r.Value
if n.Cmp(uint128.Max) == 0 {
return "BCGDENLQRQWDSLRUGSNLBTMFIJAV"
}
n = n.Add64(1)
var symbol strings.Builder
for n.Cmp(uint128.Zero) > 0 {
index := n.Sub64(1).Mod64(26)
symbol.WriteByte("ABCDEFGHIJKLMNOPQRSTUVWXYZ"[index])
n = n.Sub64(1).Div64(26)
}
// Reverse the string
runes := []rune(symbol.String())
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
func RuneFromString(s string) (*Rune, error) {
x := big.NewInt(0)
tmp := big.NewInt(0)
for i, c := range s {
if i > 0 {
x.Add(x, tmp.SetInt64(1))
}
x.Mul(x, tmp.SetInt64(26))
if x.BitLen() > 128 {
return nil, errors.New("overflow")
}
if c >= 'A' && c <= 'Z' {
x.Add(x, tmp.SetInt64(int64(c-'A')))
if x.BitLen() > 128 {
return nil, errors.New("overflow")
}
} else {
return nil, fmt.Errorf("invalid character `%c`", c)
}
}
u := uint128.FromBig(x)
return &Rune{Value: u}, nil
}
type Error struct {
Character rune
Range bool
}
func (e Error) Error() string {
if e.Range {
return "name out of range"
}
return fmt.Sprintf("invalid character `%c`", e.Character)
}
// MarshalJSON json marshal
func (r Rune) MarshalJSON() ([]byte, error) {
return []byte(`"` + r.String() + `"`), nil
}