1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const baseCodec = require("base-x");
|
7 | const utils_1 = require("./utils");
|
8 | class Codec {
|
9 | constructor(options) {
|
10 | this.sha256 = options.sha256;
|
11 | this.alphabet = options.alphabet;
|
12 | this.codec = baseCodec(this.alphabet);
|
13 | this.base = this.alphabet.length;
|
14 | }
|
15 | |
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | encode(bytes, opts) {
|
22 | const versions = opts.versions;
|
23 | return this.encodeVersioned(bytes, versions, opts.expectedLength);
|
24 | }
|
25 | encodeVersioned(bytes, versions, expectedLength) {
|
26 | if (expectedLength && bytes.length !== expectedLength) {
|
27 | throw new Error('unexpected_payload_length: bytes.length does not match expectedLength.' +
|
28 | ' Ensure that the bytes are a Buffer.');
|
29 | }
|
30 | return this.encodeChecked(Buffer.from(utils_1.concatArgs(versions, bytes)));
|
31 | }
|
32 | encodeChecked(buffer) {
|
33 | const check = this.sha256(this.sha256(buffer)).slice(0, 4);
|
34 | return this.encodeRaw(Buffer.from(utils_1.concatArgs(buffer, check)));
|
35 | }
|
36 | encodeRaw(bytes) {
|
37 | return this.codec.encode(bytes);
|
38 | }
|
39 | |
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | decode(base58string, opts) {
|
46 | const versions = opts.versions;
|
47 | const types = opts.versionTypes;
|
48 | const withoutSum = this.decodeChecked(base58string);
|
49 | if (versions.length > 1 && !opts.expectedLength) {
|
50 | throw new Error('expectedLength is required because there are >= 2 possible versions');
|
51 | }
|
52 | const versionLengthGuess = typeof versions[0] === 'number' ? 1 : versions[0].length;
|
53 | const payloadLength = opts.expectedLength || withoutSum.length - versionLengthGuess;
|
54 | const versionBytes = withoutSum.slice(0, -payloadLength);
|
55 | const payload = withoutSum.slice(-payloadLength);
|
56 | for (let i = 0; i < versions.length; i++) {
|
57 | const version = Array.isArray(versions[i]) ? versions[i] : [versions[i]];
|
58 | if (utils_1.seqEqual(versionBytes, version)) {
|
59 | return {
|
60 | version,
|
61 | bytes: payload,
|
62 | type: types ? types[i] : null
|
63 | };
|
64 | }
|
65 | }
|
66 | throw new Error('version_invalid: version bytes do not match any of the provided version(s)');
|
67 | }
|
68 | decodeChecked(base58string) {
|
69 | const buffer = this.decodeRaw(base58string);
|
70 | if (buffer.length < 5) {
|
71 | throw new Error('invalid_input_size: decoded data must have length >= 5');
|
72 | }
|
73 | if (!this.verifyCheckSum(buffer)) {
|
74 | throw new Error('checksum_invalid');
|
75 | }
|
76 | return buffer.slice(0, -4);
|
77 | }
|
78 | decodeRaw(base58string) {
|
79 | return this.codec.decode(base58string);
|
80 | }
|
81 | verifyCheckSum(bytes) {
|
82 | const computed = this.sha256(this.sha256(bytes.slice(0, -4))).slice(0, 4);
|
83 | const checksum = bytes.slice(-4);
|
84 | return utils_1.seqEqual(computed, checksum);
|
85 | }
|
86 | }
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | const createHash = require('create-hash');
|
92 |
|
93 | const ACCOUNT_ID = 0;
|
94 | const ACCOUNT_PUBLIC_KEY = 0x23;
|
95 | const FAMILY_SEED = 0x21;
|
96 | const NODE_PUBLIC = 0x1C;
|
97 | const ED25519_SEED = [0x01, 0xE1, 0x4B];
|
98 | const codecOptions = {
|
99 | sha256: function (bytes) {
|
100 | return createHash('sha256').update(Buffer.from(bytes)).digest();
|
101 | },
|
102 | alphabet: 'rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz'
|
103 | };
|
104 | const codecWithXrpAlphabet = new Codec(codecOptions);
|
105 | exports.codec = codecWithXrpAlphabet;
|
106 |
|
107 |
|
108 | function encodeSeed(entropy, type) {
|
109 | if (entropy.length !== 16) {
|
110 | throw new Error('entropy must have length 16');
|
111 | }
|
112 | const opts = {
|
113 | expectedLength: 16,
|
114 |
|
115 | versions: type === 'ed25519' ? ED25519_SEED : [FAMILY_SEED]
|
116 | };
|
117 |
|
118 | return codecWithXrpAlphabet.encode(entropy, opts);
|
119 | }
|
120 | exports.encodeSeed = encodeSeed;
|
121 | function decodeSeed(seed, opts = {
|
122 | versionTypes: ['ed25519', 'secp256k1'],
|
123 | versions: [ED25519_SEED, FAMILY_SEED],
|
124 | expectedLength: 16
|
125 | }) {
|
126 | return codecWithXrpAlphabet.decode(seed, opts);
|
127 | }
|
128 | exports.decodeSeed = decodeSeed;
|
129 | function encodeAccountID(bytes) {
|
130 | const opts = { versions: [ACCOUNT_ID], expectedLength: 20 };
|
131 | return codecWithXrpAlphabet.encode(bytes, opts);
|
132 | }
|
133 | exports.encodeAccountID = encodeAccountID;
|
134 | exports.encodeAddress = encodeAccountID;
|
135 | function decodeAccountID(accountId) {
|
136 | const opts = { versions: [ACCOUNT_ID], expectedLength: 20 };
|
137 | return codecWithXrpAlphabet.decode(accountId, opts).bytes;
|
138 | }
|
139 | exports.decodeAccountID = decodeAccountID;
|
140 | exports.decodeAddress = decodeAccountID;
|
141 | function decodeNodePublic(base58string) {
|
142 | const opts = { versions: [NODE_PUBLIC], expectedLength: 33 };
|
143 | return codecWithXrpAlphabet.decode(base58string, opts).bytes;
|
144 | }
|
145 | exports.decodeNodePublic = decodeNodePublic;
|
146 | function encodeNodePublic(bytes) {
|
147 | const opts = { versions: [NODE_PUBLIC], expectedLength: 33 };
|
148 | return codecWithXrpAlphabet.encode(bytes, opts);
|
149 | }
|
150 | exports.encodeNodePublic = encodeNodePublic;
|
151 | function encodeAccountPublic(bytes) {
|
152 | const opts = { versions: [ACCOUNT_PUBLIC_KEY], expectedLength: 33 };
|
153 | return codecWithXrpAlphabet.encode(bytes, opts);
|
154 | }
|
155 | exports.encodeAccountPublic = encodeAccountPublic;
|
156 | function decodeAccountPublic(base58string) {
|
157 | const opts = { versions: [ACCOUNT_PUBLIC_KEY], expectedLength: 33 };
|
158 | return codecWithXrpAlphabet.decode(base58string, opts).bytes;
|
159 | }
|
160 | exports.decodeAccountPublic = decodeAccountPublic;
|
161 | function isValidClassicAddress(address) {
|
162 | try {
|
163 | decodeAccountID(address);
|
164 | }
|
165 | catch (e) {
|
166 | return false;
|
167 | }
|
168 | return true;
|
169 | }
|
170 | exports.isValidClassicAddress = isValidClassicAddress;
|
171 |
|
\ | No newline at end of file |