Skip to content

Commit d8c150f

Browse files
committed
feat: toBase58xrp/fromBase58xrp
1 parent 68ec708 commit d8c150f

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ Same as `windows1252toString = createSinglebyteDecoder('windows-1252')`.
150150
##### `toBase58(arr)`
151151
##### `fromBase58(str, format = 'uint8')`
152152

153+
##### `toBase58xrp(arr)`
154+
##### `fromBase58xrp(str, format = 'uint8')`
155+
153156
### `@exodus/bytes/base58check.js`
154157

155158
##### `async toBase58check(arr)`

base58.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { assertUint8 } from './assert.js'
33
import { nativeDecoder, nativeEncoder, isHermes } from './fallback/_utils.js'
44
import { encodeAscii, decodeAscii } from './fallback/latin1.js'
55

6-
const alphabet = [...'123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz']
7-
const codes = new Uint8Array(alphabet.map((x) => x.charCodeAt(0)))
8-
const ZERO = alphabet[0]
9-
const zeroC = codes[0]
6+
const alphabet58 = [...'123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz']
7+
const alphabetXRP = [...'rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz']
8+
const codes58 = new Uint8Array(alphabet58.map((x) => x.charCodeAt(0)))
9+
const codesXRP = new Uint8Array(alphabetXRP.map((x) => x.charCodeAt(0)))
1010

1111
const _0n = BigInt(0)
1212
const _1n = BigInt(1)
@@ -22,11 +22,12 @@ const E_CHAR = 'Invalid character in base58 input'
2222

2323
const shouldUseBigIntFrom = isHermes // faster only on Hermes, numbers path beats it on normal engines
2424

25-
export function toBase58(arr) {
25+
function toBase58core(arr, alphabet, codes) {
2626
assertUint8(arr)
2727
const length = arr.length
2828
if (length === 0) return ''
2929

30+
const ZERO = alphabet[0]
3031
let zeros = 0
3132
while (zeros < length && arr[zeros] === 0) zeros++
3233

@@ -121,11 +122,12 @@ export function toBase58(arr) {
121122
}
122123

123124
// TODO: test on 'z'.repeat(from 1 to smth)
124-
export function fromBase58(str, format = 'uint8') {
125+
function fromBase58core(str, alphabet, codes, format = 'uint8') {
125126
if (typeof str !== 'string') throw new TypeError('Input is not a string')
126127
const length = str.length
127128
if (length === 0) return typedView(new Uint8Array(), format)
128129

130+
const zeroC = codes[0]
129131
let zeros = 0
130132
while (zeros < length && str.charCodeAt(zeros) === zeroC) zeros++
131133

@@ -210,3 +212,8 @@ export function fromBase58(str, format = 'uint8') {
210212

211213
return typedView(res.slice(at - zeros), format) // slice is faster for small sizes than subarray
212214
}
215+
216+
export const toBase58 = (arr) => toBase58core(arr, alphabet58, codes58)
217+
export const fromBase58 = (str, format) => fromBase58core(str, alphabet58, codes58, format)
218+
export const toBase58xrp = (arr) => toBase58core(arr, alphabetXRP, codesXRP)
219+
export const fromBase58xrp = (str, format) => fromBase58core(str, alphabetXRP, codesXRP, format)

0 commit comments

Comments
 (0)