Skip to content

Commit

Permalink
Update ethereum package (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
sz-piotr authored Aug 8, 2019
1 parent 3e5d5f9 commit c4eb986
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 34 deletions.
16 changes: 8 additions & 8 deletions ethereum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ yarn add @restless/ethereum
Accepts any string that represents a valid ethereum address. Checks the checksum if present. Returns a normalized representation.

```javascript
asEthAddress('0xA5fE...f213') // RIGHT 0xA5fE...f213
asEthAddress('0xa5fe...f213') // RIGHT 0xA5fE...f213
asEthAddress('bla bla bla') // LEFT 'expected: ethereum address'
asBigNumber(123) // LEFT 'expected: ethereum address'
asEthAddress('0xA5fE...f213', 'path') // Result.ok(0xA5fE...f213)
asEthAddress('0xa5fe...f213', 'path') // Result.ok(0xA5fE...f213)
asEthAddress('bla bla bla', 'path') // Result.error([{expected: 'ethereum address', path: 'path'}])
asEthAddress(123, 'path') // Result.error([{expected: 'ethereum address', path: 'path'}])
```

### `asBigNumber`

Accepts any string or number that represents an integer. Converts the integer to a [BigNumber](https://docs.ethers.io/ethers.js/html/api-utils.html#big-numbers).

```javascript
asBigNumber('123') // RIGHT BigNumber(123)
asBigNumber(456) // RIGHT BigNumber(456)
asBigNumber(1.5) // LEFT 'expected: big number'
asBigNumber(true) // LEFT 'expected: big number'
asBigNumber('123', 'path') // Result.ok(BigNumber(123))
asBigNumber(456, 'path') // Result.ok(BigNumber(456))
asBigNumber(1.5, 'path') // Result.error([{expected: 'big number', path: 'path'}])
asBigNumber(true, 'path') // Result.error([{expected: 'big number', path: 'path'}])
```
6 changes: 3 additions & 3 deletions ethereum/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@restless/ethereum",
"version": "0.1.1",
"version": "0.2.0",
"author": "Piotr Szlachciak <[email protected]>",
"description": "Ethereum utilities for restless",
"license": "Unlicense",
Expand Down Expand Up @@ -31,8 +31,8 @@
"lint": "tslint --project tsconfig.json \"src/**/*.ts\" \"test/**/*.ts\""
},
"dependencies": {
"ethers": "^4.0.0",
"@restless/sanitizers": "^0.1.0"
"@restless/sanitizers": "^0.2.0",
"ethers": "^4.0.0"
},
"devDependencies": {
"@types/chai": "^4.1.7",
Expand Down
6 changes: 3 additions & 3 deletions ethereum/src/asBigNumber.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Either, Sanitizer } from '@restless/sanitizers'
import { Result, Sanitizer } from '@restless/sanitizers'
import { utils } from 'ethers'

export const asBigNumber: Sanitizer<utils.BigNumber> = (value, path) => {
try {
if (typeof value === 'string' || typeof value === 'number') {
return Either.right(utils.bigNumberify(value))
return Result.ok(utils.bigNumberify(value))
}
} catch {} // tslint:disable-line
return Either.left([{ path, expected: 'big number' }])
return Result.error([{ path, expected: 'big number' }])
}
6 changes: 3 additions & 3 deletions ethereum/src/asEthAddress.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Either, Sanitizer } from '@restless/sanitizers'
import { Result, Sanitizer } from '@restless/sanitizers'
import { utils } from 'ethers'

export const asEthAddress: Sanitizer<string> = (value, path) => {
try {
if (typeof value === 'string') {
return Either.right(utils.getAddress(value))
return Result.ok(utils.getAddress(value))
}
} catch {} // tslint:disable-line
return Either.left([{ path, expected: 'ethereum address' }])
return Result.error([{ path, expected: 'ethereum address' }])
}
14 changes: 7 additions & 7 deletions ethereum/test/asBigNumber.test.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
import { Either } from '@restless/sanitizers'
import { Result } from '@restless/sanitizers'
import { expect } from 'chai'
import { utils } from 'ethers'
import { asBigNumber } from '../src/asBigNumber'

describe('asBigNumber', () => {
it('sanitizes strings that are valid big numbers', async () => {
const result = asBigNumber('1234', '')
expect(result).to.deep.equal(Either.right(utils.bigNumberify('1234')))
expect(result).to.deep.equal(Result.ok(utils.bigNumberify('1234')))
})

it('sanitizes hex strings that are valid big numbers', async () => {
const result = asBigNumber('0xaBf3', '')
expect(result).to.deep.equal(Either.right(utils.bigNumberify('0xaBf3')))
expect(result).to.deep.equal(Result.ok(utils.bigNumberify('0xaBf3')))
})

it('sanitizes numbers', async () => {
const result = asBigNumber(1234, '')
expect(result).to.deep.equal(Either.right(utils.bigNumberify(1234)))
expect(result).to.deep.equal(Result.ok(utils.bigNumberify(1234)))
})

it('does not accept non integers', async () => {
const result = asBigNumber(1.234567, 'path')
expect(result).to.deep.equal(
Either.left([{ path: 'path', expected: 'big number' }])
Result.error([{ path: 'path', expected: 'big number' }])
)
})

it('does not accept non big number strings', async () => {
const result = asBigNumber('bla bla bla', 'path')
expect(result).to.deep.equal(
Either.left([{ path: 'path', expected: 'big number' }])
Result.error([{ path: 'path', expected: 'big number' }])
)
})

it('does not accept types other than string or number', async () => {
const result = asBigNumber(true, 'path')
expect(result).to.deep.equal(
Either.left([{ path: 'path', expected: 'big number' }])
Result.error([{ path: 'path', expected: 'big number' }])
)
})
})
12 changes: 6 additions & 6 deletions ethereum/test/asEthAddress.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Either } from '@restless/sanitizers'
import { Result } from '@restless/sanitizers'
import { expect } from 'chai'
import { Wallet } from 'ethers'
import { asEthAddress } from '../src/asEthAddress'
Expand All @@ -7,35 +7,35 @@ describe('asEthAddress', () => {
it('sanitizes strings that are valid ethereum addresses', async () => {
const { address } = Wallet.createRandom()
const result = asEthAddress(address, '')
expect(result).to.deep.equal(Either.right(address))
expect(result).to.deep.equal(Result.ok(address))
})

it('keeps the string normalized', async () => {
const { address } = Wallet.createRandom()
const result = asEthAddress(address.toLowerCase(), '')
expect(result).to.deep.equal(Either.right(address))
expect(result).to.deep.equal(Result.ok(address))
})

it('does not accept invalid ethereum addresses', async () => {
const INVALID_ADDRESS = '0x7a4EdC939038dbB58befD7028b1625DFf2ca58b8'
const result = asEthAddress(INVALID_ADDRESS, 'path')
expect(result).to.deep.equal(
Either.left([{ path: 'path', expected: 'ethereum address' }])
Result.error([{ path: 'path', expected: 'ethereum address' }])
)
})

it('does not accept non address strings', async () => {
const NOT_AN_ADDRESS = 'bla bla bla'
const result = asEthAddress(NOT_AN_ADDRESS, 'path')
expect(result).to.deep.equal(
Either.left([{ path: 'path', expected: 'ethereum address' }])
Result.error([{ path: 'path', expected: 'ethereum address' }])
)
})

it('does not accept non strings', async () => {
const result = asEthAddress(123, 'path')
expect(result).to.deep.equal(
Either.left([{ path: 'path', expected: 'ethereum address' }])
Result.error([{ path: 'path', expected: 'ethereum address' }])
)
})
})
8 changes: 4 additions & 4 deletions ethereum/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
esutils "^2.0.2"
js-tokens "^4.0.0"

"@restless/sanitizers@^0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@restless/sanitizers/-/sanitizers-0.1.0.tgz#7b1af5bbb15cabb3975b33a3539f04f295637f20"
integrity sha512-aMxomLCELnv115RMW4Xksn5By6WmVdqQK0GdZDa09sUdsHpoVP8/lyAtcAZY9amP91rEtkL7oksp3hrQ0pNftg==
"@restless/sanitizers@^0.2.0":
version "0.2.0"
resolved "https://registry.yarnpkg.com/@restless/sanitizers/-/sanitizers-0.2.0.tgz#f66ba872f8215a869fcf1290f5547c06b5a7eef5"
integrity sha512-b11WcVDC+xFn8p5uKhNwKv5La+L9+I4kjQDIyiWC4HIgwhcTtP5RT/7UJPjSpV+9k0Dr6BtVUnwEueS5kI3pZQ==

"@types/chai@^4.1.7":
version "4.1.7"
Expand Down

0 comments on commit c4eb986

Please sign in to comment.