Skip to content

Commit

Permalink
feat(utils): add address and address payload validators
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith-CY committed Nov 11, 2019
1 parent 65a1175 commit d44effb
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ module.exports = {
}],
"spaced-comment": ["error", "always", {
"markers": ["/"]
}]
}],
"max-classes-per-file": [0]
},
"globals": {
"BigInt": "readonly"
Expand Down
10 changes: 10 additions & 0 deletions packages/ckb-sdk-utils/__tests__/validators/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,15 @@
"hexStringStartsWithout0x": "123",
"bigint": "123",
"number": 123
},
"assertToBeSingleSigAddress": {
"singleSigAddress": "ckt1qyqd39fkyvnrs7sa882eu8pn6t3cyuemtpkqm6zejn",
"addressWithInvalidSingleSigPayload": "ckt1qyq6jh3kt48y294mla4mxrdy380tzprcpe7sxuzm0e",
"addressWithIncorrectSize": "ckt1qyqd39fkyvnrs7sa882eu8pn6t3cyuemtpkqm6zejn1"
},
"assertToBeSingleSigAddressPayload": {
"singleSigAddressPayload": "0x0100d895362326387a1d39d59e1c33d2e382733b586c",
"payloadNotStartsWith0x0100": "0x0000d895362326387a1d39d59e1c33d2e382733b586c",
"paylaodWithIncorrectSize": "0x0100d895362326387a1d39d59e1c33d2e382733b586c1"
}
}
47 changes: 45 additions & 2 deletions packages/ckb-sdk-utils/__tests__/validators/index.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const validators = require('../../lib/validators')
const {
assertToBeHexStringOrBigint,
assertToBeSingleSigAddress,
assertToBeSingleSigAddressPayload,
} = require('../../lib/validators')
const fixtures = require('./fixtures.json')

describe('validators', () => {
describe('assert to be type of hex string or bigint', () => {
const { assertToBeHexStringOrBigint: fixture } = fixtures
const { assertToBeHexStringOrBigint } = validators

it('hex string starts with 0x should pass', () => {
expect(assertToBeHexStringOrBigint(fixture.hexStringStartsWith0x)).toBe(true)
Expand All @@ -26,4 +29,44 @@ describe('validators', () => {
)
})
})

describe('assert to be single sig address', () => {
const { assertToBeSingleSigAddress: fixture } = fixtures

it('single sig address should pass', () => {
expect(assertToBeSingleSigAddress(fixture.singleSigAddress)).toBe(true)
})

it('address with invalid single sig payload should throw an error', () => {
expect(() => assertToBeSingleSigAddress(fixture.addressWithInvalidSingleSigPayload)).toThrow(
new Error(`${fixture.addressWithInvalidSingleSigPayload} is not a single signature address`)
)
})

it('address has the incorrect size should throw an error', () => {
expect(() => assertToBeSingleSigAddress(fixture.addressWithIncorrectSize)).toThrow(
new Error(`${fixture.addressWithIncorrectSize} is not a single signature address`)
)
})
})

describe('assert to be single signature address payload', () => {
const { assertToBeSingleSigAddressPayload: fixture } = fixtures

it('single sig address payload should pass', () => {
expect(assertToBeSingleSigAddressPayload(fixture.singleSigAddressPayload)).toBe(true)
})

it('payload not starts with 0x0100 should throw an error', () => {
expect(() => assertToBeSingleSigAddressPayload(fixture.payloadNotStartsWith0x0100)).toThrow(
new Error(`${fixture.payloadNotStartsWith0x0100} is not a single signature address payload`)
)
})

it('payload has the incorrect size should throw an error', () => {
expect(() => assertToBeSingleSigAddressPayload(fixture.paylaodWithIncorrectSize)).toThrow(
new Error(`${fixture.paylaodWithIncorrectSize} is not a single signature address payload`)
)
})
})
})

This file was deleted.

14 changes: 11 additions & 3 deletions packages/ckb-sdk-utils/src/exceptions/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import HexStringShouldStartWith0x from './hexStringShouldStartWith0x'
import ArgumentRequired from './argumentRequired'
import InvalidHexString from './invalidHexString'
import { HexStringShouldStartWith0x, InvalidHexString } from './invalidHexString'
import { InvalidSingleSignatureAddress, InvalidSingleSignatureAddressPayload } from './invalidAddress'

export { HexStringShouldStartWith0x, ArgumentRequired, InvalidHexString }
export {
HexStringShouldStartWith0x,
ArgumentRequired,
InvalidHexString,
InvalidSingleSignatureAddress,
InvalidSingleSignatureAddressPayload,
}

export default {
HexStringShouldStartWith0x,
ArgumentRequired,
InvalidHexString,
InvalidSingleSignatureAddress,
InvalidSingleSignatureAddressPayload,
}
16 changes: 16 additions & 0 deletions packages/ckb-sdk-utils/src/exceptions/invalidAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export class InvalidSingleSignatureAddressPayload extends Error {
constructor(payload: string) {
super(`${payload} is not a single signature address payload`)
}
}

export class InvalidSingleSignatureAddress extends Error {
constructor(addr: string) {
super(`${addr} is not a single signature address`)
}
}

export default {
InvalidSingleSignatureAddressPayload,
InvalidSingleSignatureAddress,
}
13 changes: 12 additions & 1 deletion packages/ckb-sdk-utils/src/exceptions/invalidHexString.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
export default class extends Error {
export class InvalidHexString extends Error {
constructor(hex: string) {
super(`${hex} is an invalid hex string`)
}
}

export class HexStringShouldStartWith0x extends Error {
constructor(hex: string) {
super(`Hex string ${hex} should start with 0x`)
}
}

export default {
InvalidHexString,
HexStringShouldStartWith0x,
}
29 changes: 28 additions & 1 deletion packages/ckb-sdk-utils/src/validators.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { HexStringShouldStartWith0x } from './exceptions'
import {
HexStringShouldStartWith0x,
InvalidSingleSignatureAddress,
InvalidSingleSignatureAddressPayload,
} from './exceptions'
import { parseAddress } from './address'

export const assertToBeHexStringOrBigint = (value: string | bigint) => {
if (typeof value === 'bigint') {
Expand All @@ -13,6 +18,28 @@ export const assertToBeHexStringOrBigint = (value: string | bigint) => {
throw new TypeError(`${value} should be type of string or bigint`)
}

export const assertToBeSingleSigAddressPayload = (payload: string) => {
if (!payload.startsWith('0x0100') || payload.length !== 46) {
throw new InvalidSingleSignatureAddressPayload(payload)
}
return true
}

export const assertToBeSingleSigAddress = (address: string) => {
if (address.length !== 46) {
throw new InvalidSingleSignatureAddress(address)
}
try {
const payload = parseAddress(address, 'hex')
assertToBeSingleSigAddressPayload(payload)
} catch (err) {
throw new InvalidSingleSignatureAddress(address)
}
return true
}

export default {
assertToBeHexStringOrBigint,
assertToBeSingleSigAddressPayload,
assertToBeSingleSigAddress,
}

0 comments on commit d44effb

Please sign in to comment.