@tezos-x/octez.js-michel-codec 0.9.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/README.md +174 -0
- package/dist/lib/base58.js +255 -0
- package/dist/lib/binary.js +1272 -0
- package/dist/lib/errors.js +90 -0
- package/dist/lib/formatters.js +60 -0
- package/dist/lib/global-constants.js +12 -0
- package/dist/lib/macros.js +548 -0
- package/dist/lib/micheline-emitter.js +132 -0
- package/dist/lib/micheline-parser.js +403 -0
- package/dist/lib/micheline.js +5 -0
- package/dist/lib/michelson-contract.js +93 -0
- package/dist/lib/michelson-typecheck.js +1752 -0
- package/dist/lib/michelson-types.js +83 -0
- package/dist/lib/michelson-validator.js +835 -0
- package/dist/lib/octez.js-michel-codec.js +38 -0
- package/dist/lib/scan.js +128 -0
- package/dist/lib/utils.js +321 -0
- package/dist/lib/version.js +8 -0
- package/dist/octez.js-michel-codec.es6.js +5790 -0
- package/dist/octez.js-michel-codec.es6.js.map +1 -0
- package/dist/octez.js-michel-codec.umd.js +5854 -0
- package/dist/octez.js-michel-codec.umd.js.map +1 -0
- package/dist/types/base58.d.ts +13 -0
- package/dist/types/binary.d.ts +127 -0
- package/dist/types/errors.d.ts +57 -0
- package/dist/types/formatters.d.ts +6 -0
- package/dist/types/global-constants.d.ts +3 -0
- package/dist/types/macros.d.ts +13 -0
- package/dist/types/micheline-emitter.d.ts +17 -0
- package/dist/types/micheline-parser.d.ts +130 -0
- package/dist/types/micheline.d.ts +44 -0
- package/dist/types/michelson-contract.d.ts +24 -0
- package/dist/types/michelson-typecheck.d.ts +38 -0
- package/dist/types/michelson-types.d.ts +147 -0
- package/dist/types/michelson-validator.d.ts +66 -0
- package/dist/types/octez.js-michel-codec.d.ts +16 -0
- package/dist/types/scan.d.ts +26 -0
- package/dist/types/utils.d.ts +77 -0
- package/dist/types/version.d.ts +4 -0
- package/package.json +94 -0
- package/signature.json +315 -0
package/README.md
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# octez.js michel-codec package
|
|
2
|
+
|
|
3
|
+
**Maintained by [Nomadic Labs](https://www.nomadic-labs.com/).**
|
|
4
|
+
|
|
5
|
+
`@tezos-x/octez.js-michel-codec` converts and validates Michelson expressions between JSON-based Michelson and Micheline.
|
|
6
|
+
|
|
7
|
+
This package can:
|
|
8
|
+
|
|
9
|
+
- Retrieve Michelson in JSON form from the Tezos Node RPC and convert it to plain Michelson
|
|
10
|
+
- Parse plain Michelson (including Macros) and expand/convert it to JSON Michelson suitable for injection into the Tezos Blockchain
|
|
11
|
+
- Validate Michelson to ensure correctness
|
|
12
|
+
|
|
13
|
+
See the top-level project [https://github.com/trilitech/octez.js](https://github.com/trilitech/octez.js) for details on reporting issues, contributing and versioning.
|
|
14
|
+
|
|
15
|
+
## Examples
|
|
16
|
+
|
|
17
|
+
### Michelson expression to JSON
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
const code = `(Pair
|
|
21
|
+
(Pair { Elt 1 (Pair (Pair "tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" "tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx") 0x0501000000026869)}
|
|
22
|
+
10000000)
|
|
23
|
+
(Pair 2 333)
|
|
24
|
+
)`;
|
|
25
|
+
|
|
26
|
+
const p = new Parser();
|
|
27
|
+
|
|
28
|
+
const result = p.parseMichelineExpression(code);
|
|
29
|
+
console.log(JSON.stringify(result));
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Output:
|
|
33
|
+
|
|
34
|
+
```json
|
|
35
|
+
{
|
|
36
|
+
"prim": "Pair",
|
|
37
|
+
"args": [
|
|
38
|
+
{
|
|
39
|
+
"prim": "Pair",
|
|
40
|
+
"args": [
|
|
41
|
+
[
|
|
42
|
+
{
|
|
43
|
+
"prim": "Elt",
|
|
44
|
+
"args": [
|
|
45
|
+
{ "int": "1" },
|
|
46
|
+
{
|
|
47
|
+
"prim": "Pair",
|
|
48
|
+
"args": [
|
|
49
|
+
{
|
|
50
|
+
"prim": "Pair",
|
|
51
|
+
"args": [
|
|
52
|
+
{ "string": "tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" },
|
|
53
|
+
{ "string": "tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" }
|
|
54
|
+
]
|
|
55
|
+
},
|
|
56
|
+
{ "bytes": "0501000000026869" }
|
|
57
|
+
]
|
|
58
|
+
}
|
|
59
|
+
]
|
|
60
|
+
}
|
|
61
|
+
],
|
|
62
|
+
{ "int": "10000000" }
|
|
63
|
+
]
|
|
64
|
+
},
|
|
65
|
+
{ "prim": "Pair", "args": [{ "int": "2" }, { "int": "333" }] }
|
|
66
|
+
]
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Pretty Print a Michelson contract
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
const contract = await Tezos.contract.at('KT1EctCuorV2NfVb1XTQgvzJ88MQtWP8cMMv')
|
|
74
|
+
const p = new Parser()
|
|
75
|
+
|
|
76
|
+
const michelsonCode = p.parseJSON(contract.script.code as JSON[])
|
|
77
|
+
console.log(emitMicheline(michelsonCode, {indent:" ", newline: "\n",}))
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Pack Michelson data
|
|
81
|
+
|
|
82
|
+
Serializes any value of packable type to its optimized binary representation identical to the one used by PACK and UNPACK Michelson instructions.
|
|
83
|
+
Without a type definition (not recommended) the data will be encoded as a binary form of a generic Michelson expression.
|
|
84
|
+
Type definition allows some types like `timestamp` and `address` and other base58 representable types to be encoded to corresponding optimized binary forms borrowed from the Tezos protocol.
|
|
85
|
+
|
|
86
|
+
```js
|
|
87
|
+
const data: MichelsonData = {
|
|
88
|
+
string: 'KT1RvkwF4F7pz1gCoxkyZrG1RkrxQy3gmFTv%foo',
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const typ: MichelsonType = {
|
|
92
|
+
prim: 'address',
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const packed = packData(data, typ);
|
|
96
|
+
// 050a0000001901be41ee922ddd2cf33201e49d32da0afec571dce300666f6f
|
|
97
|
+
|
|
98
|
+
// alternatively
|
|
99
|
+
const packedBytes = packDataBytes(data, typ);
|
|
100
|
+
// { bytes: "050a0000001901be41ee922ddd2cf33201e49d32da0afec571dce300666f6f" }
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Without a type definition the base58 encoded address will be treated as a string
|
|
104
|
+
|
|
105
|
+
```js
|
|
106
|
+
const data: MichelsonData = {
|
|
107
|
+
string: 'KT1RvkwF4F7pz1gCoxkyZrG1RkrxQy3gmFTv%foo',
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const packed = packData(data);
|
|
111
|
+
// 0501000000284b543152766b7746344637707a3167436f786b795a724731526b7278517933676d46547625666f6f
|
|
112
|
+
|
|
113
|
+
// alternatively
|
|
114
|
+
const packedBytes = packDataBytes(data);
|
|
115
|
+
// {
|
|
116
|
+
// bytes: "0501000000284b543152766b7746344637707a3167436f786b795a724731526b7278517933676d46547625666f6f"
|
|
117
|
+
// }
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Unpack Michelson data
|
|
121
|
+
|
|
122
|
+
Deserialize a byte array into the corresponding Michelson value.
|
|
123
|
+
Without a type definition (not recommended) the binary data will be treated as a binary form of a generic Michelson expression and returned as is.
|
|
124
|
+
Type definition allows some types like `timestamp` and `address` and other types usually encoded in optimized binary forms to be transformed back to their string representations like base58 and ISO timestamps.
|
|
125
|
+
|
|
126
|
+
```js
|
|
127
|
+
const src = [0x05, 0x00, 0xa7, 0xe8, 0xe4, 0xd8, 0x0b];
|
|
128
|
+
|
|
129
|
+
const typ: MichelsonType = {
|
|
130
|
+
prim: 'timestamp',
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const data = unpackData(src, typ);
|
|
134
|
+
// { string: "2019-09-26T10:59:51Z" }
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Alternatively
|
|
138
|
+
|
|
139
|
+
```js
|
|
140
|
+
const src = { bytes: '0500a7e8e4d80b' };
|
|
141
|
+
|
|
142
|
+
const typ: MichelsonType = {
|
|
143
|
+
prim: 'timestamp',
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const data = unpackDataBytes(src, typ);
|
|
147
|
+
// { string: "2019-09-26T10:59:51Z" }
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Same binary data without a type definition
|
|
151
|
+
|
|
152
|
+
```js
|
|
153
|
+
const src = [0x05, 0x00, 0xa7, 0xe8, 0xe4, 0xd8, 0x0b];
|
|
154
|
+
|
|
155
|
+
const data = unpackData(src);
|
|
156
|
+
// { int: "1569495591" }
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Alternatively
|
|
160
|
+
|
|
161
|
+
```js
|
|
162
|
+
const src = { bytes: '0500a7e8e4d80b' };
|
|
163
|
+
|
|
164
|
+
const data = unpackDataBytes(src);
|
|
165
|
+
// { int: "1569495591" }
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## API Documentation
|
|
169
|
+
|
|
170
|
+
TypeDoc style documentation is available on-line [here](https://octez.js.io/typedoc/modules/_octez.js_michel_codec.html)
|
|
171
|
+
|
|
172
|
+
## Disclaimer
|
|
173
|
+
|
|
174
|
+
THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Base58DecodingError = void 0;
|
|
4
|
+
exports.decodeBase58 = decodeBase58;
|
|
5
|
+
exports.encodeBase58 = encodeBase58;
|
|
6
|
+
exports.decodeBase58Check = decodeBase58Check;
|
|
7
|
+
exports.encodeBase58Check = encodeBase58Check;
|
|
8
|
+
const octez_js_core_1 = require("@tezos-x/octez.js-core");
|
|
9
|
+
const H = [
|
|
10
|
+
0x6a09e667 | 0,
|
|
11
|
+
0xbb67ae85 | 0,
|
|
12
|
+
0x3c6ef372 | 0,
|
|
13
|
+
0xa54ff53a | 0,
|
|
14
|
+
0x510e527f | 0,
|
|
15
|
+
0x9b05688c | 0,
|
|
16
|
+
0x1f83d9ab | 0,
|
|
17
|
+
0x5be0cd19 | 0,
|
|
18
|
+
];
|
|
19
|
+
const K = [
|
|
20
|
+
0x428a2f98 | 0,
|
|
21
|
+
0x71374491 | 0,
|
|
22
|
+
0xb5c0fbcf | 0,
|
|
23
|
+
0xe9b5dba5 | 0,
|
|
24
|
+
0x3956c25b | 0,
|
|
25
|
+
0x59f111f1 | 0,
|
|
26
|
+
0x923f82a4 | 0,
|
|
27
|
+
0xab1c5ed5 | 0,
|
|
28
|
+
0xd807aa98 | 0,
|
|
29
|
+
0x12835b01 | 0,
|
|
30
|
+
0x243185be | 0,
|
|
31
|
+
0x550c7dc3 | 0,
|
|
32
|
+
0x72be5d74 | 0,
|
|
33
|
+
0x80deb1fe | 0,
|
|
34
|
+
0x9bdc06a7 | 0,
|
|
35
|
+
0xc19bf174 | 0,
|
|
36
|
+
0xe49b69c1 | 0,
|
|
37
|
+
0xefbe4786 | 0,
|
|
38
|
+
0x0fc19dc6 | 0,
|
|
39
|
+
0x240ca1cc | 0,
|
|
40
|
+
0x2de92c6f | 0,
|
|
41
|
+
0x4a7484aa | 0,
|
|
42
|
+
0x5cb0a9dc | 0,
|
|
43
|
+
0x76f988da | 0,
|
|
44
|
+
0x983e5152 | 0,
|
|
45
|
+
0xa831c66d | 0,
|
|
46
|
+
0xb00327c8 | 0,
|
|
47
|
+
0xbf597fc7 | 0,
|
|
48
|
+
0xc6e00bf3 | 0,
|
|
49
|
+
0xd5a79147 | 0,
|
|
50
|
+
0x06ca6351 | 0,
|
|
51
|
+
0x14292967 | 0,
|
|
52
|
+
0x27b70a85 | 0,
|
|
53
|
+
0x2e1b2138 | 0,
|
|
54
|
+
0x4d2c6dfc | 0,
|
|
55
|
+
0x53380d13 | 0,
|
|
56
|
+
0x650a7354 | 0,
|
|
57
|
+
0x766a0abb | 0,
|
|
58
|
+
0x81c2c92e | 0,
|
|
59
|
+
0x92722c85 | 0,
|
|
60
|
+
0xa2bfe8a1 | 0,
|
|
61
|
+
0xa81a664b | 0,
|
|
62
|
+
0xc24b8b70 | 0,
|
|
63
|
+
0xc76c51a3 | 0,
|
|
64
|
+
0xd192e819 | 0,
|
|
65
|
+
0xd6990624 | 0,
|
|
66
|
+
0xf40e3585 | 0,
|
|
67
|
+
0x106aa070 | 0,
|
|
68
|
+
0x19a4c116 | 0,
|
|
69
|
+
0x1e376c08 | 0,
|
|
70
|
+
0x2748774c | 0,
|
|
71
|
+
0x34b0bcb5 | 0,
|
|
72
|
+
0x391c0cb3 | 0,
|
|
73
|
+
0x4ed8aa4a | 0,
|
|
74
|
+
0x5b9cca4f | 0,
|
|
75
|
+
0x682e6ff3 | 0,
|
|
76
|
+
0x748f82ee | 0,
|
|
77
|
+
0x78a5636f | 0,
|
|
78
|
+
0x84c87814 | 0,
|
|
79
|
+
0x8cc70208 | 0,
|
|
80
|
+
0x90befffa | 0,
|
|
81
|
+
0xa4506ceb | 0,
|
|
82
|
+
0xbef9a3f7 | 0,
|
|
83
|
+
0xc67178f2 | 0,
|
|
84
|
+
];
|
|
85
|
+
/**
|
|
86
|
+
* @category Error
|
|
87
|
+
* @description Error that indicates a failure when decoding a base58 encoding
|
|
88
|
+
*/
|
|
89
|
+
class Base58DecodingError extends octez_js_core_1.TaquitoError {
|
|
90
|
+
constructor(message) {
|
|
91
|
+
super();
|
|
92
|
+
this.message = message;
|
|
93
|
+
this.name = 'Base58DecodingError';
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
exports.Base58DecodingError = Base58DecodingError;
|
|
97
|
+
// https://tools.ietf.org/html/rfc6234
|
|
98
|
+
function sha256(msg) {
|
|
99
|
+
// pad the message
|
|
100
|
+
const r = (msg.length + 9) % 64;
|
|
101
|
+
const pad = r === 0 ? 0 : 64 - r;
|
|
102
|
+
if (msg.length > 268435455) {
|
|
103
|
+
throw new octez_js_core_1.InvalidMessageError('', `: Invalid length ${msg.length} is too big -- SHA-256.`);
|
|
104
|
+
}
|
|
105
|
+
const l = msg.length << 3;
|
|
106
|
+
const buffer = [
|
|
107
|
+
...msg,
|
|
108
|
+
0x80,
|
|
109
|
+
...new Array(pad).fill(0),
|
|
110
|
+
0,
|
|
111
|
+
0,
|
|
112
|
+
0,
|
|
113
|
+
0,
|
|
114
|
+
(l >> 24) & 0xff,
|
|
115
|
+
(l >> 16) & 0xff,
|
|
116
|
+
(l >> 8) & 0xff,
|
|
117
|
+
l & 0xff,
|
|
118
|
+
];
|
|
119
|
+
function ror(x, n) {
|
|
120
|
+
return (x >>> n) | (x << (32 - n));
|
|
121
|
+
}
|
|
122
|
+
const h = [...H];
|
|
123
|
+
const w = new Array(64);
|
|
124
|
+
const v = new Array(8);
|
|
125
|
+
for (let offset = 0; offset < buffer.length; offset += 64) {
|
|
126
|
+
let q = offset;
|
|
127
|
+
let i = 0;
|
|
128
|
+
while (i < 16) {
|
|
129
|
+
w[i] = (buffer[q] << 24) | (buffer[q + 1] << 16) | (buffer[q + 2] << 8) | buffer[q + 3];
|
|
130
|
+
q += 4;
|
|
131
|
+
i++;
|
|
132
|
+
}
|
|
133
|
+
while (i < 64) {
|
|
134
|
+
const s0 = ror(w[i - 15], 7) ^ ror(w[i - 15], 18) ^ (w[i - 15] >>> 3);
|
|
135
|
+
const s1 = ror(w[i - 2], 17) ^ ror(w[i - 2], 19) ^ (w[i - 2] >>> 10);
|
|
136
|
+
w[i] = ((s1 | 0) + w[i - 7] + s0 + w[i - 16]) | 0;
|
|
137
|
+
i++;
|
|
138
|
+
}
|
|
139
|
+
for (let i = 0; i < 8; i++) {
|
|
140
|
+
v[i] = h[i];
|
|
141
|
+
}
|
|
142
|
+
for (let i = 0; i < 64; i++) {
|
|
143
|
+
const b0 = ror(v[0], 2) ^ ror(v[0], 13) ^ ror(v[0], 22);
|
|
144
|
+
const b1 = ror(v[4], 6) ^ ror(v[4], 11) ^ ror(v[4], 25);
|
|
145
|
+
const t1 = (v[7] + b1 + ((v[4] & v[5]) ^ (~v[4] & v[6])) + K[i] + w[i]) | 0;
|
|
146
|
+
const t2 = (b0 + ((v[0] & v[1]) ^ (v[0] & v[2]) ^ (v[1] & v[2]))) | 0;
|
|
147
|
+
v[7] = v[6];
|
|
148
|
+
v[6] = v[5];
|
|
149
|
+
v[5] = v[4];
|
|
150
|
+
v[4] = (v[3] + t1) | 0;
|
|
151
|
+
v[3] = v[2];
|
|
152
|
+
v[2] = v[1];
|
|
153
|
+
v[1] = v[0];
|
|
154
|
+
v[0] = (t1 + t2) | 0;
|
|
155
|
+
}
|
|
156
|
+
for (let i = 0; i < 8; i++) {
|
|
157
|
+
h[i] = (h[i] + v[i]) | 0;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const digest = [];
|
|
161
|
+
for (const v of h) {
|
|
162
|
+
digest.push((v >> 24) & 0xff);
|
|
163
|
+
digest.push((v >> 16) & 0xff);
|
|
164
|
+
digest.push((v >> 8) & 0xff);
|
|
165
|
+
digest.push(v & 0xff);
|
|
166
|
+
}
|
|
167
|
+
return digest;
|
|
168
|
+
}
|
|
169
|
+
const base58alphabetFwd = [
|
|
170
|
+
0, 1, 2, 3, 4, 5, 6, 7, 8, -1, -1, -1, -1, -1, -1, -1, 9, 10, 11, 12, 13, 14, 15, 16, -1, 17, 18,
|
|
171
|
+
19, 20, 21, -1, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1, -1, -1, 33, 34, 35,
|
|
172
|
+
36, 37, 38, 39, 40, 41, 42, 43, -1, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
|
|
173
|
+
];
|
|
174
|
+
const base58alphabetBwd = [
|
|
175
|
+
0, 1, 2, 3, 4, 5, 6, 7, 8, 16, 17, 18, 19, 20, 21, 22, 23, 25, 26, 27, 28, 29, 31, 32, 33, 34, 35,
|
|
176
|
+
36, 37, 38, 39, 40, 41, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 60, 61, 62, 63, 64, 65, 66,
|
|
177
|
+
67, 68, 69, 70, 71, 72, 73,
|
|
178
|
+
];
|
|
179
|
+
function byteAt(src, i) {
|
|
180
|
+
const c = src.charCodeAt(i) - 49;
|
|
181
|
+
if (c >= base58alphabetFwd.length || base58alphabetFwd[c] === -1) {
|
|
182
|
+
throw new Base58DecodingError(`Unexpected character at position ${i}: ${src[i]}`);
|
|
183
|
+
}
|
|
184
|
+
return base58alphabetFwd[c];
|
|
185
|
+
}
|
|
186
|
+
function decodeBase58(src) {
|
|
187
|
+
const acc = [];
|
|
188
|
+
let i = 0;
|
|
189
|
+
// count and skip leading zeros
|
|
190
|
+
while (i < src.length && byteAt(src, i) === 0) {
|
|
191
|
+
i++;
|
|
192
|
+
}
|
|
193
|
+
let zeros = i;
|
|
194
|
+
while (i < src.length) {
|
|
195
|
+
let carry = byteAt(src, i++);
|
|
196
|
+
/*
|
|
197
|
+
for every symbol x
|
|
198
|
+
acc = acc * 58 + x
|
|
199
|
+
where acc is a little endian arbitrary length integer
|
|
200
|
+
*/
|
|
201
|
+
let ii = 0;
|
|
202
|
+
while (carry !== 0 || ii < acc.length) {
|
|
203
|
+
const m = (acc[ii] || 0) * 58 + carry;
|
|
204
|
+
acc[ii++] = m % 256;
|
|
205
|
+
carry = Math.floor(m / 256);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
while (zeros-- > 0) {
|
|
209
|
+
acc.push(0);
|
|
210
|
+
}
|
|
211
|
+
return acc.reverse();
|
|
212
|
+
}
|
|
213
|
+
function encodeBase58(src) {
|
|
214
|
+
const acc = [];
|
|
215
|
+
let i = 0;
|
|
216
|
+
// count and skip leading zeros
|
|
217
|
+
while (i < src.length && src[i] === 0) {
|
|
218
|
+
i++;
|
|
219
|
+
}
|
|
220
|
+
let zeros = i;
|
|
221
|
+
while (i < src.length) {
|
|
222
|
+
let carry = src[i++];
|
|
223
|
+
let ii = 0;
|
|
224
|
+
while (carry !== 0 || ii < acc.length) {
|
|
225
|
+
const m = (acc[ii] || 0) * 256 + carry;
|
|
226
|
+
acc[ii++] = m % 58;
|
|
227
|
+
carry = Math.floor(m / 58);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
while (zeros-- > 0) {
|
|
231
|
+
acc.push(0);
|
|
232
|
+
}
|
|
233
|
+
acc.reverse();
|
|
234
|
+
return String.fromCharCode(...acc.map((v) => base58alphabetBwd[v] + 49));
|
|
235
|
+
}
|
|
236
|
+
function decodeBase58Check(src) {
|
|
237
|
+
const buffer = decodeBase58(src);
|
|
238
|
+
if (buffer.length < 4) {
|
|
239
|
+
throw new Base58DecodingError(`Data is too short ${buffer.length}`);
|
|
240
|
+
}
|
|
241
|
+
const data = buffer.slice(0, buffer.length - 4);
|
|
242
|
+
const sum = buffer.slice(buffer.length - 4);
|
|
243
|
+
const computed = sha256(sha256(data));
|
|
244
|
+
if (sum[0] !== computed[0] ||
|
|
245
|
+
sum[1] !== computed[1] ||
|
|
246
|
+
sum[2] !== computed[2] ||
|
|
247
|
+
sum[3] !== computed[3]) {
|
|
248
|
+
throw new Base58DecodingError('Invalid checksum');
|
|
249
|
+
}
|
|
250
|
+
return data;
|
|
251
|
+
}
|
|
252
|
+
function encodeBase58Check(src) {
|
|
253
|
+
const sum = sha256(sha256(src));
|
|
254
|
+
return encodeBase58([...src, ...sum.slice(0, 4)]);
|
|
255
|
+
}
|