@quentinadam/base58check 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -0
- package/dist/base58check.d.ts +26 -0
- package/dist/base58check.js +33 -0
- package/package.json +23 -0
package/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# @quentinadam/base58check
|
|
2
|
+
|
|
3
|
+
[![JSR][jsr-image]][jsr-url] [![NPM][npm-image]][npm-url] [![CI][ci-image]][ci-url]
|
|
4
|
+
|
|
5
|
+
A simple library to encode and decode base58check strings.
|
|
6
|
+
|
|
7
|
+
Optionnaly supports specifying the alphabet to use.
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import * as base58check from '@quentinadam/base58check';
|
|
13
|
+
|
|
14
|
+
base58check.encode(new Uint8Array([72, 101, 108, 108, 111])); // returns 'vSxRbq6XzDhP'
|
|
15
|
+
|
|
16
|
+
base58check.decode('vSxRbq6XzDhP'); // returns Uint8Array([72, 101, 108, 108, 111])
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
[ci-image]: https://img.shields.io/github/actions/workflow/status/quentinadam/deno-base58check/ci.yml?branch=main&logo=github&style=flat-square
|
|
20
|
+
[ci-url]: https://github.com/quentinadam/deno-base58check/actions/workflows/ci.yml
|
|
21
|
+
[npm-image]: https://img.shields.io/npm/v/@quentinadam/base58check.svg?style=flat-square
|
|
22
|
+
[npm-url]: https://npmjs.org/package/@quentinadam/base58check
|
|
23
|
+
[jsr-image]: https://jsr.io/badges/@quentinadam/base58check?style=flat-square
|
|
24
|
+
[jsr-url]: https://jsr.io/@quentinadam/base58check
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** Options for {@linkcode encode} and {@linkcode decode}. */
|
|
2
|
+
export interface Options {
|
|
3
|
+
/**
|
|
4
|
+
* The alphabet to use.
|
|
5
|
+
* The alphabet must be a string of 58 unique characters.
|
|
6
|
+
*
|
|
7
|
+
* @default {'123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'}
|
|
8
|
+
*/
|
|
9
|
+
alphabet?: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Encodes a Uint8Array buffer into a base58check string.
|
|
13
|
+
*
|
|
14
|
+
* @param buffer The buffer to encode.
|
|
15
|
+
* @param options The options to use for encoding.
|
|
16
|
+
* @returns The base58check encoded string.
|
|
17
|
+
*/
|
|
18
|
+
export declare function encode(buffer: Uint8Array, options?: Options): string;
|
|
19
|
+
/**
|
|
20
|
+
* Decodes a base58check encoded string into a Uint8Array buffer.
|
|
21
|
+
*
|
|
22
|
+
* @param string The base58check encoded string.
|
|
23
|
+
* @param options The options to use for decoding.
|
|
24
|
+
* @returns The decoded buffer.
|
|
25
|
+
*/
|
|
26
|
+
export declare function decode(string: string, options?: Options): Uint8Array;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import assert from '@quentinadam/assert';
|
|
2
|
+
import * as base58 from '@quentinadam/base58';
|
|
3
|
+
import sha256 from '@quentinadam/hash/sha256';
|
|
4
|
+
import * as Uint8ArrayExtension from '@quentinadam/uint8array-extension';
|
|
5
|
+
function computeChecksum(buffer) {
|
|
6
|
+
return sha256(sha256(buffer)).slice(0, 4);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Encodes a Uint8Array buffer into a base58check string.
|
|
10
|
+
*
|
|
11
|
+
* @param buffer The buffer to encode.
|
|
12
|
+
* @param options The options to use for encoding.
|
|
13
|
+
* @returns The base58check encoded string.
|
|
14
|
+
*/
|
|
15
|
+
export function encode(buffer, options) {
|
|
16
|
+
return base58.encode(Uint8ArrayExtension.concat([buffer, computeChecksum(buffer)]), options);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Decodes a base58check encoded string into a Uint8Array buffer.
|
|
20
|
+
*
|
|
21
|
+
* @param string The base58check encoded string.
|
|
22
|
+
* @param options The options to use for decoding.
|
|
23
|
+
* @returns The decoded buffer.
|
|
24
|
+
*/
|
|
25
|
+
export function decode(string, options) {
|
|
26
|
+
const { buffer, checkSum } = (() => {
|
|
27
|
+
const buffer = base58.decode(string, options);
|
|
28
|
+
assert(buffer.length >= 4);
|
|
29
|
+
return { buffer: buffer.slice(0, -4), checkSum: buffer.slice(-4) };
|
|
30
|
+
})();
|
|
31
|
+
assert(Uint8ArrayExtension.equals(computeChecksum(buffer), checkSum));
|
|
32
|
+
return buffer;
|
|
33
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@quentinadam/base58check",
|
|
3
|
+
"version": "0.1.3",
|
|
4
|
+
"description": "A simple library to encode and decode base58check strings",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Quentin Adam",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/quentinadam/deno-base58check.git"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": "./dist/base58check.js",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"README.md"
|
|
16
|
+
],
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@quentinadam/assert": "^0.1.10",
|
|
19
|
+
"@quentinadam/base58": "^0.1.5",
|
|
20
|
+
"@quentinadam/hash": "^0.1.1",
|
|
21
|
+
"@quentinadam/uint8array-extension": "^0.1.5"
|
|
22
|
+
}
|
|
23
|
+
}
|