@@ -3,10 +3,10 @@ import { assertUint8 } from './assert.js'
33import { nativeDecoder , nativeEncoder , isHermes } from './fallback/_utils.js'
44import { 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
1111const _0n = BigInt ( 0 )
1212const _1n = BigInt ( 1 )
@@ -22,11 +22,12 @@ const E_CHAR = 'Invalid character in base58 input'
2222
2323const 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