@secondlayer/stacks 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +92 -0
- package/dist/shared/stacks-02actsrx.cjs +17 -0
- package/dist/shared/stacks-02actsrx.cjs.map +10 -0
- package/dist/shared/stacks-1aqm7c2h.cjs +110 -0
- package/dist/shared/stacks-1aqm7c2h.cjs.map +11 -0
- package/dist/shared/stacks-24h5k94q.cjs +74 -0
- package/dist/shared/stacks-24h5k94q.cjs.map +11 -0
- package/dist/shared/stacks-2ar4prfz.cjs +328 -0
- package/dist/shared/stacks-2ar4prfz.cjs.map +11 -0
- package/dist/shared/stacks-4py5pm0r.cjs +237 -0
- package/dist/shared/stacks-4py5pm0r.cjs.map +10 -0
- package/dist/shared/stacks-5tprhns9.cjs +54 -0
- package/dist/shared/stacks-5tprhns9.cjs.map +11 -0
- package/dist/shared/stacks-61rvzkz1.cjs +132 -0
- package/dist/shared/stacks-61rvzkz1.cjs.map +17 -0
- package/dist/shared/stacks-709tra19.js +30 -0
- package/dist/shared/stacks-709tra19.js.map +10 -0
- package/dist/shared/stacks-74k8cxgb.cjs +567 -0
- package/dist/shared/stacks-74k8cxgb.cjs.map +13 -0
- package/dist/shared/stacks-8m9byk2j.js +7 -0
- package/dist/shared/stacks-8m9byk2j.js.map +9 -0
- package/dist/shared/stacks-8yykgdww.js +125 -0
- package/dist/shared/stacks-8yykgdww.js.map +11 -0
- package/dist/shared/stacks-9g0jhab3.cjs +40 -0
- package/dist/shared/stacks-9g0jhab3.cjs.map +10 -0
- package/dist/shared/stacks-an5k8n4t.cjs +291 -0
- package/dist/shared/stacks-an5k8n4t.cjs.map +23 -0
- package/dist/shared/stacks-b008c3w8.cjs +214 -0
- package/dist/shared/stacks-b008c3w8.cjs.map +11 -0
- package/dist/shared/stacks-bnvfmt7y.cjs +31 -0
- package/dist/shared/stacks-bnvfmt7y.cjs.map +9 -0
- package/dist/shared/stacks-bycy6t9k.js +22 -0
- package/dist/shared/stacks-bycy6t9k.js.map +10 -0
- package/dist/shared/stacks-d1fmkv3p.js +258 -0
- package/dist/shared/stacks-d1fmkv3p.js.map +13 -0
- package/dist/shared/stacks-dr5en54r.cjs +119 -0
- package/dist/shared/stacks-dr5en54r.cjs.map +10 -0
- package/dist/shared/stacks-dxhntr76.js +278 -0
- package/dist/shared/stacks-dxhntr76.js.map +10 -0
- package/dist/shared/stacks-evnzth1q.cjs +35 -0
- package/dist/shared/stacks-evnzth1q.cjs.map +10 -0
- package/dist/shared/stacks-f3tk2k5s.js +27 -0
- package/dist/shared/stacks-f3tk2k5s.js.map +9 -0
- package/dist/shared/stacks-f7wdrzs3.js +285 -0
- package/dist/shared/stacks-f7wdrzs3.js.map +11 -0
- package/dist/shared/stacks-fcaefc5b.cjs +16 -0
- package/dist/shared/stacks-fcaefc5b.cjs.map +10 -0
- package/dist/shared/stacks-g9mcjcqg.cjs +3 -0
- package/dist/shared/stacks-g9mcjcqg.cjs.map +9 -0
- package/dist/shared/stacks-gck1sq7h.cjs +28 -0
- package/dist/shared/stacks-gck1sq7h.cjs.map +10 -0
- package/dist/shared/stacks-gjvbq6w7.js +46 -0
- package/dist/shared/stacks-gjvbq6w7.js.map +10 -0
- package/dist/shared/stacks-j2cy7qtt.js +237 -0
- package/dist/shared/stacks-j2cy7qtt.js.map +11 -0
- package/dist/shared/stacks-k08tgzyp.cjs +232 -0
- package/dist/shared/stacks-k08tgzyp.cjs.map +13 -0
- package/dist/shared/stacks-k2p5tv6x.js +37 -0
- package/dist/shared/stacks-k2p5tv6x.js.map +10 -0
- package/dist/shared/stacks-k58k970n.js +234 -0
- package/dist/shared/stacks-k58k970n.js.map +10 -0
- package/dist/shared/stacks-n2g56wgg.js +338 -0
- package/dist/shared/stacks-n2g56wgg.js.map +23 -0
- package/dist/shared/stacks-nqytwfcc.js +243 -0
- package/dist/shared/stacks-nqytwfcc.js.map +11 -0
- package/dist/shared/stacks-qcae5852.js +19 -0
- package/dist/shared/stacks-qcae5852.js.map +10 -0
- package/dist/shared/stacks-qt8ehebn.js +76 -0
- package/dist/shared/stacks-qt8ehebn.js.map +11 -0
- package/dist/shared/stacks-qvg7c24g.cjs +232 -0
- package/dist/shared/stacks-qvg7c24g.cjs.map +10 -0
- package/dist/shared/stacks-r1t6g3c3.cjs +241 -0
- package/dist/shared/stacks-r1t6g3c3.cjs.map +11 -0
- package/dist/shared/stacks-tjxmrssa.js +282 -0
- package/dist/shared/stacks-tjxmrssa.js.map +11 -0
- package/dist/shared/stacks-v9jqcpd4.js +18 -0
- package/dist/shared/stacks-v9jqcpd4.js.map +10 -0
- package/dist/shared/stacks-vdfag81a.cjs +121 -0
- package/dist/shared/stacks-vdfag81a.cjs.map +10 -0
- package/dist/shared/stacks-vdfs3e5v.js +134 -0
- package/dist/shared/stacks-vdfs3e5v.js.map +10 -0
- package/dist/shared/stacks-y3dh7hv1.js +60 -0
- package/dist/shared/stacks-y3dh7hv1.js.map +11 -0
- package/dist/shared/stacks-ya3x01yd.cjs +16 -0
- package/dist/shared/stacks-ya3x01yd.cjs.map +10 -0
- package/dist/shared/stacks-yfgsm6ap.js +599 -0
- package/dist/shared/stacks-yfgsm6ap.js.map +13 -0
- package/dist/shared/stacks-yfveb68e.js +121 -0
- package/dist/shared/stacks-yfveb68e.js.map +10 -0
- package/dist/shared/stacks-zm0b3tgy.js +160 -0
- package/dist/shared/stacks-zm0b3tgy.js.map +17 -0
- package/dist/shared/stacks-zyv9qqme.cjs +249 -0
- package/dist/shared/stacks-zyv9qqme.cjs.map +11 -0
- package/dist/src/accounts/index.cjs +90 -0
- package/dist/src/accounts/index.cjs.map +12 -0
- package/dist/src/accounts/index.d.cts +47 -0
- package/dist/src/accounts/index.d.ts +47 -0
- package/dist/src/accounts/index.js +104 -0
- package/dist/src/accounts/index.js.map +12 -0
- package/dist/src/actions/index.cjs +36 -0
- package/dist/src/actions/index.cjs.map +9 -0
- package/dist/src/actions/index.d.cts +886 -0
- package/dist/src/actions/index.d.ts +886 -0
- package/dist/src/actions/index.js +81 -0
- package/dist/src/actions/index.js.map +9 -0
- package/dist/src/bns/index.cjs +590 -0
- package/dist/src/bns/index.cjs.map +15 -0
- package/dist/src/bns/index.d.cts +241 -0
- package/dist/src/bns/index.d.ts +241 -0
- package/dist/src/bns/index.js +611 -0
- package/dist/src/bns/index.js.map +15 -0
- package/dist/src/chains/index.cjs +13 -0
- package/dist/src/chains/index.cjs.map +9 -0
- package/dist/src/chains/index.d.cts +53 -0
- package/dist/src/chains/index.d.ts +53 -0
- package/dist/src/chains/index.js +18 -0
- package/dist/src/chains/index.js.map +9 -0
- package/dist/src/clarity/index.cjs +3 -0
- package/dist/src/clarity/index.cjs.map +9 -0
- package/dist/src/clarity/index.d.cts +821 -0
- package/dist/src/clarity/index.d.ts +821 -0
- package/dist/src/clarity/index.js +145 -0
- package/dist/src/clarity/index.js.map +9 -0
- package/dist/src/connect/index.cjs +480 -0
- package/dist/src/connect/index.cjs.map +19 -0
- package/dist/src/connect/index.d.cts +110 -0
- package/dist/src/connect/index.d.ts +110 -0
- package/dist/src/connect/index.js +448 -0
- package/dist/src/connect/index.js.map +19 -0
- package/dist/src/connect/walletconnect/index.cjs +1322 -0
- package/dist/src/connect/walletconnect/index.cjs.map +18 -0
- package/dist/src/connect/walletconnect/index.d.cts +132 -0
- package/dist/src/connect/walletconnect/index.d.ts +132 -0
- package/dist/src/connect/walletconnect/index.js +1290 -0
- package/dist/src/connect/walletconnect/index.js.map +18 -0
- package/dist/src/index.cjs +557 -0
- package/dist/src/index.cjs.map +21 -0
- package/dist/src/index.d.cts +847 -0
- package/dist/src/index.d.ts +847 -0
- package/dist/src/index.js +629 -0
- package/dist/src/index.js.map +21 -0
- package/dist/src/postconditions/index.cjs +3 -0
- package/dist/src/postconditions/index.cjs.map +9 -0
- package/dist/src/postconditions/index.d.cts +91 -0
- package/dist/src/postconditions/index.d.ts +91 -0
- package/dist/src/postconditions/index.js +12 -0
- package/dist/src/postconditions/index.js.map +9 -0
- package/dist/src/pox/index.cjs +860 -0
- package/dist/src/pox/index.cjs.map +15 -0
- package/dist/src/pox/index.d.cts +244 -0
- package/dist/src/pox/index.d.ts +244 -0
- package/dist/src/pox/index.js +877 -0
- package/dist/src/pox/index.js.map +15 -0
- package/dist/src/stackingdao/index.cjs +300 -0
- package/dist/src/stackingdao/index.cjs.map +13 -0
- package/dist/src/stackingdao/index.d.cts +179 -0
- package/dist/src/stackingdao/index.d.ts +179 -0
- package/dist/src/stackingdao/index.js +318 -0
- package/dist/src/stackingdao/index.js.map +13 -0
- package/dist/src/subscriptions/index.cjs +14 -0
- package/dist/src/subscriptions/index.cjs.map +9 -0
- package/dist/src/subscriptions/index.d.cts +250 -0
- package/dist/src/subscriptions/index.d.ts +250 -0
- package/dist/src/subscriptions/index.js +21 -0
- package/dist/src/subscriptions/index.js.map +9 -0
- package/dist/src/transactions/index.cjs +50 -0
- package/dist/src/transactions/index.cjs.map +9 -0
- package/dist/src/transactions/index.d.cts +412 -0
- package/dist/src/transactions/index.d.ts +412 -0
- package/dist/src/transactions/index.js +101 -0
- package/dist/src/transactions/index.js.map +9 -0
- package/dist/src/utils/index.cjs +168 -0
- package/dist/src/utils/index.cjs.map +11 -0
- package/dist/src/utils/index.d.cts +140 -0
- package/dist/src/utils/index.d.ts +140 -0
- package/dist/src/utils/index.js +222 -0
- package/dist/src/utils/index.js.map +11 -0
- package/package.json +105 -0
|
@@ -0,0 +1,877 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getContract
|
|
3
|
+
} from "../../shared/stacks-vdfs3e5v.js";
|
|
4
|
+
import"../../shared/stacks-zm0b3tgy.js";
|
|
5
|
+
import"../../shared/stacks-709tra19.js";
|
|
6
|
+
import"../../shared/stacks-d1fmkv3p.js";
|
|
7
|
+
import {
|
|
8
|
+
Pc
|
|
9
|
+
} from "../../shared/stacks-yfveb68e.js";
|
|
10
|
+
import"../../shared/stacks-f7wdrzs3.js";
|
|
11
|
+
import"../../shared/stacks-yfgsm6ap.js";
|
|
12
|
+
import"../../shared/stacks-j2cy7qtt.js";
|
|
13
|
+
import"../../shared/stacks-8yykgdww.js";
|
|
14
|
+
import"../../shared/stacks-v9jqcpd4.js";
|
|
15
|
+
import"../../shared/stacks-bycy6t9k.js";
|
|
16
|
+
import"../../shared/stacks-gjvbq6w7.js";
|
|
17
|
+
import {
|
|
18
|
+
hexToBytes
|
|
19
|
+
} from "../../shared/stacks-nqytwfcc.js";
|
|
20
|
+
import"../../shared/stacks-8m9byk2j.js";
|
|
21
|
+
|
|
22
|
+
// src/pox/abi.ts
|
|
23
|
+
var poxAddressTuple = {
|
|
24
|
+
tuple: [
|
|
25
|
+
{ name: "version", type: { buff: { length: 1 } } },
|
|
26
|
+
{ name: "hashbytes", type: { buff: { length: 32 } } }
|
|
27
|
+
]
|
|
28
|
+
};
|
|
29
|
+
var POX_ABI = {
|
|
30
|
+
functions: [
|
|
31
|
+
{
|
|
32
|
+
name: "get-pox-info",
|
|
33
|
+
access: "read-only",
|
|
34
|
+
args: [],
|
|
35
|
+
outputs: {
|
|
36
|
+
response: {
|
|
37
|
+
ok: {
|
|
38
|
+
tuple: [
|
|
39
|
+
{ name: "reward-cycle-id", type: "uint128" },
|
|
40
|
+
{ name: "min-amount-ustx", type: "uint128" },
|
|
41
|
+
{ name: "prepare-cycle-length", type: "uint128" },
|
|
42
|
+
{ name: "first-burnchain-block-height", type: "uint128" },
|
|
43
|
+
{ name: "reward-cycle-length", type: "uint128" },
|
|
44
|
+
{ name: "total-liquid-supply-ustx", type: "uint128" }
|
|
45
|
+
]
|
|
46
|
+
},
|
|
47
|
+
error: "none"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
name: "get-stacker-info",
|
|
53
|
+
access: "read-only",
|
|
54
|
+
args: [{ name: "stacker", type: "principal" }],
|
|
55
|
+
outputs: {
|
|
56
|
+
optional: {
|
|
57
|
+
tuple: [
|
|
58
|
+
{ name: "pox-addr", type: poxAddressTuple },
|
|
59
|
+
{ name: "lock-period", type: "uint128" },
|
|
60
|
+
{ name: "first-reward-cycle", type: "uint128" },
|
|
61
|
+
{
|
|
62
|
+
name: "reward-set-indexes",
|
|
63
|
+
type: { list: { type: "uint128", length: 12 } }
|
|
64
|
+
},
|
|
65
|
+
{ name: "delegated-to", type: { optional: "principal" } }
|
|
66
|
+
]
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
name: "get-delegation-info",
|
|
72
|
+
access: "read-only",
|
|
73
|
+
args: [{ name: "stacker", type: "principal" }],
|
|
74
|
+
outputs: {
|
|
75
|
+
optional: {
|
|
76
|
+
tuple: [
|
|
77
|
+
{ name: "amount-ustx", type: "uint128" },
|
|
78
|
+
{ name: "delegated-to", type: "principal" },
|
|
79
|
+
{ name: "until-burn-ht", type: { optional: "uint128" } },
|
|
80
|
+
{ name: "pox-addr", type: { optional: poxAddressTuple } }
|
|
81
|
+
]
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
name: "can-stack-stx",
|
|
87
|
+
access: "read-only",
|
|
88
|
+
args: [
|
|
89
|
+
{ name: "pox-addr", type: poxAddressTuple },
|
|
90
|
+
{ name: "amount-ustx", type: "uint128" },
|
|
91
|
+
{ name: "first-reward-cycle", type: "uint128" },
|
|
92
|
+
{ name: "num-cycles", type: "uint128" }
|
|
93
|
+
],
|
|
94
|
+
outputs: { response: { ok: "bool", error: "int128" } }
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
name: "stack-stx",
|
|
98
|
+
access: "public",
|
|
99
|
+
args: [
|
|
100
|
+
{ name: "amount-ustx", type: "uint128" },
|
|
101
|
+
{ name: "pox-addr", type: poxAddressTuple },
|
|
102
|
+
{ name: "start-burn-ht", type: "uint128" },
|
|
103
|
+
{ name: "lock-period", type: "uint128" },
|
|
104
|
+
{
|
|
105
|
+
name: "signer-sig",
|
|
106
|
+
type: { optional: { buff: { length: 65 } } }
|
|
107
|
+
},
|
|
108
|
+
{ name: "signer-key", type: { buff: { length: 33 } } },
|
|
109
|
+
{ name: "max-amount", type: "uint128" },
|
|
110
|
+
{ name: "auth-id", type: "uint128" }
|
|
111
|
+
],
|
|
112
|
+
outputs: {
|
|
113
|
+
response: {
|
|
114
|
+
ok: {
|
|
115
|
+
tuple: [
|
|
116
|
+
{ name: "stacker", type: "principal" },
|
|
117
|
+
{ name: "lock-amount", type: "uint128" },
|
|
118
|
+
{ name: "signer-key", type: { buff: { length: 33 } } },
|
|
119
|
+
{ name: "unlock-burn-height", type: "uint128" }
|
|
120
|
+
]
|
|
121
|
+
},
|
|
122
|
+
error: "int128"
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
name: "delegate-stx",
|
|
128
|
+
access: "public",
|
|
129
|
+
args: [
|
|
130
|
+
{ name: "amount-ustx", type: "uint128" },
|
|
131
|
+
{ name: "delegate-to", type: "principal" },
|
|
132
|
+
{ name: "until-burn-ht", type: { optional: "uint128" } },
|
|
133
|
+
{ name: "pox-addr", type: { optional: poxAddressTuple } }
|
|
134
|
+
],
|
|
135
|
+
outputs: { response: { ok: "bool", error: "int128" } }
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
name: "revoke-delegate-stx",
|
|
139
|
+
access: "public",
|
|
140
|
+
args: [],
|
|
141
|
+
outputs: {
|
|
142
|
+
response: {
|
|
143
|
+
ok: {
|
|
144
|
+
optional: {
|
|
145
|
+
tuple: [
|
|
146
|
+
{ name: "amount-ustx", type: "uint128" },
|
|
147
|
+
{ name: "delegated-to", type: "principal" },
|
|
148
|
+
{ name: "until-burn-ht", type: { optional: "uint128" } },
|
|
149
|
+
{ name: "pox-addr", type: { optional: poxAddressTuple } }
|
|
150
|
+
]
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
error: "int128"
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
name: "stack-extend",
|
|
159
|
+
access: "public",
|
|
160
|
+
args: [
|
|
161
|
+
{ name: "extend-count", type: "uint128" },
|
|
162
|
+
{ name: "pox-addr", type: poxAddressTuple },
|
|
163
|
+
{
|
|
164
|
+
name: "signer-sig",
|
|
165
|
+
type: { optional: { buff: { length: 65 } } }
|
|
166
|
+
},
|
|
167
|
+
{ name: "signer-key", type: { buff: { length: 33 } } },
|
|
168
|
+
{ name: "max-amount", type: "uint128" },
|
|
169
|
+
{ name: "auth-id", type: "uint128" }
|
|
170
|
+
],
|
|
171
|
+
outputs: {
|
|
172
|
+
response: {
|
|
173
|
+
ok: {
|
|
174
|
+
tuple: [
|
|
175
|
+
{ name: "stacker", type: "principal" },
|
|
176
|
+
{ name: "unlock-burn-height", type: "uint128" }
|
|
177
|
+
]
|
|
178
|
+
},
|
|
179
|
+
error: "int128"
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
{
|
|
184
|
+
name: "stack-increase",
|
|
185
|
+
access: "public",
|
|
186
|
+
args: [
|
|
187
|
+
{ name: "increase-by", type: "uint128" },
|
|
188
|
+
{
|
|
189
|
+
name: "signer-sig",
|
|
190
|
+
type: { optional: { buff: { length: 65 } } }
|
|
191
|
+
},
|
|
192
|
+
{ name: "signer-key", type: { buff: { length: 33 } } },
|
|
193
|
+
{ name: "max-amount", type: "uint128" },
|
|
194
|
+
{ name: "auth-id", type: "uint128" }
|
|
195
|
+
],
|
|
196
|
+
outputs: {
|
|
197
|
+
response: {
|
|
198
|
+
ok: {
|
|
199
|
+
tuple: [
|
|
200
|
+
{ name: "stacker", type: "principal" },
|
|
201
|
+
{ name: "total-locked", type: "uint128" }
|
|
202
|
+
]
|
|
203
|
+
},
|
|
204
|
+
error: "int128"
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
]
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
// src/pox/constants.ts
|
|
212
|
+
var POX_CONTRACTS = {
|
|
213
|
+
mainnet: {
|
|
214
|
+
address: "SP000000000000000000002Q6VF78",
|
|
215
|
+
name: "pox-4"
|
|
216
|
+
},
|
|
217
|
+
testnet: {
|
|
218
|
+
address: "ST000000000000000000002AMW42H",
|
|
219
|
+
name: "pox-4"
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
var MIN_LOCK_PERIOD = 1;
|
|
223
|
+
var MAX_LOCK_PERIOD = 12;
|
|
224
|
+
var POX_ADDRESS_VERSION = {
|
|
225
|
+
p2pkh: 0,
|
|
226
|
+
p2sh: 1,
|
|
227
|
+
p2sh_p2wpkh: 2,
|
|
228
|
+
p2sh_p2wsh: 3,
|
|
229
|
+
p2wpkh: 4,
|
|
230
|
+
p2wsh: 5,
|
|
231
|
+
p2tr: 6
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
// ../../node_modules/@scure/base/index.js
|
|
235
|
+
/*! scure-base - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
236
|
+
function isBytes(a) {
|
|
237
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
238
|
+
}
|
|
239
|
+
function abytes(b) {
|
|
240
|
+
if (!isBytes(b))
|
|
241
|
+
throw new Error("Uint8Array expected");
|
|
242
|
+
}
|
|
243
|
+
function isArrayOf(isString, arr) {
|
|
244
|
+
if (!Array.isArray(arr))
|
|
245
|
+
return false;
|
|
246
|
+
if (arr.length === 0)
|
|
247
|
+
return true;
|
|
248
|
+
if (isString) {
|
|
249
|
+
return arr.every((item) => typeof item === "string");
|
|
250
|
+
} else {
|
|
251
|
+
return arr.every((item) => Number.isSafeInteger(item));
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
function afn(input) {
|
|
255
|
+
if (typeof input !== "function")
|
|
256
|
+
throw new Error("function expected");
|
|
257
|
+
return true;
|
|
258
|
+
}
|
|
259
|
+
function astr(label, input) {
|
|
260
|
+
if (typeof input !== "string")
|
|
261
|
+
throw new Error(`${label}: string expected`);
|
|
262
|
+
return true;
|
|
263
|
+
}
|
|
264
|
+
function anumber(n) {
|
|
265
|
+
if (!Number.isSafeInteger(n))
|
|
266
|
+
throw new Error(`invalid integer: ${n}`);
|
|
267
|
+
}
|
|
268
|
+
function aArr(input) {
|
|
269
|
+
if (!Array.isArray(input))
|
|
270
|
+
throw new Error("array expected");
|
|
271
|
+
}
|
|
272
|
+
function astrArr(label, input) {
|
|
273
|
+
if (!isArrayOf(true, input))
|
|
274
|
+
throw new Error(`${label}: array of strings expected`);
|
|
275
|
+
}
|
|
276
|
+
function anumArr(label, input) {
|
|
277
|
+
if (!isArrayOf(false, input))
|
|
278
|
+
throw new Error(`${label}: array of numbers expected`);
|
|
279
|
+
}
|
|
280
|
+
function chain(...args) {
|
|
281
|
+
const id = (a) => a;
|
|
282
|
+
const wrap = (a, b) => (c) => a(b(c));
|
|
283
|
+
const encode = args.map((x) => x.encode).reduceRight(wrap, id);
|
|
284
|
+
const decode = args.map((x) => x.decode).reduce(wrap, id);
|
|
285
|
+
return { encode, decode };
|
|
286
|
+
}
|
|
287
|
+
function alphabet(letters) {
|
|
288
|
+
const lettersA = typeof letters === "string" ? letters.split("") : letters;
|
|
289
|
+
const len = lettersA.length;
|
|
290
|
+
astrArr("alphabet", lettersA);
|
|
291
|
+
const indexes = new Map(lettersA.map((l, i) => [l, i]));
|
|
292
|
+
return {
|
|
293
|
+
encode: (digits) => {
|
|
294
|
+
aArr(digits);
|
|
295
|
+
return digits.map((i) => {
|
|
296
|
+
if (!Number.isSafeInteger(i) || i < 0 || i >= len)
|
|
297
|
+
throw new Error(`alphabet.encode: digit index outside alphabet "${i}". Allowed: ${letters}`);
|
|
298
|
+
return lettersA[i];
|
|
299
|
+
});
|
|
300
|
+
},
|
|
301
|
+
decode: (input) => {
|
|
302
|
+
aArr(input);
|
|
303
|
+
return input.map((letter) => {
|
|
304
|
+
astr("alphabet.decode", letter);
|
|
305
|
+
const i = indexes.get(letter);
|
|
306
|
+
if (i === undefined)
|
|
307
|
+
throw new Error(`Unknown letter: "${letter}". Allowed: ${letters}`);
|
|
308
|
+
return i;
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
function join(separator = "") {
|
|
314
|
+
astr("join", separator);
|
|
315
|
+
return {
|
|
316
|
+
encode: (from) => {
|
|
317
|
+
astrArr("join.decode", from);
|
|
318
|
+
return from.join(separator);
|
|
319
|
+
},
|
|
320
|
+
decode: (to) => {
|
|
321
|
+
astr("join.decode", to);
|
|
322
|
+
return to.split(separator);
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
function padding(bits, chr = "=") {
|
|
327
|
+
anumber(bits);
|
|
328
|
+
astr("padding", chr);
|
|
329
|
+
return {
|
|
330
|
+
encode(data) {
|
|
331
|
+
astrArr("padding.encode", data);
|
|
332
|
+
while (data.length * bits % 8)
|
|
333
|
+
data.push(chr);
|
|
334
|
+
return data;
|
|
335
|
+
},
|
|
336
|
+
decode(input) {
|
|
337
|
+
astrArr("padding.decode", input);
|
|
338
|
+
let end = input.length;
|
|
339
|
+
if (end * bits % 8)
|
|
340
|
+
throw new Error("padding: invalid, string should have whole number of bytes");
|
|
341
|
+
for (;end > 0 && input[end - 1] === chr; end--) {
|
|
342
|
+
const last = end - 1;
|
|
343
|
+
const byte = last * bits;
|
|
344
|
+
if (byte % 8 === 0)
|
|
345
|
+
throw new Error("padding: invalid, string has too much padding");
|
|
346
|
+
}
|
|
347
|
+
return input.slice(0, end);
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
function normalize(fn) {
|
|
352
|
+
afn(fn);
|
|
353
|
+
return { encode: (from) => from, decode: (to) => fn(to) };
|
|
354
|
+
}
|
|
355
|
+
function convertRadix(data, from, to) {
|
|
356
|
+
if (from < 2)
|
|
357
|
+
throw new Error(`convertRadix: invalid from=${from}, base cannot be less than 2`);
|
|
358
|
+
if (to < 2)
|
|
359
|
+
throw new Error(`convertRadix: invalid to=${to}, base cannot be less than 2`);
|
|
360
|
+
aArr(data);
|
|
361
|
+
if (!data.length)
|
|
362
|
+
return [];
|
|
363
|
+
let pos = 0;
|
|
364
|
+
const res = [];
|
|
365
|
+
const digits = Array.from(data, (d) => {
|
|
366
|
+
anumber(d);
|
|
367
|
+
if (d < 0 || d >= from)
|
|
368
|
+
throw new Error(`invalid integer: ${d}`);
|
|
369
|
+
return d;
|
|
370
|
+
});
|
|
371
|
+
const dlen = digits.length;
|
|
372
|
+
while (true) {
|
|
373
|
+
let carry = 0;
|
|
374
|
+
let done = true;
|
|
375
|
+
for (let i = pos;i < dlen; i++) {
|
|
376
|
+
const digit = digits[i];
|
|
377
|
+
const fromCarry = from * carry;
|
|
378
|
+
const digitBase = fromCarry + digit;
|
|
379
|
+
if (!Number.isSafeInteger(digitBase) || fromCarry / from !== carry || digitBase - digit !== fromCarry) {
|
|
380
|
+
throw new Error("convertRadix: carry overflow");
|
|
381
|
+
}
|
|
382
|
+
const div = digitBase / to;
|
|
383
|
+
carry = digitBase % to;
|
|
384
|
+
const rounded = Math.floor(div);
|
|
385
|
+
digits[i] = rounded;
|
|
386
|
+
if (!Number.isSafeInteger(rounded) || rounded * to + carry !== digitBase)
|
|
387
|
+
throw new Error("convertRadix: carry overflow");
|
|
388
|
+
if (!done)
|
|
389
|
+
continue;
|
|
390
|
+
else if (!rounded)
|
|
391
|
+
pos = i;
|
|
392
|
+
else
|
|
393
|
+
done = false;
|
|
394
|
+
}
|
|
395
|
+
res.push(carry);
|
|
396
|
+
if (done)
|
|
397
|
+
break;
|
|
398
|
+
}
|
|
399
|
+
for (let i = 0;i < data.length - 1 && data[i] === 0; i++)
|
|
400
|
+
res.push(0);
|
|
401
|
+
return res.reverse();
|
|
402
|
+
}
|
|
403
|
+
var gcd = (a, b) => b === 0 ? a : gcd(b, a % b);
|
|
404
|
+
var radix2carry = (from, to) => from + (to - gcd(from, to));
|
|
405
|
+
var powers = /* @__PURE__ */ (() => {
|
|
406
|
+
let res = [];
|
|
407
|
+
for (let i = 0;i < 40; i++)
|
|
408
|
+
res.push(2 ** i);
|
|
409
|
+
return res;
|
|
410
|
+
})();
|
|
411
|
+
function convertRadix2(data, from, to, padding2) {
|
|
412
|
+
aArr(data);
|
|
413
|
+
if (from <= 0 || from > 32)
|
|
414
|
+
throw new Error(`convertRadix2: wrong from=${from}`);
|
|
415
|
+
if (to <= 0 || to > 32)
|
|
416
|
+
throw new Error(`convertRadix2: wrong to=${to}`);
|
|
417
|
+
if (radix2carry(from, to) > 32) {
|
|
418
|
+
throw new Error(`convertRadix2: carry overflow from=${from} to=${to} carryBits=${radix2carry(from, to)}`);
|
|
419
|
+
}
|
|
420
|
+
let carry = 0;
|
|
421
|
+
let pos = 0;
|
|
422
|
+
const max = powers[from];
|
|
423
|
+
const mask = powers[to] - 1;
|
|
424
|
+
const res = [];
|
|
425
|
+
for (const n of data) {
|
|
426
|
+
anumber(n);
|
|
427
|
+
if (n >= max)
|
|
428
|
+
throw new Error(`convertRadix2: invalid data word=${n} from=${from}`);
|
|
429
|
+
carry = carry << from | n;
|
|
430
|
+
if (pos + from > 32)
|
|
431
|
+
throw new Error(`convertRadix2: carry overflow pos=${pos} from=${from}`);
|
|
432
|
+
pos += from;
|
|
433
|
+
for (;pos >= to; pos -= to)
|
|
434
|
+
res.push((carry >> pos - to & mask) >>> 0);
|
|
435
|
+
const pow = powers[pos];
|
|
436
|
+
if (pow === undefined)
|
|
437
|
+
throw new Error("invalid carry");
|
|
438
|
+
carry &= pow - 1;
|
|
439
|
+
}
|
|
440
|
+
carry = carry << to - pos & mask;
|
|
441
|
+
if (!padding2 && pos >= from)
|
|
442
|
+
throw new Error("Excess padding");
|
|
443
|
+
if (!padding2 && carry > 0)
|
|
444
|
+
throw new Error(`Non-zero padding: ${carry}`);
|
|
445
|
+
if (padding2 && pos > 0)
|
|
446
|
+
res.push(carry >>> 0);
|
|
447
|
+
return res;
|
|
448
|
+
}
|
|
449
|
+
function radix(num) {
|
|
450
|
+
anumber(num);
|
|
451
|
+
const _256 = 2 ** 8;
|
|
452
|
+
return {
|
|
453
|
+
encode: (bytes) => {
|
|
454
|
+
if (!isBytes(bytes))
|
|
455
|
+
throw new Error("radix.encode input should be Uint8Array");
|
|
456
|
+
return convertRadix(Array.from(bytes), _256, num);
|
|
457
|
+
},
|
|
458
|
+
decode: (digits) => {
|
|
459
|
+
anumArr("radix.decode", digits);
|
|
460
|
+
return Uint8Array.from(convertRadix(digits, num, _256));
|
|
461
|
+
}
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
function radix2(bits, revPadding = false) {
|
|
465
|
+
anumber(bits);
|
|
466
|
+
if (bits <= 0 || bits > 32)
|
|
467
|
+
throw new Error("radix2: bits should be in (0..32]");
|
|
468
|
+
if (radix2carry(8, bits) > 32 || radix2carry(bits, 8) > 32)
|
|
469
|
+
throw new Error("radix2: carry overflow");
|
|
470
|
+
return {
|
|
471
|
+
encode: (bytes) => {
|
|
472
|
+
if (!isBytes(bytes))
|
|
473
|
+
throw new Error("radix2.encode input should be Uint8Array");
|
|
474
|
+
return convertRadix2(Array.from(bytes), 8, bits, !revPadding);
|
|
475
|
+
},
|
|
476
|
+
decode: (digits) => {
|
|
477
|
+
anumArr("radix2.decode", digits);
|
|
478
|
+
return Uint8Array.from(convertRadix2(digits, bits, 8, revPadding));
|
|
479
|
+
}
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
function unsafeWrapper(fn) {
|
|
483
|
+
afn(fn);
|
|
484
|
+
return function(...args) {
|
|
485
|
+
try {
|
|
486
|
+
return fn.apply(null, args);
|
|
487
|
+
} catch (e) {}
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
var base16 = chain(radix2(4), alphabet("0123456789ABCDEF"), join(""));
|
|
491
|
+
var base32 = chain(radix2(5), alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"), padding(5), join(""));
|
|
492
|
+
var base32nopad = chain(radix2(5), alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"), join(""));
|
|
493
|
+
var base32hex = chain(radix2(5), alphabet("0123456789ABCDEFGHIJKLMNOPQRSTUV"), padding(5), join(""));
|
|
494
|
+
var base32hexnopad = chain(radix2(5), alphabet("0123456789ABCDEFGHIJKLMNOPQRSTUV"), join(""));
|
|
495
|
+
var base32crockford = chain(radix2(5), alphabet("0123456789ABCDEFGHJKMNPQRSTVWXYZ"), join(""), normalize((s) => s.toUpperCase().replace(/O/g, "0").replace(/[IL]/g, "1")));
|
|
496
|
+
var hasBase64Builtin = /* @__PURE__ */ (() => typeof Uint8Array.from([]).toBase64 === "function" && typeof Uint8Array.fromBase64 === "function")();
|
|
497
|
+
var decodeBase64Builtin = (s, isUrl) => {
|
|
498
|
+
astr("base64", s);
|
|
499
|
+
const re = isUrl ? /^[A-Za-z0-9=_-]+$/ : /^[A-Za-z0-9=+/]+$/;
|
|
500
|
+
const alphabet2 = isUrl ? "base64url" : "base64";
|
|
501
|
+
if (s.length > 0 && !re.test(s))
|
|
502
|
+
throw new Error("invalid base64");
|
|
503
|
+
return Uint8Array.fromBase64(s, { alphabet: alphabet2, lastChunkHandling: "strict" });
|
|
504
|
+
};
|
|
505
|
+
var base64 = hasBase64Builtin ? {
|
|
506
|
+
encode(b) {
|
|
507
|
+
abytes(b);
|
|
508
|
+
return b.toBase64();
|
|
509
|
+
},
|
|
510
|
+
decode(s) {
|
|
511
|
+
return decodeBase64Builtin(s, false);
|
|
512
|
+
}
|
|
513
|
+
} : chain(radix2(6), alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"), padding(6), join(""));
|
|
514
|
+
var base64nopad = chain(radix2(6), alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"), join(""));
|
|
515
|
+
var base64url = hasBase64Builtin ? {
|
|
516
|
+
encode(b) {
|
|
517
|
+
abytes(b);
|
|
518
|
+
return b.toBase64({ alphabet: "base64url" });
|
|
519
|
+
},
|
|
520
|
+
decode(s) {
|
|
521
|
+
return decodeBase64Builtin(s, true);
|
|
522
|
+
}
|
|
523
|
+
} : chain(radix2(6), alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"), padding(6), join(""));
|
|
524
|
+
var base64urlnopad = chain(radix2(6), alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"), join(""));
|
|
525
|
+
var genBase58 = (abc) => chain(radix(58), alphabet(abc), join(""));
|
|
526
|
+
var base58 = genBase58("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz");
|
|
527
|
+
var base58flickr = genBase58("123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ");
|
|
528
|
+
var base58xrp = genBase58("rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz");
|
|
529
|
+
var BECH_ALPHABET = chain(alphabet("qpzry9x8gf2tvdw0s3jn54khce6mua7l"), join(""));
|
|
530
|
+
var POLYMOD_GENERATORS = [996825010, 642813549, 513874426, 1027748829, 705979059];
|
|
531
|
+
function bech32Polymod(pre) {
|
|
532
|
+
const b = pre >> 25;
|
|
533
|
+
let chk = (pre & 33554431) << 5;
|
|
534
|
+
for (let i = 0;i < POLYMOD_GENERATORS.length; i++) {
|
|
535
|
+
if ((b >> i & 1) === 1)
|
|
536
|
+
chk ^= POLYMOD_GENERATORS[i];
|
|
537
|
+
}
|
|
538
|
+
return chk;
|
|
539
|
+
}
|
|
540
|
+
function bechChecksum(prefix, words, encodingConst = 1) {
|
|
541
|
+
const len = prefix.length;
|
|
542
|
+
let chk = 1;
|
|
543
|
+
for (let i = 0;i < len; i++) {
|
|
544
|
+
const c = prefix.charCodeAt(i);
|
|
545
|
+
if (c < 33 || c > 126)
|
|
546
|
+
throw new Error(`Invalid prefix (${prefix})`);
|
|
547
|
+
chk = bech32Polymod(chk) ^ c >> 5;
|
|
548
|
+
}
|
|
549
|
+
chk = bech32Polymod(chk);
|
|
550
|
+
for (let i = 0;i < len; i++)
|
|
551
|
+
chk = bech32Polymod(chk) ^ prefix.charCodeAt(i) & 31;
|
|
552
|
+
for (let v of words)
|
|
553
|
+
chk = bech32Polymod(chk) ^ v;
|
|
554
|
+
for (let i = 0;i < 6; i++)
|
|
555
|
+
chk = bech32Polymod(chk);
|
|
556
|
+
chk ^= encodingConst;
|
|
557
|
+
return BECH_ALPHABET.encode(convertRadix2([chk % powers[30]], 30, 5, false));
|
|
558
|
+
}
|
|
559
|
+
function genBech32(encoding) {
|
|
560
|
+
const ENCODING_CONST = encoding === "bech32" ? 1 : 734539939;
|
|
561
|
+
const _words = radix2(5);
|
|
562
|
+
const fromWords = _words.decode;
|
|
563
|
+
const toWords = _words.encode;
|
|
564
|
+
const fromWordsUnsafe = unsafeWrapper(fromWords);
|
|
565
|
+
function encode(prefix, words, limit = 90) {
|
|
566
|
+
astr("bech32.encode prefix", prefix);
|
|
567
|
+
if (isBytes(words))
|
|
568
|
+
words = Array.from(words);
|
|
569
|
+
anumArr("bech32.encode", words);
|
|
570
|
+
const plen = prefix.length;
|
|
571
|
+
if (plen === 0)
|
|
572
|
+
throw new TypeError(`Invalid prefix length ${plen}`);
|
|
573
|
+
const actualLength = plen + 7 + words.length;
|
|
574
|
+
if (limit !== false && actualLength > limit)
|
|
575
|
+
throw new TypeError(`Length ${actualLength} exceeds limit ${limit}`);
|
|
576
|
+
const lowered = prefix.toLowerCase();
|
|
577
|
+
const sum = bechChecksum(lowered, words, ENCODING_CONST);
|
|
578
|
+
return `${lowered}1${BECH_ALPHABET.encode(words)}${sum}`;
|
|
579
|
+
}
|
|
580
|
+
function decode(str, limit = 90) {
|
|
581
|
+
astr("bech32.decode input", str);
|
|
582
|
+
const slen = str.length;
|
|
583
|
+
if (slen < 8 || limit !== false && slen > limit)
|
|
584
|
+
throw new TypeError(`invalid string length: ${slen} (${str}). Expected (8..${limit})`);
|
|
585
|
+
const lowered = str.toLowerCase();
|
|
586
|
+
if (str !== lowered && str !== str.toUpperCase())
|
|
587
|
+
throw new Error(`String must be lowercase or uppercase`);
|
|
588
|
+
const sepIndex = lowered.lastIndexOf("1");
|
|
589
|
+
if (sepIndex === 0 || sepIndex === -1)
|
|
590
|
+
throw new Error(`Letter "1" must be present between prefix and data only`);
|
|
591
|
+
const prefix = lowered.slice(0, sepIndex);
|
|
592
|
+
const data = lowered.slice(sepIndex + 1);
|
|
593
|
+
if (data.length < 6)
|
|
594
|
+
throw new Error("Data must be at least 6 characters long");
|
|
595
|
+
const words = BECH_ALPHABET.decode(data).slice(0, -6);
|
|
596
|
+
const sum = bechChecksum(prefix, words, ENCODING_CONST);
|
|
597
|
+
if (!data.endsWith(sum))
|
|
598
|
+
throw new Error(`Invalid checksum in ${str}: expected "${sum}"`);
|
|
599
|
+
return { prefix, words };
|
|
600
|
+
}
|
|
601
|
+
const decodeUnsafe = unsafeWrapper(decode);
|
|
602
|
+
function decodeToBytes(str) {
|
|
603
|
+
const { prefix, words } = decode(str, false);
|
|
604
|
+
return { prefix, words, bytes: fromWords(words) };
|
|
605
|
+
}
|
|
606
|
+
function encodeFromBytes(prefix, bytes) {
|
|
607
|
+
return encode(prefix, toWords(bytes));
|
|
608
|
+
}
|
|
609
|
+
return {
|
|
610
|
+
encode,
|
|
611
|
+
decode,
|
|
612
|
+
encodeFromBytes,
|
|
613
|
+
decodeToBytes,
|
|
614
|
+
decodeUnsafe,
|
|
615
|
+
fromWords,
|
|
616
|
+
fromWordsUnsafe,
|
|
617
|
+
toWords
|
|
618
|
+
};
|
|
619
|
+
}
|
|
620
|
+
var bech32 = genBech32("bech32");
|
|
621
|
+
var bech32m = genBech32("bech32m");
|
|
622
|
+
var hasHexBuiltin = /* @__PURE__ */ (() => typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function")();
|
|
623
|
+
var hexBuiltin = {
|
|
624
|
+
encode(data) {
|
|
625
|
+
abytes(data);
|
|
626
|
+
return data.toHex();
|
|
627
|
+
},
|
|
628
|
+
decode(s) {
|
|
629
|
+
astr("hex", s);
|
|
630
|
+
return Uint8Array.fromHex(s);
|
|
631
|
+
}
|
|
632
|
+
};
|
|
633
|
+
var hex = hasHexBuiltin ? hexBuiltin : chain(radix2(4), alphabet("0123456789abcdef"), join(""), normalize((s) => {
|
|
634
|
+
if (typeof s !== "string" || s.length % 2 !== 0)
|
|
635
|
+
throw new TypeError(`hex.decode: expected string, got ${typeof s} with length ${s.length}`);
|
|
636
|
+
return s.toLowerCase();
|
|
637
|
+
}));
|
|
638
|
+
|
|
639
|
+
// src/pox/utils.ts
|
|
640
|
+
var BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
641
|
+
function base58Decode(str) {
|
|
642
|
+
let num = 0n;
|
|
643
|
+
for (const char of str) {
|
|
644
|
+
const idx = BASE58_ALPHABET.indexOf(char);
|
|
645
|
+
if (idx === -1)
|
|
646
|
+
throw new Error(`Invalid base58 character: ${char}`);
|
|
647
|
+
num = num * 58n + BigInt(idx);
|
|
648
|
+
}
|
|
649
|
+
const hex2 = num.toString(16).padStart(2, "0");
|
|
650
|
+
const padded = hex2.length % 2 ? "0" + hex2 : hex2;
|
|
651
|
+
const rawBytes = hexToBytes(padded);
|
|
652
|
+
let leadingZeros = 0;
|
|
653
|
+
for (const char of str) {
|
|
654
|
+
if (char === "1")
|
|
655
|
+
leadingZeros++;
|
|
656
|
+
else
|
|
657
|
+
break;
|
|
658
|
+
}
|
|
659
|
+
const result = new Uint8Array(leadingZeros + rawBytes.length);
|
|
660
|
+
result.set(rawBytes, leadingZeros);
|
|
661
|
+
return result;
|
|
662
|
+
}
|
|
663
|
+
function parseBtcAddress(address) {
|
|
664
|
+
if (address.startsWith("bc1") || address.startsWith("tb1") || address.startsWith("bcrt1")) {
|
|
665
|
+
return parseSegwitAddress(address);
|
|
666
|
+
}
|
|
667
|
+
return parseLegacyAddress(address);
|
|
668
|
+
}
|
|
669
|
+
function parseSegwitAddress(address) {
|
|
670
|
+
let decoded;
|
|
671
|
+
try {
|
|
672
|
+
decoded = bech32.decode(address);
|
|
673
|
+
} catch {
|
|
674
|
+
decoded = bech32m.decode(address);
|
|
675
|
+
}
|
|
676
|
+
const witnessVersion = decoded.words[0];
|
|
677
|
+
const data = bech32.fromWords(decoded.words.slice(1));
|
|
678
|
+
const hashbytes = new Uint8Array(data);
|
|
679
|
+
let version;
|
|
680
|
+
if (witnessVersion === 0) {
|
|
681
|
+
if (hashbytes.length === 20) {
|
|
682
|
+
version = POX_ADDRESS_VERSION.p2wpkh;
|
|
683
|
+
} else if (hashbytes.length === 32) {
|
|
684
|
+
version = POX_ADDRESS_VERSION.p2wsh;
|
|
685
|
+
} else {
|
|
686
|
+
throw new Error(`Invalid witness v0 program length: ${hashbytes.length}`);
|
|
687
|
+
}
|
|
688
|
+
} else if (witnessVersion === 1) {
|
|
689
|
+
if (hashbytes.length !== 32) {
|
|
690
|
+
throw new Error(`Invalid witness v1 program length: ${hashbytes.length}`);
|
|
691
|
+
}
|
|
692
|
+
version = POX_ADDRESS_VERSION.p2tr;
|
|
693
|
+
} else {
|
|
694
|
+
throw new Error(`Unsupported witness version: ${witnessVersion}`);
|
|
695
|
+
}
|
|
696
|
+
const padded = new Uint8Array(32);
|
|
697
|
+
padded.set(hashbytes);
|
|
698
|
+
return {
|
|
699
|
+
version: new Uint8Array([version]),
|
|
700
|
+
hashbytes: padded
|
|
701
|
+
};
|
|
702
|
+
}
|
|
703
|
+
function parseLegacyAddress(address) {
|
|
704
|
+
const decoded = base58Decode(address);
|
|
705
|
+
if (decoded.length !== 25) {
|
|
706
|
+
throw new Error(`Invalid legacy address length: ${decoded.length}`);
|
|
707
|
+
}
|
|
708
|
+
const versionByte = decoded[0];
|
|
709
|
+
const hashbytes = decoded.slice(1, 21);
|
|
710
|
+
let version;
|
|
711
|
+
if (versionByte === 0 || versionByte === 111) {
|
|
712
|
+
version = POX_ADDRESS_VERSION.p2pkh;
|
|
713
|
+
} else if (versionByte === 5 || versionByte === 196) {
|
|
714
|
+
version = POX_ADDRESS_VERSION.p2sh;
|
|
715
|
+
} else {
|
|
716
|
+
throw new Error(`Unknown legacy address version byte: 0x${versionByte.toString(16)}`);
|
|
717
|
+
}
|
|
718
|
+
const padded = new Uint8Array(32);
|
|
719
|
+
padded.set(hashbytes);
|
|
720
|
+
return {
|
|
721
|
+
version: new Uint8Array([version]),
|
|
722
|
+
hashbytes: padded
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
function validateLockPeriod(periods) {
|
|
726
|
+
return Number.isInteger(periods) && periods >= MIN_LOCK_PERIOD && periods <= MAX_LOCK_PERIOD;
|
|
727
|
+
}
|
|
728
|
+
function burnHeightToRewardCycle(burnHeight, firstBurnchainBlockHeight, rewardCycleLength) {
|
|
729
|
+
return (burnHeight - firstBurnchainBlockHeight) / rewardCycleLength;
|
|
730
|
+
}
|
|
731
|
+
function rewardCycleToBurnHeight(cycle, firstBurnchainBlockHeight, rewardCycleLength) {
|
|
732
|
+
return firstBurnchainBlockHeight + cycle * rewardCycleLength;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
// src/pox/actions.ts
|
|
736
|
+
function getPoxContract(client) {
|
|
737
|
+
if (!client.chain) {
|
|
738
|
+
throw new Error("Client must have a chain configured");
|
|
739
|
+
}
|
|
740
|
+
const network = client.chain.network;
|
|
741
|
+
const contract = network === "mainnet" ? POX_CONTRACTS.mainnet : POX_CONTRACTS.testnet;
|
|
742
|
+
return getContract({
|
|
743
|
+
client,
|
|
744
|
+
address: contract.address,
|
|
745
|
+
name: contract.name,
|
|
746
|
+
abi: POX_ABI
|
|
747
|
+
});
|
|
748
|
+
}
|
|
749
|
+
async function getPoxInfo(client) {
|
|
750
|
+
const pox = getPoxContract(client);
|
|
751
|
+
return await pox.read["get-pox-info"]({});
|
|
752
|
+
}
|
|
753
|
+
async function getStackerInfo(client, address) {
|
|
754
|
+
const pox = getPoxContract(client);
|
|
755
|
+
return await pox.read["get-stacker-info"]({
|
|
756
|
+
stacker: address
|
|
757
|
+
});
|
|
758
|
+
}
|
|
759
|
+
async function getDelegationInfo(client, address) {
|
|
760
|
+
const pox = getPoxContract(client);
|
|
761
|
+
return await pox.read["get-delegation-info"]({
|
|
762
|
+
stacker: address
|
|
763
|
+
});
|
|
764
|
+
}
|
|
765
|
+
async function canStack(client, amount) {
|
|
766
|
+
const info = await getPoxInfo(client);
|
|
767
|
+
return amount >= info.minAmountUstx;
|
|
768
|
+
}
|
|
769
|
+
async function stackStx(client, params) {
|
|
770
|
+
if (!client.account)
|
|
771
|
+
throw new Error("Wallet client required");
|
|
772
|
+
if (!validateLockPeriod(params.lockPeriod)) {
|
|
773
|
+
throw new Error("Lock period must be 1-12 cycles");
|
|
774
|
+
}
|
|
775
|
+
const info = await getPoxInfo(client);
|
|
776
|
+
if (params.amount < info.minAmountUstx) {
|
|
777
|
+
throw new Error(`Amount below minimum: ${info.minAmountUstx}`);
|
|
778
|
+
}
|
|
779
|
+
const poxAddr = parseBtcAddress(params.btcAddress);
|
|
780
|
+
const pox = getPoxContract(client);
|
|
781
|
+
return pox.call["stack-stx"]({
|
|
782
|
+
amountUstx: params.amount,
|
|
783
|
+
poxAddr,
|
|
784
|
+
startBurnHt: params.startBurnHeight,
|
|
785
|
+
lockPeriod: BigInt(params.lockPeriod),
|
|
786
|
+
signerSig: params.signerSig,
|
|
787
|
+
signerKey: params.signerKey,
|
|
788
|
+
maxAmount: params.maxAmount,
|
|
789
|
+
authId: params.authId
|
|
790
|
+
}, {
|
|
791
|
+
postConditions: [
|
|
792
|
+
Pc.principal(client.account.address).willSendEq(params.amount).ustx()
|
|
793
|
+
]
|
|
794
|
+
});
|
|
795
|
+
}
|
|
796
|
+
async function delegateStx(client, params) {
|
|
797
|
+
if (!client.account)
|
|
798
|
+
throw new Error("Wallet client required");
|
|
799
|
+
const poxAddr = params.poxAddr ? parseBtcAddress(params.poxAddr) : null;
|
|
800
|
+
const pox = getPoxContract(client);
|
|
801
|
+
return pox.call["delegate-stx"]({
|
|
802
|
+
amountUstx: params.amount,
|
|
803
|
+
delegateTo: params.delegateTo,
|
|
804
|
+
untilBurnHt: params.untilBurnHeight ?? null,
|
|
805
|
+
poxAddr
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
async function revokeDelegateStx(client) {
|
|
809
|
+
if (!client.account)
|
|
810
|
+
throw new Error("Wallet client required");
|
|
811
|
+
const pox = getPoxContract(client);
|
|
812
|
+
return pox.call["revoke-delegate-stx"]({});
|
|
813
|
+
}
|
|
814
|
+
async function stackExtend(client, params) {
|
|
815
|
+
if (!client.account)
|
|
816
|
+
throw new Error("Wallet client required");
|
|
817
|
+
if (!validateLockPeriod(params.extendCount)) {
|
|
818
|
+
throw new Error("Extend count must be 1-12 cycles");
|
|
819
|
+
}
|
|
820
|
+
const poxAddr = parseBtcAddress(params.btcAddress);
|
|
821
|
+
const pox = getPoxContract(client);
|
|
822
|
+
return pox.call["stack-extend"]({
|
|
823
|
+
extendCount: BigInt(params.extendCount),
|
|
824
|
+
poxAddr,
|
|
825
|
+
signerSig: params.signerSig,
|
|
826
|
+
signerKey: params.signerKey,
|
|
827
|
+
maxAmount: params.maxAmount,
|
|
828
|
+
authId: params.authId
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
async function stackIncrease(client, params) {
|
|
832
|
+
if (!client.account)
|
|
833
|
+
throw new Error("Wallet client required");
|
|
834
|
+
const pox = getPoxContract(client);
|
|
835
|
+
return pox.call["stack-increase"]({
|
|
836
|
+
increaseBy: params.increaseBy,
|
|
837
|
+
signerSig: params.signerSig,
|
|
838
|
+
signerKey: params.signerKey,
|
|
839
|
+
maxAmount: params.maxAmount,
|
|
840
|
+
authId: params.authId
|
|
841
|
+
}, {
|
|
842
|
+
postConditions: [
|
|
843
|
+
Pc.principal(client.account.address).willSendEq(params.increaseBy).ustx()
|
|
844
|
+
]
|
|
845
|
+
});
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
// src/pox/index.ts
|
|
849
|
+
function pox() {
|
|
850
|
+
return (client) => ({
|
|
851
|
+
pox: {
|
|
852
|
+
getPoxInfo: () => getPoxInfo(client),
|
|
853
|
+
getStackerInfo: (address) => getStackerInfo(client, address),
|
|
854
|
+
getDelegationInfo: (address) => getDelegationInfo(client, address),
|
|
855
|
+
canStack: (amount) => canStack(client, amount),
|
|
856
|
+
stackStx: (params) => stackStx(client, params),
|
|
857
|
+
delegateStx: (params) => delegateStx(client, params),
|
|
858
|
+
revokeDelegateStx: () => revokeDelegateStx(client),
|
|
859
|
+
stackExtend: (params) => stackExtend(client, params),
|
|
860
|
+
stackIncrease: (params) => stackIncrease(client, params)
|
|
861
|
+
}
|
|
862
|
+
});
|
|
863
|
+
}
|
|
864
|
+
export {
|
|
865
|
+
validateLockPeriod,
|
|
866
|
+
rewardCycleToBurnHeight,
|
|
867
|
+
pox,
|
|
868
|
+
parseBtcAddress,
|
|
869
|
+
burnHeightToRewardCycle,
|
|
870
|
+
POX_CONTRACTS,
|
|
871
|
+
POX_ADDRESS_VERSION,
|
|
872
|
+
MIN_LOCK_PERIOD,
|
|
873
|
+
MAX_LOCK_PERIOD
|
|
874
|
+
};
|
|
875
|
+
|
|
876
|
+
//# debugId=4940239BB9C22EB664756E2164756E21
|
|
877
|
+
//# sourceMappingURL=index.js.map
|