Skip to content

Commit

Permalink
Implemented requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
siradji committed May 28, 2021
1 parent fbd0588 commit 2644ac7
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ContainerAdapterClient } from '../../container_adapter_client'
import waitForExpect from 'wait-for-expect'

describe('DumpPrivKey', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)

beforeAll(async () => {
await container.start()
await container.waitForReady()
await container.waitForWalletCoinbaseMaturity()
await container.waitForWalletBalanceGTE(101)
})

afterAll(async () => {
await container.stop()
})

it('should reveal private key of given address', async () => {
await waitForExpect(async () => {
const address = 'mwsZw8nF7pKxWH8eoKL9tPxTpaFkz7QeLU'
const privateKey = await client.wallet.dumpPrivKey(address)

expect(typeof privateKey).toStrictEqual('string')
expect(privateKey).toStrictEqual('cRiRQ9cHmy5evDqNDdEV8f6zfbK6epi9Fpz4CRZsmLEmkwy54dWz')
})
})

it('should throw and error when invalid DFI address is provided', async () => {
await waitForExpect(async () => {
const invalidAddress = 'invalidAddress'

await expect(client.wallet.dumpPrivKey(invalidAddress)).rejects.toThrow('Invalid Defi address')
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ContainerAdapterClient } from '../../container_adapter_client'

describe('ImportPrivKey', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)

beforeAll(async () => {
await container.start()
await container.waitForReady()
await container.waitForWalletCoinbaseMaturity()
})

afterAll(async () => {
await container.stop()
})

it('should should import private key without failing ', async () => {
const privatekey = await client.wallet.dumpPrivKey(await client.wallet.getNewAddress())
const promise = client.wallet.importPrivKey(privatekey)

await expect(promise).resolves.not.toThrow()
})

it('should import private key with rescan set to false', async () => {
const privateKey = await client.wallet.dumpPrivKey(await client.wallet.getNewAddress())
const promise = client.wallet.importPrivKey(privateKey, '', false)

await expect(promise).resolves.not.toThrow()
})

it('should import private key with label passed', async () => {
const privateKey = await client.wallet.dumpPrivKey(await client.wallet.getNewAddress('testing'))
const promise = client.wallet.importPrivKey(privateKey, 'testing')

await expect(promise).resolves.not.toThrow()
})

it('should fail and throw an error with invalid private key', async () => {
const privateKey = 'invalidPrivateKey'
const promise = client.wallet.importPrivKey(privateKey)

await expect(promise).rejects.toThrow('Invalid private key encoding')
})
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ContainerAdapterClient } from '../container_adapter_client'
import { ContainerAdapterClient } from '../../container_adapter_client'
import { MasterNodeRegTestContainer, RegTestContainer } from '@defichain/testcontainers'
import { BigNumber, wallet } from '../../src'
import { BigNumber, wallet } from '../../../src'
import waitForExpect from 'wait-for-expect'
import {
UTXO,
Expand All @@ -9,7 +9,7 @@ import {
SendToAddressOptions,
Mode,
SendManyOptions
} from '../../src/category/wallet'
} from '../../../src/category/wallet'

describe('getBalance', () => {
describe('regtest', () => {
Expand Down Expand Up @@ -876,80 +876,3 @@ describe('masternode', () => {
})
})
})

describe('dumpPrivKey', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)

beforeAll(async () => {
await container.start()
await container.waitForReady()
await container.waitForWalletCoinbaseMaturity()
await container.waitForWalletBalanceGTE(101)
})

afterAll(async () => {
await container.stop()
})

it('should reveal private key of given address', async () => {
await waitForExpect(async () => {
const address = 'mwsZw8nF7pKxWH8eoKL9tPxTpaFkz7QeLU'
const privateKey = await client.wallet.dumpPrivKey(address)

expect(typeof privateKey).toStrictEqual('string')
expect(privateKey).toStrictEqual('cRiRQ9cHmy5evDqNDdEV8f6zfbK6epi9Fpz4CRZsmLEmkwy54dWz')
})
})

it('should throw and error when invalid DFI address is provided', async () => {
await waitForExpect(async () => {
const invalidAddress = 'invalidAddress'

await expect(client.wallet.dumpPrivKey(invalidAddress)).rejects.toThrow('Invalid Defi address')
})
})
})

describe('importPrivKey', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)

beforeAll(async () => {
await container.start()
await container.waitForReady()
await container.waitForWalletCoinbaseMaturity()
})

afterAll(async () => {
await container.stop()
})

it('should should import private key without failing ', async () => {
const privatekey = await client.wallet.dumpPrivKey(await client.wallet.getNewAddress())
const promise = client.wallet.importPrivKey(privatekey)

await expect(promise).resolves.not.toThrow()
})

it('should import private key with rescan set to false', async () => {
const privateKey = await client.wallet.dumpPrivKey(await client.wallet.getNewAddress())
const promise = client.wallet.importPrivKey(privateKey, '', false)

await expect(promise).resolves.not.toThrow()
})

it('should import private key with label passed', async () => {
const privateKey = await client.wallet.dumpPrivKey(await client.wallet.getNewAddress('testing'))
const promise = client.wallet.importPrivKey(privateKey, 'testing')

await expect(promise).resolves.not.toThrow()
})

it('should fail and throw an error with invalid private key', async () => {
const privateKey = 'invalidPrivateKey'
const promise = client.wallet.importPrivKey(privateKey)

await expect(promise).rejects.toThrow('Invalid private key encoding')
})
})

0 comments on commit 2644ac7

Please sign in to comment.