parent
7b41bae315
commit
14546f58ea
@ -0,0 +1,23 @@ |
||||
{ |
||||
"name": "@harmony/contract", |
||||
"version": "0.0.1", |
||||
"description": "contract libraries for harmony", |
||||
"main": "dist/index.js", |
||||
"node": "dist/index.js", |
||||
"browser": "dist/index.js", |
||||
"module": "dist/index.esm.js", |
||||
"jsnext:main": "dist/index.esm.js", |
||||
"typings": "dist/index.d.ts", |
||||
"scripts": { |
||||
"test": "echo \"Error: no test specified\" && exit 1" |
||||
}, |
||||
"author": "neeboo@firestack.one", |
||||
"publishConfig": { |
||||
"access": "public" |
||||
}, |
||||
"license": "ISC", |
||||
"dependencies": { |
||||
"@harmony/crypto": "^0.0.1", |
||||
"@harmony/utils": "^0.0.1" |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1 @@ |
||||
export * from './abi/abiCoder'; |
@ -0,0 +1,8 @@ |
||||
{ |
||||
"extends": "../../tsconfig.base.json", |
||||
"compilerOptions": { |
||||
"outDir": "dist", |
||||
"rootDir": "src" |
||||
}, |
||||
"include": ["src", "../../typings/**/*.d.ts"] |
||||
} |
@ -0,0 +1,5 @@ |
||||
{ |
||||
"extends": "../../tsconfig.test.json", |
||||
"include": ["src", "test", "../../typings/**/*.d.ts"], |
||||
"references": [] |
||||
} |
@ -0,0 +1,39 @@ |
||||
import { bs58Decode, bs58Encode } from '../src/base58'; |
||||
import { fixtures, testData } from './base58fixture'; |
||||
|
||||
describe('test bs58', () => { |
||||
it('should test encode', () => { |
||||
fixtures.valid.forEach((f: any) => { |
||||
const actual = bs58Encode(f.hex); |
||||
expect(actual).toEqual(f.string); |
||||
}); |
||||
}); |
||||
it('should test decode', () => { |
||||
fixtures.valid.forEach((f: any) => { |
||||
const actual = bs58Decode(f.string); |
||||
expect(actual).toEqual(f.hex); |
||||
}); |
||||
}); |
||||
it('should throw error', () => { |
||||
fixtures.invalid.forEach((f: any) => { |
||||
try { |
||||
bs58Decode(f.string); |
||||
} catch (error) { |
||||
expect(error.message).toEqual('Non-base58 character'); |
||||
} |
||||
}); |
||||
}); |
||||
it('another test', () => { |
||||
testData.forEach((f: any) => { |
||||
const actual = bs58Encode(f.raw); |
||||
expect(actual).toEqual(f.res); |
||||
const actualDecode = bs58Decode(f.res); |
||||
expect(actualDecode).toEqual(f.raw); |
||||
}); |
||||
}); |
||||
it('manual test', () => { |
||||
const raw = '0x'; |
||||
const actual = bs58Encode(raw); |
||||
expect(actual).toEqual(''); |
||||
}); |
||||
}); |
@ -0,0 +1,140 @@ |
||||
export 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', |
||||
}, |
||||
], |
||||
}; |
||||
|
||||
export const testData = [ |
||||
{ |
||||
raw: '', |
||||
res: '', |
||||
}, |
||||
{ |
||||
raw: '61', |
||||
res: '2g', |
||||
}, |
||||
{ |
||||
raw: '626262', |
||||
res: 'a3gV', |
||||
}, |
||||
{ |
||||
raw: '636363', |
||||
res: 'aPEr', |
||||
}, |
||||
{ |
||||
raw: '73696d706c792061206c6f6e6720737472696e67', |
||||
res: '2cFupjhnEsSn59qHXstmK2ffpLv2', |
||||
}, |
||||
{ |
||||
raw: '00eb15231dfceb60925886b67d065299925915aeb172c06647', |
||||
res: '1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L', |
||||
}, |
||||
{ |
||||
raw: '516b6fcd0f', |
||||
res: 'ABnLTmg', |
||||
}, |
||||
{ |
||||
raw: '572e4794', |
||||
res: '3EFU7m', |
||||
}, |
||||
{ |
||||
raw: 'ecac89cad93923c02321', |
||||
res: 'EJDM8drfXA6uyA', |
||||
}, |
||||
{ |
||||
raw: '10c8511e', |
||||
res: 'Rt5zm', |
||||
}, |
||||
{ |
||||
raw: '00000000000000000000', |
||||
res: '1111111111', |
||||
}, |
||||
{ |
||||
raw: |
||||
'801184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd206ec97e', |
||||
res: '5Hx15HFGyep2CfPxsJKe2fXJsCVn5DEiyoeGGF6JZjGbTRnqfiD', |
||||
}, |
||||
{ |
||||
raw: '003c176e659bea0f29a3e9bf7880c112b1b31b4dc826268187', |
||||
|
||||
res: '16UjcYNBG9GTK4uq2f7yYEbuifqCzoLMGS', |
||||
}, |
||||
]; |
@ -0,0 +1,18 @@ |
||||
import base from 'base-x'; |
||||
|
||||
const BASE58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; |
||||
|
||||
const base58 = base(BASE58); |
||||
|
||||
export const bs58Encode = (hex: string): string => { |
||||
if (hex.startsWith('0x') || hex.startsWith('0X')) { |
||||
hex = hex.substring(2); |
||||
} |
||||
const hexBuff = Buffer.from(hex, 'hex'); |
||||
return base58.encode(hexBuff); |
||||
}; |
||||
|
||||
export const bs58Decode = (baseString: string): string => { |
||||
const baseBuf = base58.decode(baseString); |
||||
return baseBuf.toString('hex'); |
||||
}; |
Loading…
Reference in new issue