ss58Format option for Keyring (#471)
* ss58Format option for Keyring * Expose setSS58Format everywhere * Remove setAddressPrefix * 1.4.0
This commit is contained in:
@@ -14,6 +14,12 @@ describe('encode', (): void => {
|
||||
).toEqual('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaQua');
|
||||
});
|
||||
|
||||
it('can re-encode an address', (): void => {
|
||||
expect(
|
||||
encode('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY', 68)
|
||||
).toEqual('7sL6eNJj5ZGV5cn3hhV2deRUsivXfBfMH76wCALCqWj1EKzv');
|
||||
});
|
||||
|
||||
it('fails when non-valid publicKey provided', (): void => {
|
||||
expect(
|
||||
(): string => encode(
|
||||
|
||||
@@ -7,13 +7,15 @@ import { Prefix } from './types';
|
||||
// Original implementation: https://github.com/paritytech/polka-ui/blob/4858c094684769080f5811f32b081dd7780b0880/src/polkadot.js#L34
|
||||
|
||||
import bs58 from 'bs58';
|
||||
import { assert, u8aConcat, u8aToBuffer, u8aToU8a } from '@polkadot/util';
|
||||
import { assert, u8aConcat, u8aToBuffer } from '@polkadot/util';
|
||||
|
||||
import decode from './decode';
|
||||
import defaults from './defaults';
|
||||
import sshash from './sshash';
|
||||
|
||||
export default function encode (_key: Uint8Array | string, prefix: Prefix = defaults.prefix): string {
|
||||
const key = u8aToU8a(_key);
|
||||
// decode it, this means we can re-encode an address
|
||||
const key = decode(_key);
|
||||
|
||||
assert(defaults.allowedDecodedLengths.includes(key.length), `Expected a valid key to convert, with length ${defaults.allowedDecodedLengths}`);
|
||||
|
||||
|
||||
@@ -5,11 +5,11 @@
|
||||
import checkAddress from './check';
|
||||
import decodeAddress from './decode';
|
||||
import encodeAddress from './encode';
|
||||
import setAddressPrefix from './setPrefix';
|
||||
import setSS58Format from './setSS58Format';
|
||||
|
||||
export {
|
||||
checkAddress,
|
||||
decodeAddress,
|
||||
encodeAddress,
|
||||
setAddressPrefix
|
||||
setSS58Format
|
||||
};
|
||||
|
||||
+3
-3
@@ -3,11 +3,11 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import encode from './encode';
|
||||
import setPrefix from './setPrefix';
|
||||
import setSS58Format from './setSS58Format';
|
||||
|
||||
describe('setPrefix', (): void => {
|
||||
describe('setSS58Format', (): void => {
|
||||
beforeEach((): void => {
|
||||
setPrefix(68);
|
||||
setSS58Format(68);
|
||||
});
|
||||
|
||||
it('sets and allows encoding using', (): void => {
|
||||
+1
-1
@@ -6,6 +6,6 @@ import { Prefix } from './types';
|
||||
|
||||
import defaults from './defaults';
|
||||
|
||||
export default function setPrefix (prefix: Prefix): void {
|
||||
export default function setSS58Format (prefix: Prefix): void {
|
||||
defaults.prefix = prefix;
|
||||
}
|
||||
Reference in New Issue
Block a user