Skip to content

Commit

Permalink
refactor!: remove unused functions
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Jan 28, 2022
1 parent 15950ea commit 8c00de5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 59 deletions.
10 changes: 1 addition & 9 deletions src/utils/bignumber.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
/**
* Big Number Helpers
* @module @aeternity/aepp-sdk/es/utils/bignumber
* @example import { parseBigNumber, isBigNumber, ceil } from '@aeternity/aepp-sdk/es/utils/bignumber'
* @example import { isBigNumber, ceil } from '@aeternity/aepp-sdk/es/utils/bignumber'
*/
import BigNumber from 'bignumber.js'

/**
* Convert number to string
* @param {String|Number|BigNumber} number number to convert
* @return {String}
*/
export const parseBigNumber = (number: string | number | BigNumber): string =>
new BigNumber(number.toString()).toString(10)

/**
* Check if value is BigNumber, Number or number string representation
* @param {String|Number|BigNumber} number number to convert
Expand Down
25 changes: 1 addition & 24 deletions src/utils/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import nacl from 'tweetnacl'
import aesjs from 'aes-js'
import shajs from 'sha.js'

import { str2buf, toBytes } from './bytes'
import { str2buf } from './bytes'
import { decode } from '../tx/builder/helpers'
import { hash } from './crypto-ts'
import { InvalidChecksumError, MessageLimitError } from './errors'
Expand Down Expand Up @@ -63,16 +63,6 @@ export function isAddressValid (address, prefix = 'ak') {
}
}

/**
* Parse decimal address and return base58Check encoded address with prefix 'ak'
* @rtype (input: String) => address: String
* @param {String} decimalAddress - Address
* @return {String} address
*/
export function addressFromDecimal (decimalAddress) {
return aeEncodeKey(toBytes(decimalAddress, true))
}

/**
* Calculate SHA256 hash of `input`
* @rtype (input: String) => hash: String
Expand Down Expand Up @@ -284,19 +274,6 @@ export function verifyMessage (str, signature, publicKey) {
return verify(messageToHash(str), signature, publicKey)
}

/**
* æternity readable public keys are the base58-encoded public key, prepended
* with 'ak_'
* @rtype (binaryKey: Buffer) => String
* @param {Buffer} binaryKey - Key to encode
* @return {String} Encoded key
*/
export function aeEncodeKey (binaryKey) {
const publicKeyBuffer = Buffer.from(binaryKey, 'hex')
const pubKeyAddress = encodeBase58Check(publicKeyBuffer)
return `ak_${pubKeyAddress}`
}

/**
* Check key pair for validity
*
Expand Down
42 changes: 21 additions & 21 deletions test/unit/amount-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,31 @@ import { expect } from 'chai'
import BigNumber from 'bignumber.js'
import '..'
import { AE_AMOUNT_FORMATS, formatAmount, toAe, toAettos } from '../../src/utils/amount-formatter'
import { parseBigNumber } from '../../src/utils/bignumber'
import { InvalidDenominationError, IllegalArgumentError } from '../../src/utils/errors'

describe('Amount Formatter', function () {
it('to aettos', async () => {
it('to aettos', () => {
[
[1, AE_AMOUNT_FORMATS.AE, 1e18],
[10, AE_AMOUNT_FORMATS.AE, 10e18],
[100, AE_AMOUNT_FORMATS.AE, 100e18],
[10012312, AE_AMOUNT_FORMATS.AE, 10012312e18],
[1, AE_AMOUNT_FORMATS.AETTOS, 1]
].forEach(
([v, d, e]) => expect(parseBigNumber(e)).to.be.equal(toAettos(v, { denomination: d.toString() }))
)
[1, AE_AMOUNT_FORMATS.AE, '1000000000000000000'],
[11, AE_AMOUNT_FORMATS.AE, '11000000000000000000'],
[111, AE_AMOUNT_FORMATS.AE, '111000000000000000000'],
[10012312, AE_AMOUNT_FORMATS.AE, '10012312000000000000000000'],
[1, AE_AMOUNT_FORMATS.AETTOS, '1']
].forEach(([v, denomination, e]: [number, string, string]) =>
expect(toAettos(v, { denomination })).to.be.equal(e))
})

it('to Ae', () => {
[
[1, AE_AMOUNT_FORMATS.AETTOS, new BigNumber(1).div(1e18)],
[10, AE_AMOUNT_FORMATS.AETTOS, new BigNumber(10).div(1e18)],
[100, AE_AMOUNT_FORMATS.AETTOS, new BigNumber(100).div(1e18)],
[10012312, AE_AMOUNT_FORMATS.AETTOS, new BigNumber(10012312).div(1e18)],
[1, AE_AMOUNT_FORMATS.AE, 1]
].forEach(
([v, d, e]) => expect(parseBigNumber(e)).to.be.equal(toAe(v, { denomination: d.toString() }))
)
].forEach(([v, denomination, e]: [number, string, BigNumber]) =>
expect(toAe(v, { denomination })).to.be.equal(e.toString(10)))
})

it('format', () => {
[
[1, AE_AMOUNT_FORMATS.AE, AE_AMOUNT_FORMATS.AETTOS, new BigNumber(1e18)],
Expand All @@ -67,17 +66,18 @@ describe('Amount Formatter', function () {
[1, AE_AMOUNT_FORMATS.NANO_AE, AE_AMOUNT_FORMATS.AETTOS, new BigNumber(1000000000)],
[1, AE_AMOUNT_FORMATS.FEMTO_AE, AE_AMOUNT_FORMATS.AETTOS, new BigNumber(1000)],
[1, AE_AMOUNT_FORMATS.NANO_AE, AE_AMOUNT_FORMATS.FEMTO_AE, new BigNumber(1000000)]
].forEach(
([v, dF, dT, e]) => expect(parseBigNumber(e)).to.be.equal(formatAmount(v, { denomination: dF.toString(), targetDenomination: dT.toString() }))
)
].forEach(([v, denomination, targetDenomination, e]: [number, string, string, BigNumber]) =>
expect(formatAmount(v, { denomination, targetDenomination })).to.be.equal(e.toString(10)))
})

it('Invalid value', () => {
[
[true, [AE_AMOUNT_FORMATS.AE, AE_AMOUNT_FORMATS.AE], 'Value true is not type of number', IllegalArgumentError],
[1, [AE_AMOUNT_FORMATS.AE, 'ASD'], 'Invalid target denomination: ASD', InvalidDenominationError],
[1, ['ASD', AE_AMOUNT_FORMATS.AE], 'Invalid denomination: ASD', InvalidDenominationError]
].forEach(([v, [dF, dT], error]: any[]) => {
expect(() => formatAmount(v, { denomination: dF, targetDenomination: dT })).to.throw(error)
[true, AE_AMOUNT_FORMATS.AE, AE_AMOUNT_FORMATS.AE, IllegalArgumentError, 'Value true is not type of number'],
[1, AE_AMOUNT_FORMATS.AE, 'ASD', InvalidDenominationError, 'Invalid target denomination: ASD'],
[1, 'ASD', AE_AMOUNT_FORMATS.AE, InvalidDenominationError, 'Invalid denomination: ASD']
].forEach(([v, dF, dT, error, msg]: any[]) => {
expect(() => formatAmount(v, { denomination: dF, targetDenomination: dT }))
.to.throw(error, msg)
})
})
})
5 changes: 0 additions & 5 deletions test/unit/tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {
} from '../../src/tx/builder/helpers'
import BigNumber from 'bignumber.js'
import { toBytes } from '../../src/utils/bytes'
import { parseBigNumber } from '../../src/utils/bignumber'
import { buildTx, unpackTx } from '../../src/tx/builder'
import { NAME_BID_RANGES } from '../../src/tx/builder/schema'
import {
Expand All @@ -49,10 +48,6 @@ describe('Tx', function () {
return hash.should.be.equal(await commitmentHash('foobar.chain', _salt))
})

it('Parse big number', async () => {
parseBigNumber('123123123123').should.be.a('string')
})

it('test from big number to bytes', async () => {
// TODO investigate about float numbers serialization
const data = [
Expand Down

0 comments on commit 8c00de5

Please sign in to comment.