forked from ExodusOSS/bytes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase58check.js
More file actions
69 lines (60 loc) · 2.28 KB
/
base58check.js
File metadata and controls
69 lines (60 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { typedView } from './array.js'
import { assertUint8 } from './assert.js'
import { toBase58, fromBase58 } from './base58.js'
import { hashSync } from '@exodus/crypto/hash' // eslint-disable-line @exodus/import/no-deprecated
// Note: while API is async, we use hashSync for now until we improve webcrypto perf for hash256
// Inputs to base58 are typically very small, and that makes a difference
const E_CHECKSUM = 'Invalid checksum'
// checksum length is 4, i.e. only the first 4 bytes of the hash are used
function encodeWithChecksum(arr, checksum) {
// arr type in already validated in input
const res = new Uint8Array(arr.length + 4)
res.set(arr, 0)
res.set(checksum.subarray(0, 4), arr.length)
return toBase58(res)
}
function decodeWithChecksum(str) {
const arr = fromBase58(str) // checks input
const payloadSize = arr.length - 4
if (payloadSize < 0) throw new Error(E_CHECKSUM)
return [arr.subarray(0, payloadSize), arr.subarray(payloadSize)]
}
function assertChecksum(c, r) {
if ((c[0] ^ r[0]) | (c[1] ^ r[1]) | (c[2] ^ r[2]) | (c[3] ^ r[3])) throw new Error(E_CHECKSUM)
}
export const makeBase58check = (hashAlgo, hashAlgoSync) => {
const apis = {
async encode(arr) {
assertUint8(arr)
return encodeWithChecksum(arr, await hashAlgo(arr))
},
async decode(str, format = 'uint8') {
const [payload, checksum] = decodeWithChecksum(str)
assertChecksum(checksum, await hashAlgo(payload))
return typedView(payload, format)
},
}
if (!hashAlgoSync) return apis
return {
...apis,
encodeSync(arr) {
assertUint8(arr)
return encodeWithChecksum(arr, hashAlgoSync(arr))
},
decodeSync(str, format = 'uint8') {
const [payload, checksum] = decodeWithChecksum(str)
assertChecksum(checksum, hashAlgoSync(payload))
return typedView(payload, format)
},
}
}
// eslint-disable-next-line @exodus/import/no-deprecated
const hash256sync = (x) => hashSync('sha256', hashSync('sha256', x, 'uint8'), 'uint8')
const hash256 = hash256sync // See note at the top
const {
encode: toBase58check,
decode: fromBase58check,
encodeSync: toBase58checkSync,
decodeSync: fromBase58checkSync,
} = makeBase58check(hash256, hash256sync)
export { toBase58check, fromBase58check, toBase58checkSync, fromBase58checkSync }