Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring jellyfish-address #302

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 0 additions & 57 deletions packages/jellyfish-address/__tests__/base58_address.test.ts

This file was deleted.

51 changes: 0 additions & 51 deletions packages/jellyfish-address/__tests__/bech32_address.test.ts

This file was deleted.

37 changes: 8 additions & 29 deletions packages/jellyfish-address/__tests__/p2pkh.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import bs58 from 'bs58'
import { MainNet, RegTest, TestNet } from '@defichain/jellyfish-network'
import { OP_CODES } from '@defichain/jellyfish-transaction'
import { RegTestContainer } from '@defichain/testcontainers'
Expand All @@ -25,21 +24,21 @@ describe('P2PKH', () => {

describe('from() - valid address', () => {
it('should get the type precisely', () => {
const p2pkh = DeFiAddress.from('mainnet', p2pkhFixture.mainnet)
const p2pkh = DeFiAddress.from(p2pkhFixture.mainnet)
expect(p2pkh.valid).toBeTruthy()
expect(p2pkh.type).toStrictEqual('P2PKH')
expect(p2pkh.constructor.name).toStrictEqual('P2PKH')
expect(p2pkh.network).toStrictEqual(MainNet)
})

it('should work for all recognized network type', () => {
const testnet = DeFiAddress.from('testnet', p2pkhFixture.testnet)
const testnet = DeFiAddress.from(p2pkhFixture.testnet)
expect(testnet.valid).toBeTruthy()
expect(testnet.type).toStrictEqual('P2PKH')
expect(testnet.constructor.name).toStrictEqual('P2PKH')
expect(testnet.network).toStrictEqual(TestNet)

const regtest = DeFiAddress.from('regtest', p2pkhFixture.regtest)
const regtest = DeFiAddress.from(p2pkhFixture.regtest)
expect(regtest.valid).toBeTruthy()
expect(regtest.type).toStrictEqual('P2PKH')
expect(regtest.constructor.name).toStrictEqual('P2PKH')
Expand All @@ -49,20 +48,20 @@ describe('P2PKH', () => {

describe('from() - invalid address', () => {
it('should be able to validate in address prefix with network', () => {
const invalid = DeFiAddress.from('mainnet', p2pkhFixture.invalid)
const invalid = DeFiAddress.from(p2pkhFixture.invalid)
expect(invalid.valid).toBeFalsy()
})

it('should be able to validate in address prefix with network', () => {
// valid address, used on different network
const p2pkh = DeFiAddress.from('testnet', p2pkhFixture.mainnet)
const p2pkh = DeFiAddress.from(p2pkhFixture.mainnet)
expect(p2pkh.valid).toBeFalsy()
// expect(p2pkh.type).toStrictEqual('P2PKH') // invalid address guessed type is not promising, as p2pkh and p2sh are versy similar
expect(p2pkh.network).toStrictEqual(TestNet)
})

it('should get the type precisely', () => {
const invalid = DeFiAddress.from('mainnet', p2pkhFixture.invalidChecksum)
const invalid = DeFiAddress.from(p2pkhFixture.invalidChecksum)
expect(invalid.valid).toBeFalsy()
})
})
Expand Down Expand Up @@ -97,7 +96,7 @@ describe('P2PKH', () => {

describe('getScript()', () => {
it('should refuse to build ops code stack for invalid address', () => {
const invalid = DeFiAddress.from('testnet', p2pkhFixture.mainnet)
const invalid = DeFiAddress.from(p2pkhFixture.mainnet)
expect(invalid.valid).toBeFalsy()
try {
invalid.getScript()
Expand All @@ -107,7 +106,7 @@ describe('P2PKH', () => {
})

it('should be able to build script', async () => {
const p2pkh = DeFiAddress.from('mainnet', p2pkhFixture.mainnet)
const p2pkh = DeFiAddress.from(p2pkhFixture.mainnet)
const scriptStack = p2pkh.getScript()

expect(scriptStack.stack.length).toStrictEqual(5)
Expand All @@ -118,24 +117,4 @@ describe('P2PKH', () => {
expect(scriptStack.stack[4]).toStrictEqual(OP_CODES.OP_CHECKSIG)
})
})

it('validate()', () => {
const hex = bs58.decode(p2pkhFixture.mainnet).toString('hex').substring(2, 42) // take 20 bytes data only
const p2pkh = new P2PKH(MainNet, p2pkhFixture.mainnet, hex)

expect(p2pkh.validatorPassed).toStrictEqual(0)
expect(p2pkh.valid).toBeFalsy()

const isValid = p2pkh.validate()
expect(p2pkh.validatorPassed).toStrictEqual(5)
expect(isValid).toBeTruthy()
})

it('guess()', () => {
const p2pkh = DeFiAddress.guess(p2pkhFixture.mainnet)
expect(p2pkh.valid).toBeTruthy()
expect(p2pkh.type).toStrictEqual('P2PKH')
expect(p2pkh.constructor.name).toStrictEqual('P2PKH')
expect(p2pkh.network).toStrictEqual(MainNet)
})
})
37 changes: 8 additions & 29 deletions packages/jellyfish-address/__tests__/p2sh.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import bs58 from 'bs58'
import { MainNet, RegTest, TestNet } from '@defichain/jellyfish-network'
import { OP_CODES } from '@defichain/jellyfish-transaction'
import { RegTestContainer } from '@defichain/testcontainers'
Expand All @@ -25,21 +24,21 @@ describe('P2SH', () => {

describe('from() - valid address', () => {
it('should get the type precisely', () => {
const p2sh = DeFiAddress.from('mainnet', p2shFixture.mainnet)
const p2sh = DeFiAddress.from(p2shFixture.mainnet)
expect(p2sh.valid).toBeTruthy()
expect(p2sh.type).toStrictEqual('P2SH')
expect(p2sh.constructor.name).toStrictEqual('P2SH')
expect(p2sh.network).toStrictEqual(MainNet)
})

it('should work for all recognized network type', () => {
const testnet = DeFiAddress.from('testnet', p2shFixture.testnet)
const testnet = DeFiAddress.from(p2shFixture.testnet)
expect(testnet.valid).toBeTruthy()
expect(testnet.type).toStrictEqual('P2SH')
expect(testnet.constructor.name).toStrictEqual('P2SH')
expect(testnet.network).toStrictEqual(TestNet)

const regtest = DeFiAddress.from('regtest', p2shFixture.regtest)
const regtest = DeFiAddress.from(p2shFixture.regtest)
expect(regtest.valid).toBeTruthy()
expect(regtest.type).toStrictEqual('P2SH')
expect(regtest.constructor.name).toStrictEqual('P2SH')
Expand All @@ -49,20 +48,20 @@ describe('P2SH', () => {

describe('from() - invalid address', () => {
it('trimmed prefix', () => {
const invalid = DeFiAddress.from('mainnet', p2shFixture.invalid)
const invalid = DeFiAddress.from(p2shFixture.invalid)
expect(invalid.valid).toBeFalsy()
})

it('should be able to validate in address prefix with network', () => {
// valid address, used on different network
const p2sh = DeFiAddress.from('testnet', p2shFixture.mainnet)
const p2sh = DeFiAddress.from(p2shFixture.mainnet)
expect(p2sh.valid).toBeFalsy()
// expect(p2sh.type).toStrictEqual('P2SH') // invalid address guessed type is not promising, as p2sh and p2sh are versy similar
expect(p2sh.network).toStrictEqual(TestNet)
})

it('should get the type precisely', () => {
const invalid = DeFiAddress.from('mainnet', p2shFixture.invalidChecksum)
const invalid = DeFiAddress.from(p2shFixture.invalidChecksum)
expect(invalid.valid).toBeFalsy()
})
})
Expand Down Expand Up @@ -98,7 +97,7 @@ describe('P2SH', () => {

describe('getScript()', () => {
it('should refuse to build ops code stack for invalid address', () => {
const invalid = DeFiAddress.from('testnet', p2shFixture.mainnet)
const invalid = DeFiAddress.from(p2shFixture.mainnet)
expect(invalid.valid).toBeFalsy()
try {
invalid.getScript()
Expand All @@ -108,7 +107,7 @@ describe('P2SH', () => {
})

it('should be able to build script', async () => {
const p2sh = DeFiAddress.from('mainnet', p2shFixture.mainnet)
const p2sh = DeFiAddress.from(p2shFixture.mainnet)
const scriptStack = p2sh.getScript()

expect(scriptStack.stack.length).toStrictEqual(3)
Expand All @@ -117,24 +116,4 @@ describe('P2SH', () => {
expect(scriptStack.stack[2]).toStrictEqual(OP_CODES.OP_EQUAL)
})
})

it('validate()', () => {
const hex = bs58.decode(p2shFixture.mainnet).toString('hex').substring(2, 42) // take 20 bytes data only
const p2sh = new P2SH(MainNet, p2shFixture.mainnet, hex)

expect(p2sh.validatorPassed).toStrictEqual(0)
expect(p2sh.valid).toBeFalsy()

const isValid = p2sh.validate()
expect(p2sh.validatorPassed).toStrictEqual(5)
expect(isValid).toBeTruthy()
})

it('guess()', () => {
const p2sh = DeFiAddress.guess(p2shFixture.mainnet)
expect(p2sh.valid).toBeTruthy()
expect(p2sh.type).toStrictEqual('P2SH')
expect(p2sh.constructor.name).toStrictEqual('P2SH')
expect(p2sh.network).toStrictEqual(MainNet)
})
})
36 changes: 8 additions & 28 deletions packages/jellyfish-address/__tests__/p2wpkh.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ describe('P2WPKH', () => {

describe('from() - valid address', () => {
it('should get the type precisely', () => {
const p2wpkh = DeFiAddress.from('mainnet', p2wpkhFixture.mainnet)
const p2wpkh = DeFiAddress.from(p2wpkhFixture.mainnet)
expect(p2wpkh.valid).toBeTruthy()
expect(p2wpkh.type).toStrictEqual('P2WPKH')
expect(p2wpkh.constructor.name).toStrictEqual('P2WPKH')
expect(p2wpkh.network).toStrictEqual(MainNet)
})

it('should work for all recognized network type', () => {
const testnet = DeFiAddress.from('testnet', p2wpkhFixture.testnet)
const testnet = DeFiAddress.from(p2wpkhFixture.testnet)
expect(testnet.valid).toBeTruthy()
expect(testnet.type).toStrictEqual('P2WPKH')
expect(testnet.constructor.name).toStrictEqual('P2WPKH')
expect(testnet.network).toStrictEqual(TestNet)

const regtest = DeFiAddress.from('regtest', p2wpkhFixture.regtest)
const regtest = DeFiAddress.from(p2wpkhFixture.regtest)
expect(regtest.valid).toBeTruthy()
expect(regtest.type).toStrictEqual('P2WPKH')
expect(regtest.constructor.name).toStrictEqual('P2WPKH')
Expand All @@ -48,18 +48,18 @@ describe('P2WPKH', () => {

describe('from() - invalid address', () => {
it('trimmed prefix', () => {
const invalid = DeFiAddress.from('mainnet', p2wpkhFixture.trimmedPrefix)
const invalid = DeFiAddress.from(p2wpkhFixture.trimmedPrefix)
expect(invalid.valid).toBeFalsy()
})

it('invalid character set', () => {
const invalid = DeFiAddress.from('mainnet', p2wpkhFixture.invalid)
const invalid = DeFiAddress.from(p2wpkhFixture.invalid)
expect(invalid.valid).toBeFalsy()
})

it('should be able to validate in address prefix with network', () => {
// valid address, used on different network
const p2wpkh = DeFiAddress.from('testnet', p2wpkhFixture.mainnet)
const p2wpkh = DeFiAddress.from(p2wpkhFixture.mainnet)
expect(p2wpkh.valid).toBeFalsy()
// expect(p2wpkh.type).toStrictEqual('P2WPKH') // invalid address guessed type is not promising, as p2wpkh and p2wpkh are versy similar
expect(p2wpkh.network).toStrictEqual(TestNet)
Expand Down Expand Up @@ -96,7 +96,7 @@ describe('P2WPKH', () => {

describe('getScript()', () => {
it('should refuse to build ops code stack for invalid address', () => {
const invalid = DeFiAddress.from('testnet', p2wpkhFixture.mainnet)
const invalid = DeFiAddress.from(p2wpkhFixture.mainnet)
expect(invalid.valid).toBeFalsy()
try {
invalid.getScript()
Expand All @@ -106,32 +106,12 @@ describe('P2WPKH', () => {
})

it('should be able to build script', async () => {
const p2wpkh = DeFiAddress.from('mainnet', p2wpkhFixture.mainnet)
const p2wpkh = DeFiAddress.from(p2wpkhFixture.mainnet)
const scriptStack = p2wpkh.getScript()

expect(scriptStack.stack.length).toStrictEqual(2)
expect(scriptStack.stack[0]).toStrictEqual(OP_CODES.OP_0)
expect(scriptStack.stack[1].type).toStrictEqual('OP_PUSHDATA')
})
})

it('validate()', () => {
const data = '0e7c0ab18b305bc987a266dc06de26fcfab4b56a' // 20 bytes

const p2wpkh = new P2WPKH(RegTest, p2wpkhFixture.regtest, data)
expect(p2wpkh.validatorPassed).toStrictEqual(0)
expect(p2wpkh.valid).toBeFalsy()

const isValid = p2wpkh.validate()
expect(p2wpkh.validatorPassed).toStrictEqual(5) // length, network prefix, data character set
expect(isValid).toBeTruthy()
})

it('guess()', () => {
const p2wpkh = DeFiAddress.guess(p2wpkhFixture.mainnet)
expect(p2wpkh.valid).toBeTruthy()
expect(p2wpkh.type).toStrictEqual('P2WPKH')
expect(p2wpkh.constructor.name).toStrictEqual('P2WPKH')
expect(p2wpkh.network).toStrictEqual(MainNet)
})
})
Loading