-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Small
app/api/wallet
refactoring (#332)
* moved all network code into network.ts * updated persistence.ts * refactor provider * Fixed screen with wallet reference * added missing snapshot
- Loading branch information
Showing
19 changed files
with
282 additions
and
208 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { EnvironmentNetwork } from "../../environment"; | ||
import { getBip32Option } from "./network"; | ||
|
||
it('should resolve bip32 option for MainNet', () => { | ||
const bip32Options = getBip32Option(EnvironmentNetwork.MainNet) | ||
expect(bip32Options).toMatchSnapshot() | ||
}) | ||
|
||
it('should resolve bip32 option for TestNet', () => { | ||
const bip32Options = getBip32Option(EnvironmentNetwork.TestNet) | ||
expect(bip32Options).toMatchSnapshot() | ||
}) | ||
|
||
it('should resolve bip32 option for LocalPlayground', () => { | ||
const bip32Options = getBip32Option(EnvironmentNetwork.LocalPlayground) | ||
expect(bip32Options).toMatchSnapshot() | ||
}) | ||
|
||
it('should resolve bip32 option for RemotePlayground', () => { | ||
const bip32Options = getBip32Option(EnvironmentNetwork.RemotePlayground) | ||
expect(bip32Options).toMatchSnapshot() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { MnemonicProviderData } from "@defichain/jellyfish-wallet-mnemonic"; | ||
import { WhaleApiClient } from "@defichain/whale-api-client"; | ||
import { EnvironmentNetwork } from "../../../environment"; | ||
import { WalletPersistenceData, WalletType } from "../persistence"; | ||
import { initWhaleWallet } from "./index"; | ||
|
||
beforeEach(async () => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
const network = EnvironmentNetwork.LocalPlayground | ||
const client = new WhaleApiClient({ url: 'http://localhost:19553', network: 'regtest' }) | ||
|
||
it('should initWhaleWallet', async () => { | ||
const data: WalletPersistenceData<MnemonicProviderData> = { | ||
version: "v1", | ||
type: WalletType.MNEMONIC_UNPROTECTED, | ||
raw: { | ||
words: ['abandon', 'abandon', 'abandon', 'abandon', 'abandon', 'abandon', 'abandon', 'abandon', 'abandon', 'abandon', 'abandon', 'abandon', 'abandon', 'abandon', 'abandon', 'abandon', 'abandon', 'abandon', 'abandon', 'abandon', 'abandon', 'abandon', 'abandon', 'art'], | ||
privKey: '235b34cd7c9f6d7e4595ffe9ae4b1cb5606df8aca2b527d20a07c8f56b2342f4', | ||
chainCode: 'f40eaad21641ca7cb5ac00f9ce21cac9ba070bb673a237f7bce57acda54386a4' | ||
} | ||
} | ||
|
||
const wallet = initWhaleWallet(data, network, client) | ||
|
||
expect(wallet.get(0).withTransactionBuilder().utxo).toBeDefined() | ||
expect(wallet.get(0).withTransactionBuilder().dex).toBeDefined() | ||
expect(wallet.get(0).withTransactionBuilder().account).toBeDefined() | ||
expect(wallet.get(0).withTransactionBuilder().liqPool).toBeDefined() | ||
}); | ||
|
||
|
||
it('should fail as wallet type not available', async () => { | ||
const data: WalletPersistenceData<string> = { | ||
version: "v1", | ||
type: undefined as any, | ||
raw: "" | ||
} | ||
|
||
expect(() => { | ||
initWhaleWallet(data, network, client) | ||
}).toThrow('wallet undefined not available') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { JellyfishWallet, WalletHdNode, WalletHdNodeProvider } from '@defichain/jellyfish-wallet' | ||
import { WhaleApiClient } from '@defichain/whale-api-client' | ||
import { WhaleWalletAccount, WhaleWalletAccountProvider } from '@defichain/whale-api-wallet' | ||
import { EnvironmentNetwork } from '../../../environment' | ||
import { getJellyfishNetwork } from '../network' | ||
import { WalletPersistenceData, WalletType } from '../persistence' | ||
import { MnemonicUnprotected } from './mnemonic_unprotected' | ||
|
||
/** | ||
* Whale JellyfishWallet connected to Whale APIs via the Ocean Infrastructure | ||
*/ | ||
export type WhaleWallet = JellyfishWallet<WhaleWalletAccount, WalletHdNode> | ||
|
||
export function initWhaleWallet (data: WalletPersistenceData<any>, network: EnvironmentNetwork, client: WhaleApiClient): WhaleWallet { | ||
const jellyfishNetwork = getJellyfishNetwork(network) | ||
|
||
const walletProvider = resolveProvider(data, network) | ||
const accountProvider = new WhaleWalletAccountProvider(client, jellyfishNetwork) | ||
|
||
return new JellyfishWallet(walletProvider, accountProvider) | ||
} | ||
|
||
/** | ||
* @param {WalletPersistenceData} data to resolve wallet provider for init | ||
* @param {EnvironmentNetwork} network | ||
*/ | ||
function resolveProvider (data: WalletPersistenceData<any>, network: EnvironmentNetwork): WalletHdNodeProvider<WalletHdNode> { | ||
switch (data.type) { | ||
case WalletType.MNEMONIC_UNPROTECTED: | ||
return MnemonicUnprotected.initProvider(data, network) | ||
|
||
default: | ||
throw new Error(`wallet ${data.type as string} not available`) | ||
} | ||
} |
Oops, something went wrong.