ox 0.13.2 → 0.14.0
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/Bech32m/package.json +6 -0
- package/CHANGELOG.md +19 -0
- package/_cjs/core/Bech32m.js +205 -0
- package/_cjs/core/Bech32m.js.map +1 -0
- package/_cjs/index.js +3 -2
- package/_cjs/index.js.map +1 -1
- package/_cjs/tempo/KeyAuthorization.js +4 -4
- package/_cjs/tempo/KeyAuthorization.js.map +1 -1
- package/_cjs/tempo/SignatureEnvelope.js +18 -3
- package/_cjs/tempo/SignatureEnvelope.js.map +1 -1
- package/_cjs/tempo/TempoAddress.js +40 -39
- package/_cjs/tempo/TempoAddress.js.map +1 -1
- package/_cjs/tempo/TxEnvelopeTempo.js +5 -2
- package/_cjs/tempo/TxEnvelopeTempo.js.map +1 -1
- package/_cjs/tempo/index.js.map +1 -1
- package/_cjs/version.js +1 -1
- package/_esm/core/Bech32m.js +238 -0
- package/_esm/core/Bech32m.js.map +1 -0
- package/_esm/index.js +24 -0
- package/_esm/index.js.map +1 -1
- package/_esm/tempo/KeyAuthorization.js +19 -9
- package/_esm/tempo/KeyAuthorization.js.map +1 -1
- package/_esm/tempo/SignatureEnvelope.js +22 -5
- package/_esm/tempo/SignatureEnvelope.js.map +1 -1
- package/_esm/tempo/TempoAddress.js +47 -46
- package/_esm/tempo/TempoAddress.js.map +1 -1
- package/_esm/tempo/TxEnvelopeTempo.js +42 -2
- package/_esm/tempo/TxEnvelopeTempo.js.map +1 -1
- package/_esm/tempo/index.js +2 -2
- package/_esm/tempo/index.js.map +1 -1
- package/_esm/version.js +1 -1
- package/_types/core/Bech32m.d.ts +93 -0
- package/_types/core/Bech32m.d.ts.map +1 -0
- package/_types/index.d.ts +24 -0
- package/_types/index.d.ts.map +1 -1
- package/_types/tempo/KeyAuthorization.d.ts +17 -7
- package/_types/tempo/KeyAuthorization.d.ts.map +1 -1
- package/_types/tempo/SignatureEnvelope.d.ts +19 -5
- package/_types/tempo/SignatureEnvelope.d.ts.map +1 -1
- package/_types/tempo/TempoAddress.d.ts +19 -11
- package/_types/tempo/TempoAddress.d.ts.map +1 -1
- package/_types/tempo/TxEnvelopeTempo.d.ts +47 -1
- package/_types/tempo/TxEnvelopeTempo.d.ts.map +1 -1
- package/_types/tempo/index.d.ts +2 -2
- package/_types/tempo/index.d.ts.map +1 -1
- package/_types/version.d.ts +1 -1
- package/core/Bech32m.ts +263 -0
- package/index.ts +24 -2
- package/package.json +6 -1
- package/tempo/KeyAuthorization.test.ts +70 -23
- package/tempo/KeyAuthorization.ts +21 -18
- package/tempo/SignatureEnvelope.test.ts +15 -8
- package/tempo/SignatureEnvelope.ts +43 -8
- package/tempo/TempoAddress.test.ts +49 -14
- package/tempo/TempoAddress.ts +56 -59
- package/tempo/Transaction.test.ts +4 -2
- package/tempo/TxEnvelopeTempo.test.ts +7 -3
- package/tempo/TxEnvelopeTempo.ts +52 -1
- package/tempo/e2e.test.ts +45 -10
- package/tempo/index.ts +6 -2
- package/version.ts +1 -1
package/core/Bech32m.ts
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import type * as Errors from './Errors.js'
|
|
2
|
+
import { BaseError } from './Errors.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Encodes data bytes with a human-readable part (HRP) into a bech32m string (BIP-350).
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts twoslash
|
|
9
|
+
* import { Bech32m } from 'ox'
|
|
10
|
+
*
|
|
11
|
+
* const encoded = Bech32m.encode('tempo', new Uint8Array(20))
|
|
12
|
+
* // @log: 'tempo1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqwa7xtm'
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @param hrp - The human-readable part (e.g. `"tempo"`, `"tempoz"`).
|
|
16
|
+
* @param data - The data bytes to encode.
|
|
17
|
+
* @returns The bech32m-encoded string.
|
|
18
|
+
*/
|
|
19
|
+
export function encode(
|
|
20
|
+
hrp: string,
|
|
21
|
+
data: Uint8Array,
|
|
22
|
+
options: encode.Options = {},
|
|
23
|
+
): string {
|
|
24
|
+
const { limit = 90 } = options
|
|
25
|
+
|
|
26
|
+
hrp = hrp.toLowerCase()
|
|
27
|
+
|
|
28
|
+
if (hrp.length === 0) throw new InvalidHrpError()
|
|
29
|
+
for (let i = 0; i < hrp.length; i++) {
|
|
30
|
+
const c = hrp.charCodeAt(i)
|
|
31
|
+
if (c < 33 || c > 126) throw new InvalidHrpError()
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const data5 = convertBits(data, 8, 5, true)
|
|
35
|
+
|
|
36
|
+
if (hrp.length + 1 + data5.length + 6 > limit)
|
|
37
|
+
throw new ExceedsLengthError({ limit })
|
|
38
|
+
|
|
39
|
+
const checksum = createChecksum(hrp, data5)
|
|
40
|
+
let result = hrp + '1'
|
|
41
|
+
for (const d of data5.concat(checksum)) result += alphabet[d]
|
|
42
|
+
return result
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export declare namespace encode {
|
|
46
|
+
type Options = {
|
|
47
|
+
/** Maximum length of the encoded string. @default 90 */
|
|
48
|
+
limit?: number | undefined
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
type ErrorType = InvalidHrpError | ExceedsLengthError | Errors.GlobalErrorType
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Decodes a bech32m string (BIP-350) into a human-readable part and data bytes.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts twoslash
|
|
59
|
+
* import { Bech32m } from 'ox'
|
|
60
|
+
*
|
|
61
|
+
* const { hrp, data } = Bech32m.decode('tempo1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqwa7xtm')
|
|
62
|
+
* // @log: { hrp: 'tempo', data: Uint8Array(20) }
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* @param str - The bech32m-encoded string to decode.
|
|
66
|
+
* @returns The decoded HRP and data bytes.
|
|
67
|
+
*/
|
|
68
|
+
export function decode(
|
|
69
|
+
str: string,
|
|
70
|
+
options: decode.Options = {},
|
|
71
|
+
): decode.ReturnType {
|
|
72
|
+
const { limit = 90 } = options
|
|
73
|
+
|
|
74
|
+
if (str.length > limit) throw new ExceedsLengthError({ limit })
|
|
75
|
+
|
|
76
|
+
if (str !== str.toLowerCase() && str !== str.toUpperCase())
|
|
77
|
+
throw new MixedCaseError()
|
|
78
|
+
|
|
79
|
+
const lower = str.toLowerCase()
|
|
80
|
+
const pos = lower.lastIndexOf('1')
|
|
81
|
+
if (pos === -1) throw new NoSeparatorError()
|
|
82
|
+
if (pos === 0) throw new InvalidHrpError()
|
|
83
|
+
if (pos + 7 > lower.length) throw new InvalidChecksumError()
|
|
84
|
+
|
|
85
|
+
const hrp = lower.slice(0, pos)
|
|
86
|
+
for (let i = 0; i < hrp.length; i++) {
|
|
87
|
+
const c = hrp.charCodeAt(i)
|
|
88
|
+
if (c < 33 || c > 126) throw new InvalidHrpError()
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const dataChars = lower.slice(pos + 1)
|
|
92
|
+
|
|
93
|
+
const data5: number[] = []
|
|
94
|
+
for (const c of dataChars) {
|
|
95
|
+
const v = alphabetMap[c]
|
|
96
|
+
if (v === undefined) throw new InvalidCharacterError({ character: c })
|
|
97
|
+
data5.push(v)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (!verifyChecksum(hrp, data5)) throw new InvalidChecksumError()
|
|
101
|
+
|
|
102
|
+
const data8 = convertBits(data5.slice(0, -6), 5, 8, false)
|
|
103
|
+
return { hrp, data: new Uint8Array(data8) }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export declare namespace decode {
|
|
107
|
+
type Options = {
|
|
108
|
+
/** Maximum length of the encoded string. @default 90 */
|
|
109
|
+
limit?: number | undefined
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
type ReturnType = {
|
|
113
|
+
/** The human-readable part. */
|
|
114
|
+
hrp: string
|
|
115
|
+
/** The decoded data bytes. */
|
|
116
|
+
data: Uint8Array
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
type ErrorType =
|
|
120
|
+
| NoSeparatorError
|
|
121
|
+
| InvalidChecksumError
|
|
122
|
+
| InvalidCharacterError
|
|
123
|
+
| InvalidPaddingError
|
|
124
|
+
| MixedCaseError
|
|
125
|
+
| InvalidHrpError
|
|
126
|
+
| ExceedsLengthError
|
|
127
|
+
| Errors.GlobalErrorType
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Thrown when a bech32m string has no separator. */
|
|
131
|
+
export class NoSeparatorError extends BaseError {
|
|
132
|
+
override readonly name = 'Bech32m.NoSeparatorError'
|
|
133
|
+
constructor() {
|
|
134
|
+
super('Bech32m string has no separator.')
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Thrown when a bech32m string has an invalid checksum. */
|
|
139
|
+
export class InvalidChecksumError extends BaseError {
|
|
140
|
+
override readonly name = 'Bech32m.InvalidChecksumError'
|
|
141
|
+
constructor() {
|
|
142
|
+
super('Invalid bech32m checksum.')
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Thrown when a bech32m string contains an invalid character. */
|
|
147
|
+
export class InvalidCharacterError extends BaseError {
|
|
148
|
+
override readonly name = 'Bech32m.InvalidCharacterError'
|
|
149
|
+
constructor({ character }: { character: string }) {
|
|
150
|
+
super(`Invalid bech32m character: "${character}".`)
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Thrown when the padding bits are invalid during base32 conversion. */
|
|
155
|
+
export class InvalidPaddingError extends BaseError {
|
|
156
|
+
override readonly name = 'Bech32m.InvalidPaddingError'
|
|
157
|
+
constructor() {
|
|
158
|
+
super('Invalid padding in bech32m data.')
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Thrown when a bech32m string contains mixed case. */
|
|
163
|
+
export class MixedCaseError extends BaseError {
|
|
164
|
+
override readonly name = 'Bech32m.MixedCaseError'
|
|
165
|
+
constructor() {
|
|
166
|
+
super('Bech32m string must not contain mixed case.')
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Thrown when the HRP is invalid (empty or contains non-ASCII characters). */
|
|
171
|
+
export class InvalidHrpError extends BaseError {
|
|
172
|
+
override readonly name = 'Bech32m.InvalidHrpError'
|
|
173
|
+
constructor() {
|
|
174
|
+
super(
|
|
175
|
+
'Invalid bech32m human-readable part (HRP). Must be 1+ characters in ASCII range 33-126.',
|
|
176
|
+
)
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Thrown when the encoded string exceeds the length limit. */
|
|
181
|
+
export class ExceedsLengthError extends BaseError {
|
|
182
|
+
override readonly name = 'Bech32m.ExceedsLengthError'
|
|
183
|
+
constructor({ limit }: { limit: number }) {
|
|
184
|
+
super(`Bech32m string exceeds length limit of ${limit}.`)
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** @internal */
|
|
189
|
+
const alphabet = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l'
|
|
190
|
+
|
|
191
|
+
/** @internal */
|
|
192
|
+
const alphabetMap = /*#__PURE__*/ (() => {
|
|
193
|
+
const map: Record<string, number> = {}
|
|
194
|
+
for (let i = 0; i < alphabet.length; i++) map[alphabet[i]!] = i
|
|
195
|
+
return map
|
|
196
|
+
})()
|
|
197
|
+
|
|
198
|
+
/** @internal */
|
|
199
|
+
const BECH32M_CONST = 0x2bc830a3
|
|
200
|
+
|
|
201
|
+
/** @internal */
|
|
202
|
+
const GEN = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3]
|
|
203
|
+
|
|
204
|
+
/** @internal */
|
|
205
|
+
function polymod(values: number[]): number {
|
|
206
|
+
let chk = 1
|
|
207
|
+
for (const v of values) {
|
|
208
|
+
const b = chk >> 25
|
|
209
|
+
chk = ((chk & 0x1ffffff) << 5) ^ v
|
|
210
|
+
for (let i = 0; i < 5; i++) if ((b >> i) & 1) chk ^= GEN[i]!
|
|
211
|
+
}
|
|
212
|
+
return chk
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** @internal */
|
|
216
|
+
function hrpExpand(hrp: string): number[] {
|
|
217
|
+
const ret: number[] = []
|
|
218
|
+
for (let i = 0; i < hrp.length; i++) ret.push(hrp.charCodeAt(i) >> 5)
|
|
219
|
+
ret.push(0)
|
|
220
|
+
for (let i = 0; i < hrp.length; i++) ret.push(hrp.charCodeAt(i) & 31)
|
|
221
|
+
return ret
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** @internal */
|
|
225
|
+
function createChecksum(hrp: string, data: number[]): number[] {
|
|
226
|
+
const values = hrpExpand(hrp).concat(data).concat([0, 0, 0, 0, 0, 0])
|
|
227
|
+
const mod = polymod(values) ^ BECH32M_CONST
|
|
228
|
+
const ret: number[] = []
|
|
229
|
+
for (let i = 0; i < 6; i++) ret.push((mod >> (5 * (5 - i))) & 31)
|
|
230
|
+
return ret
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** @internal */
|
|
234
|
+
function verifyChecksum(hrp: string, data: number[]): boolean {
|
|
235
|
+
return polymod(hrpExpand(hrp).concat(data)) === BECH32M_CONST
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** @internal */
|
|
239
|
+
function convertBits(
|
|
240
|
+
data: Iterable<number>,
|
|
241
|
+
fromBits: number,
|
|
242
|
+
toBits: number,
|
|
243
|
+
pad: boolean,
|
|
244
|
+
): number[] {
|
|
245
|
+
let acc = 0
|
|
246
|
+
let bits = 0
|
|
247
|
+
const maxv = (1 << toBits) - 1
|
|
248
|
+
const ret: number[] = []
|
|
249
|
+
for (const value of data) {
|
|
250
|
+
acc = (acc << fromBits) | value
|
|
251
|
+
bits += fromBits
|
|
252
|
+
while (bits >= toBits) {
|
|
253
|
+
bits -= toBits
|
|
254
|
+
ret.push((acc >> bits) & maxv)
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (pad) {
|
|
258
|
+
if (bits > 0) ret.push((acc << (toBits - bits)) & maxv)
|
|
259
|
+
} else if (bits >= fromBits || (acc << (toBits - bits)) & maxv) {
|
|
260
|
+
throw new InvalidPaddingError()
|
|
261
|
+
}
|
|
262
|
+
return ret
|
|
263
|
+
}
|
package/index.ts
CHANGED
|
@@ -862,7 +862,6 @@ export * as Authorization from './core/Authorization.js'
|
|
|
862
862
|
* @category Data
|
|
863
863
|
*/
|
|
864
864
|
export * as Base32 from './core/Base32.js'
|
|
865
|
-
|
|
866
865
|
/**
|
|
867
866
|
* Utility functions for working with [Base58](https://digitalbazaar.github.io/base58-spec/) values.
|
|
868
867
|
*
|
|
@@ -917,7 +916,6 @@ export * as Base32 from './core/Base32.js'
|
|
|
917
916
|
* @category Data
|
|
918
917
|
*/
|
|
919
918
|
export * as Base58 from './core/Base58.js'
|
|
920
|
-
|
|
921
919
|
/**
|
|
922
920
|
* Utility functions for working with [RFC-4648](https://datatracker.ietf.org/doc/html/rfc4648) Base64.
|
|
923
921
|
*
|
|
@@ -971,6 +969,30 @@ export * as Base58 from './core/Base58.js'
|
|
|
971
969
|
* @category Data
|
|
972
970
|
*/
|
|
973
971
|
export * as Base64 from './core/Base64.js'
|
|
972
|
+
/**
|
|
973
|
+
* Utility functions for [BIP-350](https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki) bech32m encoding and decoding.
|
|
974
|
+
*
|
|
975
|
+
* @example
|
|
976
|
+
* ### Encoding
|
|
977
|
+
*
|
|
978
|
+
* ```ts twoslash
|
|
979
|
+
* import { Bech32m } from 'ox'
|
|
980
|
+
*
|
|
981
|
+
* const encoded = Bech32m.encode('tempo', new Uint8Array(20))
|
|
982
|
+
* ```
|
|
983
|
+
*
|
|
984
|
+
* @example
|
|
985
|
+
* ### Decoding
|
|
986
|
+
*
|
|
987
|
+
* ```ts twoslash
|
|
988
|
+
* import { Bech32m } from 'ox'
|
|
989
|
+
*
|
|
990
|
+
* const { hrp, data } = Bech32m.decode('tempo1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq7w9gdx')
|
|
991
|
+
* ```
|
|
992
|
+
*
|
|
993
|
+
* @category Data
|
|
994
|
+
*/
|
|
995
|
+
export * as Bech32m from './core/Bech32m.js'
|
|
974
996
|
|
|
975
997
|
/**
|
|
976
998
|
* Utility functions for working with [EIP-7864](https://eips.ethereum.org/EIPS/eip-7864) Binary State Trees.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ox",
|
|
3
3
|
"description": "Ethereum Standard Library",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.14.0",
|
|
5
5
|
"main": "./_cjs/index.js",
|
|
6
6
|
"module": "./_esm/index.js",
|
|
7
7
|
"types": "./_types/index.d.ts",
|
|
@@ -128,6 +128,11 @@
|
|
|
128
128
|
"import": "./_esm/core/Base64.js",
|
|
129
129
|
"default": "./_cjs/core/Base64.js"
|
|
130
130
|
},
|
|
131
|
+
"./Bech32m": {
|
|
132
|
+
"types": "./_types/core/Bech32m.d.ts",
|
|
133
|
+
"import": "./_esm/core/Bech32m.js",
|
|
134
|
+
"default": "./_cjs/core/Bech32m.js"
|
|
135
|
+
},
|
|
131
136
|
"./BinaryStateTree": {
|
|
132
137
|
"types": "./_types/core/BinaryStateTree.d.ts",
|
|
133
138
|
"import": "./_esm/core/BinaryStateTree.js",
|