Skip to content

Commit

Permalink
improve wallet + test and improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-aurele-besner committed Jun 5, 2024
1 parent 558d028 commit 6996b89
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 22 deletions.
30 changes: 30 additions & 0 deletions packages/auto-utils/__test__/wallet.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { defaultNetwork, networks } from '../src/constants/network'
import { setupWallet } from '../src/wallet'

describe('Verify wallet functions', () => {
const isLocalhost = process.env.LOCALHOST === 'true'

// Define the test network and its details
const TEST_NETWORK = !isLocalhost
? { networkId: networks[0].id }
: { networkId: 'autonomys-localhost' }
const TEST_NETWORK_DETAIL = networks.find((network) => network.id === TEST_NETWORK.networkId)
if (!TEST_NETWORK_DETAIL) throw new Error(`Network with id ${TEST_NETWORK.networkId} not found`)

const TEST_INVALID_NETWORK = { networkId: 'invalid-network' }

const ALICE_MNEMONIC = 'bottom drive obey lake curtain smoke basket hold race lonely fit walk' //Alice
const ALICE_ADDRESS = '5DFJF7tY4bpbpcKPJcBTQaKuCDEPCpiz8TRjpmLeTtweqmXL'
const BOB_URI = '//BOB'
const BOB_ADDRESS = '5DAw2FpYk2y3JHrsia14KEx7tpezNymdFKkunicZ5ygPGXYF'

test('Check setupWallet return a pair with matching address and public key when provided with a mnemonic', async () => {
const pair = setupWallet({ mnemonic: ALICE_MNEMONIC })
expect(pair.address).toEqual(ALICE_ADDRESS)
})

test('Check setupWallet return a pair with matching private key when provided with a pk', async () => {
const pair = setupWallet({ uri: BOB_URI })
expect(pair.address).toEqual(BOB_ADDRESS)
})
})
2 changes: 1 addition & 1 deletion packages/auto-utils/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApiPromise, WsProvider } from '@polkadot/api'
import { getNetworkDomainRpcUrls, getNetworkRpcUrls } from './network'
import type { DomainInput, NetworkInput } from './types/types'
import type { DomainInput, NetworkInput } from './types/network'

let provider: WsProvider | null = null
let apiInstance: ApiPromise | null = null
Expand Down
2 changes: 1 addition & 1 deletion packages/auto-utils/src/constants/network.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Network } from '../types/types'
import type { Network } from '../types/network'

export const networks: Network[] = [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/auto-utils/src/network.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defaultNetwork, networks } from './constants/network'
import type { DomainInput, NetworkInput } from './types/types'
import type { DomainInput, NetworkInput } from './types/network'

export const getNetworkDetails = (input?: NetworkInput) => {
// If no id is provided, return the default network
Expand Down
3 changes: 2 additions & 1 deletion packages/auto-utils/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './types'
export * from './network'
export * from './wallet'
File renamed without changes.
13 changes: 13 additions & 0 deletions packages/auto-utils/src/types/wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export type Mnemonic = {
mnemonic: string
}

export type URI = {
uri: string
}

export type AppName = {
appName: string
}

export type MnemonicOrURI = Mnemonic | URI
31 changes: 13 additions & 18 deletions packages/auto-utils/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,31 @@ import { Keyring } from '@polkadot/api'
import type { KeyringPair } from '@polkadot/keyring/types'
import { ed25519PairFromSeed, mnemonicToMiniSecret } from '@polkadot/util-crypto'
import { activate, activateDomain } from './api'
import type { DomainInput, NetworkInput } from './types/types'
import type { AppName, DomainInput, Mnemonic, MnemonicOrURI, NetworkInput, URI } from './types'

export const setupWallet = (mnemonicOrPk: string) => {
export const setupWallet = (input: MnemonicOrURI) => {
const keyring = new Keyring({ type: 'sr25519' })

let pair: KeyringPair
if (mnemonicOrPk.startsWith('0x') || mnemonicOrPk.length === 64) {
// Treat as private key
const seed = Buffer.from(mnemonicOrPk.replace(/^0x/, ''), 'hex')
pair = keyring.addFromSeed(seed)
} else {
if ((input as URI).uri) {
// Treat as as uri
pair = keyring.addFromUri((input as URI).uri)
} else if ((input as Mnemonic).mnemonic) {
// Treat as mnemonic
const seed = mnemonicToMiniSecret(mnemonicOrPk)
const seed = mnemonicToMiniSecret((input as Mnemonic).mnemonic)

const { publicKey, secretKey } = ed25519PairFromSeed(seed)
pair = keyring.addFromSeed(secretKey)
}
pair = keyring.addFromPair(ed25519PairFromSeed(seed))
} else throw new Error('Invalid mnemonic or private key')

return pair
}

export type ActivateWalletInput = (NetworkInput | DomainInput) & {
mnemonicOrPk?: string
appName?: string
}
export type ActivateWalletInput = (NetworkInput | DomainInput) & MnemonicOrURI & AppName

export const activateWallet = async (input: ActivateWalletInput) => {
// Create the API instance
const apiInstance =
(input as any).domainId === undefined
(input as DomainInput).domainId === undefined
? await activate(input)
: await activateDomain(input as DomainInput)

Expand All @@ -52,9 +47,9 @@ export const activateWallet = async (input: ActivateWalletInput) => {
} else {
console.warn('No accounts found in the Polkadot.js extension')
}
} else if (input.mnemonicOrPk) {
} else if ((input as Mnemonic).mnemonic || (input as URI).uri) {
// Attach the wallet in a node environment
const account = await setupWallet(input.mnemonicOrPk)
const account = await setupWallet(input)
if (account) console.log('Wallet attached:', account.address)
} else throw new Error('No wallet provided')

Expand Down

0 comments on commit 6996b89

Please sign in to comment.