Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Own Base58 #1391

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/warm-insects-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@near-js/utils": patch
---

use own base58 and depend directly on base-x
2 changes: 1 addition & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"license": "ISC",
"dependencies": {
"@near-js/types": "workspace:*",
"bs58": "4.0.0",
"base-x": "5.0.0",
"depd": "2.0.0",
"mustache": "4.0.0"
},
Expand Down
14 changes: 14 additions & 0 deletions packages/utils/src/b58.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* This code is copied verbatim from
* https://github.com/cryptocoinjs/bs58/blob/master/ts_src/index.ts
* because
* 1. Its very minimal middleware.
* 2. The latest version (v6.0.0) has an incorrect build.
* https://www.npmjs.com/package/bs58/v/6.0.0
* 3. It doesn't doesn't actively update the primary dependency:
* https://www.npmjs.com/package/base-x
*/
import basex from "base-x";
const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';

export default basex(ALPHABET);
3 changes: 1 addition & 2 deletions packages/utils/src/format.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import bs58 from "bs58";

import bs58 from "./b58";
/**
* Exponent for calculating how many indivisible units are there in one NEAR. See {@link NEAR_NOMINATION}.
*/
Expand Down
72 changes: 72 additions & 0 deletions packages/utils/test/base58.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { describe, test, expect } from '@jest/globals';
import base58 from '../src/b58';

const fixtures = {
valid: [
{ hex: '', string: '' },
{ hex: '61', string: '2g' },
{ hex: '626262', string: 'a3gV' },
{ hex: '636363', string: 'aPEr' },
{
hex: '73696d706c792061206c6f6e6720737472696e67',
string: '2cFupjhnEsSn59qHXstmK2ffpLv2',
},
{
hex: '00eb15231dfceb60925886b67d065299925915aeb172c06647',
string: '1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L',
},
{ hex: '516b6fcd0f', string: 'ABnLTmg' },
{ hex: 'bf4f89001e670274dd', string: '3SEo3LWLoPntC' },
{ hex: '572e4794', string: '3EFU7m' },
{ hex: 'ecac89cad93923c02321', string: 'EJDM8drfXA6uyA' },
{ hex: '10c8511e', string: 'Rt5zm' },
{ hex: '00000000000000000000', string: '1111111111' },
{
hex: '801184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd206ec97e',
string: '5Hx15HFGyep2CfPxsJKe2fXJsCVn5DEiyoeGGF6JZjGbTRnqfiD',
},
{
hex: '003c176e659bea0f29a3e9bf7880c112b1b31b4dc826268187',
string: '16UjcYNBG9GTK4uq2f7yYEbuifqCzoLMGS',
},
],
invalid: [
{ description: 'non-base58 string', string: 'invalid' },
{ description: 'non-base58 alphabet', string: 'c2F0b3NoaQo=' },
{ description: 'leading whitespace', string: ' 1111111111' },
{ description: 'trailing whitespace', string: '1111111111 ' },
{
description: 'unexpected character after whitespace',
string: ' \t\n\u000b\f\r skip \r\f\u000b\n\t a',
},
],
};

const { encode, decode } = base58;
const { valid, invalid } = fixtures;

describe('base58', () => {
describe('encode', () => {
valid.forEach((f) => {
test(`can encode ${f.hex}`, () => {
const actual = encode(Buffer.from(f.hex, 'hex'));
expect(actual).toBe(f.string);
});
});
});

describe('decode', () => {
valid.forEach((f) => {
test(`can decode ${f.string}`, () => {
const actual = Buffer.from(decode(f.string)).toString('hex');
expect(actual).toBe(f.hex);
});
});

invalid.forEach((f) => {
test(`throws on ${f.description}`, () => {
expect(() => decode(f.string)).toThrow('Non-base58 character');
});
});
});
});
11 changes: 8 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.