Skip to content

Commit

Permalink
refactor(encoder)!: remove required prefix param
Browse files Browse the repository at this point in the history
BREAKING CHANGE: since the prefix is evaluated by the type
itself the required prefix parameter is no more required.

rewrite
```js
decode('cb_DA6sWJo=', 'cb')
```
to
```js
decode('cb_DA6sWJo=')
```
  • Loading branch information
subhod-i committed May 17, 2022
1 parent c20b373 commit 50228ec
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/account/multiple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class _AccountMultiple extends _AccountResolver {
_resolveAccount (account: Account | EncodedData<'ak'> = this.selectedAddress): _AccountBase {
if (typeof account === 'string') {
const address = account as EncodedData<'ak'>
decode(address, 'ak')
decode(address)
if (this.accounts[address] == null) throw new UnavailableAccountError(account)
account = this.accounts[address]
}
Expand Down Expand Up @@ -109,7 +109,7 @@ class _AccountMultiple extends _AccountResolver {
* @example selectAccount('ak_xxxxxxxx')
*/
selectAccount (address: EncodedData<'ak'>): void {
decode(address, 'ak')
decode(address)
if (this.accounts[address] == null) throw new UnavailableAccountError(address)
this.selectedAddress = address
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function getAddressFromPriv (secret: string | Uint8Array): string {
*/
export function isAddressValid (address: string, prefix: EncodingType = 'ak'): boolean {
try {
decode(address as EncodedData<typeof prefix>, prefix)
decode(address as EncodedData<typeof prefix>)
return true
} catch (e) {
return false
Expand Down Expand Up @@ -102,7 +102,7 @@ export function hash (input: Data): Buffer {
* @return {String} - Contract address
*/
export function encodeContractAddress (owner: EncodedData<'ak'>, nonce: number): string {
const publicKey = decode(owner, 'ak')
const publicKey = decode(owner)
const binary = Buffer.concat([publicKey, encodeUnsigned(nonce)])
return encode(hash(binary), 'ct')
}
Expand Down
13 changes: 5 additions & 8 deletions src/utils/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import {
DecodeError,
ArgumentError,
InvalidChecksumError,
PayloadLengthError,
PrefixMismatchError
PayloadLengthError
} from './errors'

/**
Expand All @@ -19,8 +18,9 @@ export function sha256hash (input: Uint8Array | string): Buffer {
}

// based on https://github.com/aeternity/protocol/blob/master/node/api/api_encoding.md
const base64Types = ['ba', 'cb', 'or', 'ov', 'pi', 'ss', 'cs', 'ck', 'cv', 'st', 'tx']
const base58Types = ['ak', 'bf', 'bs', 'bx', 'ch', 'cm', 'ct', 'kh', 'mh', 'nm', 'ok', 'oq', 'pp', 'sg', 'th']
const base64Types = ['ba', 'cb', 'or', 'ov', 'pi', 'ss', 'cs', 'ck', 'cv', 'st', 'tx'] as const
const base58Types = ['ak', 'bf', 'bs', 'bx', 'ch', 'cm', 'ct', 'kh', 'mh', 'nm', 'ok', 'oq', 'pp', 'sg', 'th'] as const

export type EncodingType = typeof base64Types[number] | typeof base58Types[number]
export type EncodedData<Type extends EncodingType> = `${Type}_${string}`
// TODO: add all types with a fixed length
Expand Down Expand Up @@ -76,11 +76,8 @@ const parseType = (maybeType: unknown): [EncodingType, typeof base64] => {
* @param {string} [requiredPrefix] Ensure that data have this prefix
* @return {Buffer} Decoded data
*/
export function decode (data: EncodedData<EncodingType>, requiredPrefix?: EncodingType): Buffer {
export function decode (data: EncodedData<EncodingType>): Buffer {
const [prefix, encodedPayload, extra] = data.split('_')
if (requiredPrefix != null && requiredPrefix !== prefix) {
throw new PrefixMismatchError(prefix, requiredPrefix)
}
if (encodedPayload == null) throw new DecodeError(`Encoded string missing payload: ${data}`)
if (extra != null) throw new DecodeError(`Encoded string have extra parts: ${data}`)
const [type, { decode }] = parseType(prefix)
Expand Down
4 changes: 2 additions & 2 deletions test/unit/hd-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
getSaveHDWalletAccounts,
DerivationError
} from '../../src/utils/hd-wallet'
import { encode, decode } from '../../src/utils/encoder'
import { encode, decode, EncodedData } from '../../src/utils/encoder'

describe('hd wallet', () => {
const testMnemonicSeed = Buffer.from('Git7bFJkmfC1Ho+6YFSFuxSzmDZydmjzk8FubrPDz4PmrkORlBDlfnPTk02Wq9Pj2ZdQ5cTA0SxHKGrq3xSjOw==', 'base64')
Expand Down Expand Up @@ -174,7 +174,7 @@ describe('hd wallet', () => {

it('get HdWalletAccount from seed', () => {
const wallet = getHdWalletAccountFromSeed(testMnemonicSeed, 0)
decode(wallet.publicKey, 'ak')
decode(wallet.publicKey as EncodedData)
})

it('Derive child with invalid path', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/memory-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { describe, it } from 'mocha'
import { expect } from 'chai'
import MemoryAccount from '../../src/account/memory'
import { generateKeyPair } from '../../src/utils/crypto'
import { InvalidKeypairError, PrefixMismatchError } from '../../src/utils/errors'
import { InvalidKeypairError, DecodeError } from '../../src/utils/errors'

const testAcc = generateKeyPair()

Expand All @@ -33,7 +33,7 @@ describe('MemoryAccount', function () {

it('Fail on invalid publicKey', async () => {
expect(() => MemoryAccount({ keypair: { publicKey: ' ', secretKey: testAcc.secretKey } }))
.to.throw(PrefixMismatchError, 'Encoded string have a wrong type: (expected: ak)')
.to.throw(DecodeError, 'Encoded string missing payload')
})

it('Fail on invalid publicKey', async () => {
Expand Down
3 changes: 0 additions & 3 deletions test/unit/tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ describe('Tx', function () {
it('throws if invalid checksum', () => expect(() => decode('ak_23aaaaa'))
.to.throw('Invalid checksum'))

it('throws if not matching type', () => expect(() => decode('cb_DA6sWJo=', 'ak'))
.to.throw('Encoded string have a wrong type: cb (expected: ak)'))

it('throws if invalid size', () => expect(() => decode('ak_An6Ui6sE1F'))
.to.throw('Payload should be 32 bytes, got 4 instead'))
})
Expand Down

0 comments on commit 50228ec

Please sign in to comment.