@helios-lang/effect 0.1.5 → 0.1.7
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/bun.lock +60 -0
- package/dist/Uplc/Data.js +20 -0
- package/dist/Uplc/Data.js.map +1 -1
- package/package.json +1 -1
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.check.tsbuildinfo +1 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/types/Address.d.ts +5 -0
- package/types/Address.d.ts.map +1 -0
- package/types/Bech32.d.ts +30 -0
- package/types/Bech32.d.ts.map +1 -0
- package/types/Cbor.d.ts +430 -0
- package/types/Cbor.d.ts.map +1 -0
- package/types/Ledger/Address.d.ts +109 -0
- package/types/Ledger/Address.d.ts.map +1 -0
- package/types/Ledger/AssetClass.d.ts +101 -0
- package/types/Ledger/AssetClass.d.ts.map +1 -0
- package/types/Ledger/Assets.d.ts +70 -0
- package/types/Ledger/Assets.d.ts.map +1 -0
- package/types/Ledger/Credential.d.ts +26 -0
- package/types/Ledger/Credential.d.ts.map +1 -0
- package/types/Ledger/DatumHash.d.ts +40 -0
- package/types/Ledger/DatumHash.d.ts.map +1 -0
- package/types/Ledger/IsMainnet.d.ts +6 -0
- package/types/Ledger/IsMainnet.d.ts.map +1 -0
- package/types/Ledger/MintingPolicy.d.ts +39 -0
- package/types/Ledger/MintingPolicy.d.ts.map +1 -0
- package/{src/Ledger/NetworkParams.ts → types/Ledger/NetworkParams.d.ts} +24 -26
- package/types/Ledger/NetworkParams.d.ts.map +1 -0
- package/types/Ledger/PubKeyHash.d.ts +40 -0
- package/types/Ledger/PubKeyHash.d.ts.map +1 -0
- package/types/Ledger/TxId.d.ts +10 -0
- package/types/Ledger/TxId.d.ts.map +1 -0
- package/types/Ledger/TxInput.d.ts +55 -0
- package/types/Ledger/TxInput.d.ts.map +1 -0
- package/types/Ledger/TxOutput.d.ts +63 -0
- package/types/Ledger/TxOutput.d.ts.map +1 -0
- package/types/Ledger/TxOutputDatum.d.ts +41 -0
- package/types/Ledger/TxOutputDatum.d.ts.map +1 -0
- package/types/Ledger/TxOutputId.d.ts +14 -0
- package/types/Ledger/TxOutputId.d.ts.map +1 -0
- package/types/Ledger/ValidatorHash.d.ts +40 -0
- package/types/Ledger/ValidatorHash.d.ts.map +1 -0
- package/types/Ledger/index.d.ts +16 -0
- package/types/Ledger/index.d.ts.map +1 -0
- package/types/Uplc/Cek.d.ts +72 -0
- package/types/Uplc/Cek.d.ts.map +1 -0
- package/types/Uplc/Data.d.ts +559 -0
- package/types/Uplc/Data.d.ts.map +1 -0
- package/types/Uplc/DataSchema.d.ts +227 -0
- package/types/Uplc/DataSchema.d.ts.map +1 -0
- package/types/Uplc/Primitive.d.ts +26 -0
- package/types/Uplc/Primitive.d.ts.map +1 -0
- package/types/Uplc/index.d.ts +3 -0
- package/types/Uplc/index.d.ts.map +1 -0
- package/types/index.d.ts +5 -0
- package/types/index.d.ts.map +1 -0
- package/types/internal/Base32.d.ts +49 -0
- package/types/internal/Base32.d.ts.map +1 -0
- package/types/internal/BigEndian.d.ts +22 -0
- package/types/internal/BigEndian.d.ts.map +1 -0
- package/types/internal/Bits.d.ts +123 -0
- package/types/internal/Bits.d.ts.map +1 -0
- package/types/internal/Bytes.d.ts +88 -0
- package/types/internal/Bytes.d.ts.map +1 -0
- package/types/internal/Flat.d.ts +71 -0
- package/types/internal/Flat.d.ts.map +1 -0
- package/types/internal/Float.d.ts +38 -0
- package/types/internal/Float.d.ts.map +1 -0
- package/types/internal/Utf8.d.ts +24 -0
- package/types/internal/Utf8.d.ts.map +1 -0
- package/src/Bech32.test.ts +0 -117
- package/src/Bech32.ts +0 -198
- package/src/Cbor.test.ts +0 -1610
- package/src/Cbor.ts +0 -1704
- package/src/Ledger/Address.ts +0 -248
- package/src/Ledger/AssetClass.ts +0 -90
- package/src/Ledger/Assets.ts +0 -164
- package/src/Ledger/Credential.ts +0 -29
- package/src/Ledger/DatumHash.ts +0 -36
- package/src/Ledger/IsMainnet.ts +0 -6
- package/src/Ledger/MintingPolicy.ts +0 -57
- package/src/Ledger/PubKeyHash.ts +0 -36
- package/src/Ledger/TxId.ts +0 -31
- package/src/Ledger/TxInput.test.ts +0 -21
- package/src/Ledger/TxInput.ts +0 -66
- package/src/Ledger/TxOutput.ts +0 -166
- package/src/Ledger/TxOutputDatum.ts +0 -64
- package/src/Ledger/TxOutputId.ts +0 -63
- package/src/Ledger/ValidatorHash.ts +0 -36
- package/src/Ledger/index.ts +0 -15
- package/src/Uplc/Cek.ts +0 -92
- package/src/Uplc/Data.test.ts +0 -321
- package/src/Uplc/Data.ts +0 -657
- package/src/Uplc/Primitive.ts +0 -56
- package/src/Uplc/index.ts +0 -2
- package/src/index.ts +0 -4
- package/src/internal/Base32.test.ts +0 -219
- package/src/internal/Base32.ts +0 -341
- package/src/internal/BigEndian.test.ts +0 -79
- package/src/internal/BigEndian.ts +0 -67
- package/src/internal/Bits.test.ts +0 -300
- package/src/internal/Bits.ts +0 -398
- package/src/internal/Bytes.test.ts +0 -369
- package/src/internal/Bytes.ts +0 -343
- package/src/internal/Flat.test.ts +0 -29
- package/src/internal/Flat.ts +0 -387
- package/src/internal/Float.test.ts +0 -51
- package/src/internal/Float.ts +0 -190
- package/src/internal/Utf8.test.ts +0 -69
- package/src/internal/Utf8.ts +0 -58
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Encoding, Either } from "effect";
|
|
2
|
+
/**
|
|
3
|
+
* Custom IEEE 754 Float16 implementation, not fast, but easy to audit
|
|
4
|
+
* @param {number[]} bytes
|
|
5
|
+
* @returns {number}
|
|
6
|
+
*/
|
|
7
|
+
export declare function decodeFloat16(bytes: number[]): Either.Either<number, Encoding.DecodeException>;
|
|
8
|
+
/**
|
|
9
|
+
* Custom IEEE 754 Float16 implementation, not fast, but easy to audit
|
|
10
|
+
* @param f
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
export declare function encodeFloat16(f: number): number[];
|
|
14
|
+
/**
|
|
15
|
+
* Leverages the builtin DataView class to decode a IEEE 754 float32 number
|
|
16
|
+
* @param {number[]} bytes
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
19
|
+
export declare function decodeFloat32(bytes: number[]): Either.Either<number, Encoding.DecodeException>;
|
|
20
|
+
/**
|
|
21
|
+
* Leverages the builtin DataView class to encode a floating point number using IEEE 754 float32 encoding
|
|
22
|
+
* @param f
|
|
23
|
+
* @returns
|
|
24
|
+
*/
|
|
25
|
+
export declare function encodeFloat32(f: number): number[];
|
|
26
|
+
/**
|
|
27
|
+
* Leverages the builtin DataView class to decode a IEEE 754 float64 number
|
|
28
|
+
* @param bytes
|
|
29
|
+
* @returns
|
|
30
|
+
*/
|
|
31
|
+
export declare function decodeFloat64(bytes: number[]): Either.Either<number, Encoding.DecodeException>;
|
|
32
|
+
/**
|
|
33
|
+
* Leverages the builtin DataView class to encode a floating point number using IEEE 754 float64 encoding
|
|
34
|
+
* @param f
|
|
35
|
+
* @returns
|
|
36
|
+
*/
|
|
37
|
+
export declare function encodeFloat64(f: number): number[];
|
|
38
|
+
//# sourceMappingURL=Float.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Float.d.ts","sourceRoot":"","sources":["../../src/internal/Float.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAuCzC;;;;GAIG;AACH,wBAAgB,aAAa,CAC3B,KAAK,EAAE,MAAM,EAAE,GACd,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,eAAe,CAAC,CAiCjD;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAkCjD;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAC3B,KAAK,EAAE,MAAM,EAAE,GACd,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,eAAe,CAAC,CAajD;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAMjD;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAC3B,KAAK,EAAE,MAAM,EAAE,GACd,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,eAAe,CAAC,CAYjD;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAMjD"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Effect, Encoding } from "effect";
|
|
2
|
+
/**
|
|
3
|
+
* Decodes a list of uint8 bytes into a string using UTF-8 encoding.
|
|
4
|
+
* @example
|
|
5
|
+
* bytesToUtf8([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]) == "hello world"
|
|
6
|
+
* @param bytes
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
export declare function decode(bytes: string | number[] | Uint8Array): Effect.Effect<string, Encoding.DecodeException>;
|
|
10
|
+
/**
|
|
11
|
+
* Encodes a string into a list of uint8 bytes using UTF-8 encoding.
|
|
12
|
+
* @example
|
|
13
|
+
* utf8ToBytes("hello world") == [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
|
|
14
|
+
* @param str
|
|
15
|
+
* @returns
|
|
16
|
+
*/
|
|
17
|
+
export declare function encode(str: string): Uint8Array;
|
|
18
|
+
/**
|
|
19
|
+
* Tests if a uint8 array is valid utf8 encoding.
|
|
20
|
+
* @param {number[]} bytes
|
|
21
|
+
* @returns {boolean}
|
|
22
|
+
*/
|
|
23
|
+
export declare function isValid(bytes: string | number[] | Uint8Array): boolean;
|
|
24
|
+
//# sourceMappingURL=Utf8.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Utf8.d.ts","sourceRoot":"","sources":["../../src/internal/Utf8.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAA;AAGzC;;;;;;GAMG;AACH,wBAAgB,MAAM,CACpB,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,UAAU,GACpC,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,eAAe,CAAC,CAUjD;AAED;;;;;;GAMG;AACH,wBAAgB,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAE9C;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,UAAU,GAAG,OAAO,CAiBtE"}
|
package/src/Bech32.test.ts
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "bun:test"
|
|
2
|
-
import { Either } from "effect"
|
|
3
|
-
import * as Bech32 from "./Bech32.js"
|
|
4
|
-
import * as Utf8 from "./internal/Utf8.js"
|
|
5
|
-
|
|
6
|
-
describe("Bech32.encode", () => {
|
|
7
|
-
it("fails with empty human readable part", () => {
|
|
8
|
-
expect(() => Bech32.encode("", [])).toThrow()
|
|
9
|
-
})
|
|
10
|
-
|
|
11
|
-
it('returns "foo1vehk7cnpwgry9h96" for "foobar" with "foo" human-readable-part', () => {
|
|
12
|
-
expect(Bech32.encode("foo", Utf8.encode("foobar"))).toBe(
|
|
13
|
-
"foo1vehk7cnpwgry9h96"
|
|
14
|
-
)
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
it('returns "addr_test1wz54prcptnaullpa3zkyc8ynfddc954m9qw5v3nj7mzf2wggs2uld" for #70a9508f015cfbcffc3d88ac4c1c934b5b82d2bb281d464672f6c49539 with "addr_test" human-readable-part', () => {
|
|
18
|
-
expect(
|
|
19
|
-
Bech32.encode(
|
|
20
|
-
"addr_test",
|
|
21
|
-
"70a9508f015cfbcffc3d88ac4c1c934b5b82d2bb281d464672f6c49539"
|
|
22
|
-
)
|
|
23
|
-
).toBe("addr_test1wz54prcptnaullpa3zkyc8ynfddc954m9qw5v3nj7mzf2wggs2uld")
|
|
24
|
-
})
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
describe("Bech32.decode", () => {
|
|
28
|
-
it("fails for empty string", () => {
|
|
29
|
-
expect(Bech32.decode("")._tag).toBe("Left")
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
it("fails for random string", () => {
|
|
33
|
-
expect(Bech32.decode("balbalbal")._tag).toBe("Left")
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
it('returns #70a9508f015cfbcffc3d88ac4c1c934b5b82d2bb281d464672f6c49539 for "addr_test1wz54prcptnaullpa3zkyc8ynfddc954m9qw5v3nj7mzf2wggs2uld"', () => {
|
|
37
|
-
expect(
|
|
38
|
-
Bech32.decode(
|
|
39
|
-
"addr_test1wz54prcptnaullpa3zkyc8ynfddc954m9qw5v3nj7mzf2wggs2uld"
|
|
40
|
-
)
|
|
41
|
-
).toEqual(
|
|
42
|
-
Either.right({
|
|
43
|
-
prefix: "addr_test",
|
|
44
|
-
bytes: [
|
|
45
|
-
0x70, 0xa9, 0x50, 0x8f, 0x01, 0x5c, 0xfb, 0xcf, 0xfc, 0x3d, 0x88,
|
|
46
|
-
0xac, 0x4c, 0x1c, 0x93, 0x4b, 0x5b, 0x82, 0xd2, 0xbb, 0x28, 0x1d,
|
|
47
|
-
0x46, 0x46, 0x72, 0xf6, 0xc4, 0x95, 0x39
|
|
48
|
-
]
|
|
49
|
-
})
|
|
50
|
-
)
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
it("for script1agrmwv7exgffcdu27cn5xmnuhsh0p0ukuqpkhdgm800xksw7e2w", () => {
|
|
54
|
-
expect(
|
|
55
|
-
Bech32.decode(
|
|
56
|
-
"script1agrmwv7exgffcdu27cn5xmnuhsh0p0ukuqpkhdgm800xksw7e2w"
|
|
57
|
-
)
|
|
58
|
-
).toEqual(
|
|
59
|
-
Either.right({
|
|
60
|
-
prefix: "script",
|
|
61
|
-
bytes: [
|
|
62
|
-
0xea, 0x07, 0xb7, 0x33, 0xd9, 0x32, 0x12, 0x9c, 0x37, 0x8a, 0xf6,
|
|
63
|
-
0x27, 0x43, 0x6e, 0x7c, 0xbc, 0x2e, 0xf0, 0xbf, 0x96, 0xe0, 0x03,
|
|
64
|
-
0x6b, 0xb5, 0x1b, 0x3b, 0xde, 0x6b
|
|
65
|
-
]
|
|
66
|
-
})
|
|
67
|
-
)
|
|
68
|
-
})
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
const testVector: [string, boolean][] = [
|
|
72
|
-
["", false],
|
|
73
|
-
["blablabla", false],
|
|
74
|
-
["addr_test1wz54prcptnaullpa3zkyc8ynfddc954m9qw5v3nj7mzf2wggs2uld", true],
|
|
75
|
-
["foo1vehk7cnpwgry9h96", true],
|
|
76
|
-
["foo1vehk7cnpwgry9h97", false],
|
|
77
|
-
["a12uel5l", true],
|
|
78
|
-
["mm1crxm3i", false],
|
|
79
|
-
["A1G7SGD8", false],
|
|
80
|
-
["abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw", true],
|
|
81
|
-
["?1ezyfcl", true],
|
|
82
|
-
["addr_test1wz54prcptnaullpa3zkyc8ynfddc954m9qw5v3nj7mzf2wggs2uld", true]
|
|
83
|
-
]
|
|
84
|
-
|
|
85
|
-
describe("Bech32.isValid", () => {
|
|
86
|
-
testVector.forEach(([encoded, expected]) => {
|
|
87
|
-
it(`returns ${expected} for "${encoded}"`, () => {
|
|
88
|
-
expect(Bech32.isValid(encoded)).toBe(expected)
|
|
89
|
-
})
|
|
90
|
-
})
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
describe("Bech32.decode()/Bech32.encode() roundtrip", () => {
|
|
94
|
-
const roundtrip = (encoded: string): string => {
|
|
95
|
-
const decodeResult = Bech32.decode(encoded)
|
|
96
|
-
if (decodeResult._tag == "Left") {
|
|
97
|
-
// eslint-disable-next-line @typescript-eslint/only-throw-error
|
|
98
|
-
throw decodeResult.left
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const { prefix, bytes } = decodeResult.right
|
|
102
|
-
|
|
103
|
-
return Bech32.encode(prefix, bytes)
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
testVector.forEach(([encoded, expected]) => {
|
|
107
|
-
if (expected) {
|
|
108
|
-
it(`ok for "${encoded}"`, () => {
|
|
109
|
-
expect(roundtrip(encoded)).toBe(encoded)
|
|
110
|
-
})
|
|
111
|
-
} else {
|
|
112
|
-
it(`fails for "${encoded}"`, () => {
|
|
113
|
-
expect(() => roundtrip(encoded)).toThrow()
|
|
114
|
-
})
|
|
115
|
-
}
|
|
116
|
-
})
|
|
117
|
-
})
|
package/src/Bech32.ts
DELETED
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
import { Either, Encoding } from "effect"
|
|
2
|
-
import * as Base32 from "./internal/Base32.js"
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Bech32 base32 alphabet
|
|
6
|
-
*/
|
|
7
|
-
const ALPHABET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l" as const
|
|
8
|
-
|
|
9
|
-
const PAYLOAD_CODEC = /* @__PURE__ */ Base32.make({
|
|
10
|
-
alphabet: ALPHABET
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Decomposes a Bech32 checksummed string (eg. a Cardano address), and returns the human readable part and the original bytes
|
|
15
|
-
* Throws an error if checksum is invalid.
|
|
16
|
-
* @param encoded
|
|
17
|
-
* @returns
|
|
18
|
-
* `prefix` part is the human-readable part, `bytes` part is a list containing the underlying bytes.
|
|
19
|
-
*/
|
|
20
|
-
export function decode(
|
|
21
|
-
encoded: string
|
|
22
|
-
): Either.Either<
|
|
23
|
-
{ prefix: string; bytes: number[] },
|
|
24
|
-
Encoding.DecodeException
|
|
25
|
-
> {
|
|
26
|
-
const [prefix, payload] = split(encoded)
|
|
27
|
-
|
|
28
|
-
if (!verifySplit(prefix, payload)) {
|
|
29
|
-
return Either.left(
|
|
30
|
-
Encoding.DecodeException(encoded, "invalid bech32 encoding")
|
|
31
|
-
)
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const bytes = PAYLOAD_CODEC.decode(payload.slice(0, payload.length - 6))
|
|
35
|
-
|
|
36
|
-
if (bytes._tag == "Left") {
|
|
37
|
-
return Either.left(bytes.left)
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return Either.right({ prefix, bytes: Array.from(bytes.right) })
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Creates a Bech32 checksummed string (eg. used to represent Cardano addresses).
|
|
45
|
-
* @param prefix
|
|
46
|
-
* human-readable part (eg. "addr")
|
|
47
|
-
* @param payload
|
|
48
|
-
* Hex encoded or a list of uint8 bytes
|
|
49
|
-
* @returns
|
|
50
|
-
* @throws
|
|
51
|
-
* If prefix is empty
|
|
52
|
-
*/
|
|
53
|
-
export function encode(
|
|
54
|
-
prefix: string,
|
|
55
|
-
payload: string | number[] | Uint8Array
|
|
56
|
-
): string {
|
|
57
|
-
if (prefix.length == 0) {
|
|
58
|
-
throw new Error("human-readable-part must have non-zero length")
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
payload = PAYLOAD_CODEC.encodeRaw(payload)
|
|
62
|
-
|
|
63
|
-
const chkSum = calcChecksum(prefix, payload)
|
|
64
|
-
|
|
65
|
-
return (
|
|
66
|
-
prefix +
|
|
67
|
-
"1" +
|
|
68
|
-
payload
|
|
69
|
-
.concat(chkSum)
|
|
70
|
-
.map((i) => ALPHABET[i])
|
|
71
|
-
.join("")
|
|
72
|
-
)
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Verifies a Bech32 checksum. Prefix must be checked externally
|
|
77
|
-
* @param {string} encoded
|
|
78
|
-
* @returns {boolean}
|
|
79
|
-
*/
|
|
80
|
-
export function isValid(encoded: string): boolean {
|
|
81
|
-
const [prefix, payload] = split(encoded)
|
|
82
|
-
|
|
83
|
-
return verifySplit(prefix, payload)
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Expand human readable prefix of the bech32 encoding so it can be used in the checkSum.
|
|
88
|
-
* @param prefix
|
|
89
|
-
* @returns
|
|
90
|
-
*/
|
|
91
|
-
function expandPrefix(prefix: string): number[] {
|
|
92
|
-
const bytes = []
|
|
93
|
-
for (const c of prefix) {
|
|
94
|
-
bytes.push(c.charCodeAt(0) >> 5)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
bytes.push(0)
|
|
98
|
-
|
|
99
|
-
for (const c of prefix) {
|
|
100
|
-
bytes.push(c.charCodeAt(0) & 31)
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return bytes
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Split bech32 encoded string into human-readable-part and payload part.
|
|
108
|
-
* @param encoded
|
|
109
|
-
* @returns
|
|
110
|
-
* First item is human-readable-part, second part is payload part
|
|
111
|
-
*/
|
|
112
|
-
function split(encoded: string): [string, string] {
|
|
113
|
-
const i = encoded.indexOf("1")
|
|
114
|
-
|
|
115
|
-
if (i == -1 || i == 0) {
|
|
116
|
-
return ["", encoded]
|
|
117
|
-
} else {
|
|
118
|
-
return [encoded.slice(0, i), encoded.slice(i + 1)]
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Used as part of the bech32 checksum.
|
|
124
|
-
* @param bytes
|
|
125
|
-
* @returns
|
|
126
|
-
*/
|
|
127
|
-
function polymod(bytes: number[]): number {
|
|
128
|
-
const GEN = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3]
|
|
129
|
-
|
|
130
|
-
let chk = 1
|
|
131
|
-
for (const b of bytes) {
|
|
132
|
-
const c = chk >> 25
|
|
133
|
-
chk = ((chk & 0x1fffffff) << 5) ^ b
|
|
134
|
-
|
|
135
|
-
for (let i = 0; i < 5; i++) {
|
|
136
|
-
if (((c >> i) & 1) != 0) {
|
|
137
|
-
chk ^= GEN[i]
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
return chk
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Generate the bech32 checksum.
|
|
147
|
-
* @param prefix
|
|
148
|
-
* @param payload
|
|
149
|
-
* numbers between 0 and 32
|
|
150
|
-
* @returns
|
|
151
|
-
* 6 numbers between 0 and 32
|
|
152
|
-
*/
|
|
153
|
-
function calcChecksum(prefix: string, payload: number[]): number[] {
|
|
154
|
-
const bytes = expandPrefix(prefix).concat(payload)
|
|
155
|
-
|
|
156
|
-
const chk = polymod(bytes.concat([0, 0, 0, 0, 0, 0])) ^ 1
|
|
157
|
-
|
|
158
|
-
const chkSum: number[] = []
|
|
159
|
-
for (let i = 0; i < 6; i++) {
|
|
160
|
-
chkSum.push((chk >> (5 * (5 - i))) & 31)
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
return chkSum
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* @param prefix
|
|
168
|
-
* @param payload
|
|
169
|
-
* @returns
|
|
170
|
-
*/
|
|
171
|
-
function verifySplit(prefix: string, payload: string): boolean {
|
|
172
|
-
if (prefix.length == 0) {
|
|
173
|
-
return false
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
const data: number[] = []
|
|
177
|
-
|
|
178
|
-
for (const c of payload) {
|
|
179
|
-
const j = ALPHABET.indexOf(c)
|
|
180
|
-
if (j == -1) {
|
|
181
|
-
return false
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
data.push(j)
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
const chkSumA = data.slice(data.length - 6)
|
|
188
|
-
|
|
189
|
-
const chkSumB = calcChecksum(prefix, data.slice(0, data.length - 6))
|
|
190
|
-
|
|
191
|
-
for (let j = 0; j < 6; j++) {
|
|
192
|
-
if (chkSumA[j] != chkSumB[j]) {
|
|
193
|
-
return false
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
return true
|
|
198
|
-
}
|