forked from OKEAMAH/js-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoding.ts
More file actions
47 lines (36 loc) · 1.45 KB
/
encoding.ts
File metadata and controls
47 lines (36 loc) · 1.45 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
import { base58FromBytes, base58ToBytes as b58ToBytes, Hex } from '@iden3/js-crypto';
import { base64url, base64 } from 'rfc4648';
export const byteEncoder = new TextEncoder();
export const byteDecoder = new TextDecoder();
export function bytesToBase64url(b: Uint8Array, opts = { pad: false }): string {
return base64url.stringify(b, opts);
}
export function base64ToBytes(s: string, opts = { loose: true }): Uint8Array {
return base64.parse(s, opts);
}
export function bytesToBase64(b: Uint8Array, opts = { pad: false }): string {
return base64.stringify(b, opts);
}
export function base64UrlToBytes(s: string, opts = { loose: true }): Uint8Array {
const inputBase64Url = s.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
return base64url.parse(inputBase64Url, opts);
}
export function base58ToBytes(s: string): Uint8Array {
return b58ToBytes(s);
}
export function bytesToBase58(b: Uint8Array): string {
return base58FromBytes(b);
}
export function hexToBytes(s: string): Uint8Array {
const input = s.startsWith('0x') ? s.substring(2) : s;
return Hex.decodeString(input.toLowerCase());
}
export function encodeBase64url(s: string, opts = { pad: false }): string {
return base64url.stringify(byteEncoder.encode(s), opts);
}
export function decodeBase64url(s: string, opts = { loose: true }): string {
return byteDecoder.decode(base64url.parse(s, opts));
}
export function bytesToHex(b: Uint8Array): string {
return Hex.encodeString(b);
}