Skip to content

Commit 6c5abcb

Browse files
authored
Merge pull request #2 from near/base-encode-base-decode
baseEncode and baseDecode added to the library
2 parents fcb11b1 + 98741c6 commit 6c5abcb

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

borsh-ts/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
import BN from 'bn.js';
2+
import bs58 from 'bs58';
23

34
// TODO: Make sure this polyfill not included when not required
45
import * as encoding from 'text-encoding-utf-8';
56
const TextDecoder = (typeof (global as any).TextDecoder !== 'function') ? encoding.TextDecoder : (global as any).TextDecoder;
67
const textDecoder = new TextDecoder('utf-8', { fatal: true });
78

9+
export function baseEncode(value: Uint8Array | string): string {
10+
if (typeof(value) === 'string') {
11+
value = Buffer.from(value, 'utf8');
12+
}
13+
return bs58.encode(Buffer.from(value));
14+
}
15+
16+
export function baseDecode(value: string): Buffer {
17+
return Buffer.from(bs58.decode(value));
18+
}
19+
820
const INITIAL_LENGTH = 1024;
921

1022
export type Schema = Map<Function, any>;

borsh-ts/test/serialize.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,26 @@ test('serialize object', async () => {
1919
expect(newValue.y.toString()).toEqual('20');
2020
expect(newValue.z).toEqual('123');
2121
expect(newValue.q).toEqual(new Uint8Array([1, 2, 3]));
22+
});
23+
24+
test('baseEncode string test', async () => {
25+
const encodedValue = borsh.baseEncode("244ZQ9cgj3CQ6bWBdytfrJMuMQ1jdXLFGnr4HhvtCTnM");
26+
const expectedValue = "HKk9gqNj4xb4rLdJuzT5zzJbLa4vHBdYCxQT9H99csQh6nz3Hfpqn4jtWA92";
27+
expect(encodedValue).toEqual(expectedValue);
28+
});
29+
30+
test('baseEncode array test', async () => {
31+
expect(borsh.baseEncode([1, 2, 3, 4, 5])).toEqual('7bWpTW');
32+
});
33+
34+
test('baseDecode test', async () => {
35+
const value = "HKk9gqNj4xb4rLdJu";
36+
const expectedDecodedArray = [3, 96, 254, 84, 10, 240, 93, 199, 52, 244, 164, 240, 6];
37+
const expectedBuffer = Buffer.from(expectedDecodedArray);
38+
expect(borsh.baseDecode(value)).toEqual(expectedBuffer);
39+
});
40+
41+
test('base encode and decode test', async () => {
42+
const value = '244ZQ9cgj3CQ6bWBdytfrJMuMQ1jdXLFGnr4HhvtCTnM';
43+
expect(borsh.baseEncode(borsh.baseDecode(value))).toEqual(value);
2244
});

lib/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/// <reference types="node" />
22
import BN from 'bn.js';
3+
export declare function baseEncode(value: Uint8Array | string): string;
4+
export declare function baseDecode(value: string): Buffer;
35
export declare type Schema = Map<Function, any>;
46
export declare class BorshError extends Error {
57
originalMessage: string;

lib/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,24 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
2828
return (mod && mod.__esModule) ? mod : { "default": mod };
2929
};
3030
Object.defineProperty(exports, "__esModule", { value: true });
31-
exports.deserialize = exports.serialize = exports.BinaryReader = exports.BinaryWriter = exports.BorshError = void 0;
31+
exports.deserialize = exports.serialize = exports.BinaryReader = exports.BinaryWriter = exports.BorshError = exports.baseDecode = exports.baseEncode = void 0;
3232
const bn_js_1 = __importDefault(require("bn.js"));
33+
const bs58_1 = __importDefault(require("bs58"));
3334
// TODO: Make sure this polyfill not included when not required
3435
const encoding = __importStar(require("text-encoding-utf-8"));
3536
const TextDecoder = (typeof global.TextDecoder !== 'function') ? encoding.TextDecoder : global.TextDecoder;
3637
const textDecoder = new TextDecoder('utf-8', { fatal: true });
38+
function baseEncode(value) {
39+
if (typeof (value) === 'string') {
40+
value = Buffer.from(value, 'utf8');
41+
}
42+
return bs58_1.default.encode(Buffer.from(value));
43+
}
44+
exports.baseEncode = baseEncode;
45+
function baseDecode(value) {
46+
return Buffer.from(bs58_1.default.decode(value));
47+
}
48+
exports.baseDecode = baseDecode;
3749
const INITIAL_LENGTH = 1024;
3850
class BorshError extends Error {
3951
constructor(message) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"dependencies": {
4848
"@types/bn.js": "^4.11.5",
4949
"bn.js": "^5.0.0",
50+
"bs58": "^4.0.0",
5051
"text-encoding-utf-8": "^1.0.2"
5152
}
5253
}

0 commit comments

Comments
 (0)