diff --git a/.yarn/install-state.gz b/.yarn/install-state.gz deleted file mode 100644 index 52963633..00000000 Binary files a/.yarn/install-state.gz and /dev/null differ diff --git a/packages/auto-consensus/__test__/balances.test.ts b/packages/auto-consensus/__test__/balances.test.ts index 776141a0..d3f44caa 100644 --- a/packages/auto-consensus/__test__/balances.test.ts +++ b/packages/auto-consensus/__test__/balances.test.ts @@ -1,5 +1,5 @@ -import { address, balance, totalIssuance } from '@autonomys/auto-consensus' -import { ActivateWalletInput, activateWallet, getMockWallet } from '@autonomys/auto-utils' +import { balance, totalIssuance } from '@autonomys/auto-consensus' +import { ActivateWalletInput, activateWallet, address, getMockWallet } from '@autonomys/auto-utils' import { setup } from './helpers' describe('Verify balances functions', () => { diff --git a/packages/auto-consensus/__test__/helpers/staking.ts b/packages/auto-consensus/__test__/helpers/staking.ts index 6dd2d6e2..4b50d368 100644 --- a/packages/auto-consensus/__test__/helpers/staking.ts +++ b/packages/auto-consensus/__test__/helpers/staking.ts @@ -1,4 +1,3 @@ -import type { ApiPromise } from '@polkadot/api' import { u8aToHex } from '@polkadot/util' import { operator, operators, RegisterOperatorInput } from '../../src/staking' diff --git a/packages/auto-consensus/__test__/helpers/tx.ts b/packages/auto-consensus/__test__/helpers/tx.ts index 447d5204..9e0d2e3a 100644 --- a/packages/auto-consensus/__test__/helpers/tx.ts +++ b/packages/auto-consensus/__test__/helpers/tx.ts @@ -1,6 +1,5 @@ import { Events, signAndSendTx as signAndSend } from '@autonomys/auto-consensus' -import type { AddressOrPair, SubmittableExtrinsic } from '@polkadot/api/types' -import type { ISubmittableResult } from '@polkadot/types/types' +import type { AddressOrPair, ISubmittableResult, SubmittableExtrinsic } from '@autonomys/auto-utils' export const signAndSendTx = async ( sender: AddressOrPair, diff --git a/packages/auto-consensus/__test__/staking.test.ts b/packages/auto-consensus/__test__/staking.test.ts index fbc0d448..dc39bc51 100644 --- a/packages/auto-consensus/__test__/staking.test.ts +++ b/packages/auto-consensus/__test__/staking.test.ts @@ -1,5 +1,4 @@ import { - address, balance, deregisterOperator, events, @@ -9,8 +8,13 @@ import { sudo, withdrawStake, } from '@autonomys/auto-consensus' -import { ActivateWalletInput, activateWallet, getMockWallet } from '@autonomys/auto-utils' -import { mnemonicGenerate } from '@polkadot/util-crypto' +import { + ActivateWalletInput, + activateWallet, + address, + generateWallet, + getMockWallet, +} from '@autonomys/auto-utils' import { setup, signAndSendTx, @@ -26,10 +30,10 @@ describe('Verify staking functions', () => { if (isLocalhost) { describe('Test registerOperator()', () => { test('Check Alice can register random wallet as an operator', async () => { - const mnemonicOperator = mnemonicGenerate() + const { mnemonic } = generateWallet() const { accounts: operatorAccounts } = await activateWallet({ ...TEST_NETWORK, - uri: mnemonicOperator, + mnemonic, } as ActivateWalletInput) const sender = alice.accounts[0] @@ -87,10 +91,10 @@ describe('Verify staking functions', () => { }, 10000) test('Check Operator can addFunds after registration', async () => { - const mnemonicOperator = mnemonicGenerate() + const { mnemonic } = generateWallet() const { accounts: operatorAccounts } = await activateWallet({ ...TEST_NETWORK, - uri: mnemonicOperator, + mnemonic, } as ActivateWalletInput) const sender = alice.accounts[0] @@ -139,10 +143,10 @@ describe('Verify staking functions', () => { describe('Test deregisterOperator()', () => { test('Check Operator can deregisterOperator after registration', async () => { - const mnemonicOperator = mnemonicGenerate() + const { mnemonic } = generateWallet() const { accounts: operatorAccounts } = await activateWallet({ ...TEST_NETWORK, - uri: mnemonicOperator, + mnemonic, } as ActivateWalletInput) const sender = alice.accounts[0] @@ -181,7 +185,7 @@ describe('Verify staking functions', () => { api: alice.api, operatorId: findOperator.operatorId, }), - [events.operatorDeRegistered], + [events.operatorDeregistered], ) } }, 60000) diff --git a/packages/auto-consensus/__test__/transfer.test.ts b/packages/auto-consensus/__test__/transfer.test.ts index f0ea4eee..3339b643 100644 --- a/packages/auto-consensus/__test__/transfer.test.ts +++ b/packages/auto-consensus/__test__/transfer.test.ts @@ -1,5 +1,5 @@ -import { address, balance, events, transfer } from '@autonomys/auto-consensus' -import { getMockWallet } from '@autonomys/auto-utils' +import { balance, events, transfer } from '@autonomys/auto-consensus' +import { address, getMockWallet } from '@autonomys/auto-utils' import { setup, signAndSendTx } from './helpers' describe('Verify transfer functions', () => { diff --git a/packages/auto-consensus/src/address.ts b/packages/auto-consensus/src/address.ts deleted file mode 100644 index 8ea418c2..00000000 --- a/packages/auto-consensus/src/address.ts +++ /dev/null @@ -1,5 +0,0 @@ -// file: src/address.ts - -import { encodeAddress } from '@polkadot/keyring' - -export const address = (address: string | Uint8Array): string => encodeAddress(address, 2254) diff --git a/packages/auto-consensus/src/balances.ts b/packages/auto-consensus/src/balances.ts index f051326f..fe022331 100644 --- a/packages/auto-consensus/src/balances.ts +++ b/packages/auto-consensus/src/balances.ts @@ -1,8 +1,7 @@ // file: src/balances.ts +import type { ApiPromise, BN } from '@autonomys/auto-utils' import { activate } from '@autonomys/auto-utils' -import type { ApiPromise } from '@polkadot/api' -import type { BN } from '@polkadot/util' type RawBalanceData = { free: BN diff --git a/packages/auto-consensus/src/batch.ts b/packages/auto-consensus/src/batch.ts index cf2e6a38..1a1f7464 100644 --- a/packages/auto-consensus/src/batch.ts +++ b/packages/auto-consensus/src/batch.ts @@ -1,3 +1,3 @@ -import type { ApiPromise } from '@polkadot/api' +import type { ApiPromise } from '@autonomys/auto-utils' export const batch = async (api: ApiPromise, txs: any[]) => await api.tx.utility.batch(txs) diff --git a/packages/auto-consensus/src/index.ts b/packages/auto-consensus/src/index.ts index b2845280..5fdedda4 100644 --- a/packages/auto-consensus/src/index.ts +++ b/packages/auto-consensus/src/index.ts @@ -1,6 +1,5 @@ // file: src/index.ts -export * from './address' export * from './balances' export * from './batch' export * from './info' diff --git a/packages/auto-consensus/src/remark.ts b/packages/auto-consensus/src/remark.ts index cc8a2274..a95b5fe6 100644 --- a/packages/auto-consensus/src/remark.ts +++ b/packages/auto-consensus/src/remark.ts @@ -1,4 +1,4 @@ -import type { ApiPromise } from '@polkadot/api' +import type { ApiPromise } from '@autonomys/auto-utils' export const remark = async (api: ApiPromise, remark: string, withEvent?: boolean) => !withEvent ? await api.tx.system.remark(remark) : await api.tx.system.remarkWithEvent(remark) diff --git a/packages/auto-consensus/src/staking.ts b/packages/auto-consensus/src/staking.ts index 3119a311..747fefe4 100644 --- a/packages/auto-consensus/src/staking.ts +++ b/packages/auto-consensus/src/staking.ts @@ -1,11 +1,9 @@ // file: src/staking.ts -import type { ApiPromise } from '@polkadot/api' -import type { KeyringPair } from '@polkadot/keyring/types' +import type { ApiPromise, KeyringPair } from '@autonomys/auto-utils' +import { createAccountIdType, signingKey } from '@autonomys/auto-utils' import type { StorageKey } from '@polkadot/types' -import { createType } from '@polkadot/types' import type { AnyTuple, Codec } from '@polkadot/types-codec/types' -import { u8aToHex } from '@polkadot/util' type RawOperatorId = string[] type RawOperatorDetails = { @@ -126,15 +124,14 @@ export const registerOperator = async (input: RegisterOperatorInput) => { nominationTax, } = input - const message = createType(api.registry, 'AccountId', senderAddress).toU8a() - const signingKey = u8aToHex(Operator.publicKey) + const message = createAccountIdType(api, senderAddress) const signature = Operator.sign(message) return await api.tx.domains.registerOperator( parseString(domainId), parseString(amountToStake), { - signingKey, + signingKey: signingKey(Operator.publicKey), minimumNominatorStake: parseString(minimumNominatorStake), nominationTax: parseString(nominationTax), }, diff --git a/packages/auto-consensus/src/transfer.ts b/packages/auto-consensus/src/transfer.ts index 414f2f4e..0b0fcd15 100644 --- a/packages/auto-consensus/src/transfer.ts +++ b/packages/auto-consensus/src/transfer.ts @@ -1,6 +1,6 @@ // file: src/transfer.ts -import type { ApiPromise } from '@polkadot/api' +import type { ApiPromise } from '@autonomys/auto-utils' export type Amount = BigInt | number | string diff --git a/packages/auto-consensus/src/utils/detectTxSuccess.ts b/packages/auto-consensus/src/utils/detectTxSuccess.ts index f1f00488..e75abaa3 100644 --- a/packages/auto-consensus/src/utils/detectTxSuccess.ts +++ b/packages/auto-consensus/src/utils/detectTxSuccess.ts @@ -1,6 +1,6 @@ // file: src/utils/detectTxSuccess.ts -import type { EventRecord } from '@polkadot/types/interfaces' +import type { EventRecord } from '@autonomys/auto-utils' import { expectSuccessfulTxEvent } from './events' export const detectTxSuccess = (events: EventRecord[]): boolean => { diff --git a/packages/auto-consensus/src/utils/signAndSendTx.ts b/packages/auto-consensus/src/utils/signAndSendTx.ts index bcb7bb04..86ec1d1b 100644 --- a/packages/auto-consensus/src/utils/signAndSendTx.ts +++ b/packages/auto-consensus/src/utils/signAndSendTx.ts @@ -1,7 +1,6 @@ // file: src/utils/signAndSendTx.ts -import type { AddressOrPair, SubmittableExtrinsic } from '@polkadot/api/types' -import type { ISubmittableResult } from '@polkadot/types/types' +import type { AddressOrPair, ISubmittableResult, SubmittableExtrinsic } from '@autonomys/auto-utils' import type { Events } from '../types/events' import type { EventsValidated, TransactionSignedAndSend } from '../types/transaction' import { detectTxSuccess } from './detectTxSuccess' diff --git a/packages/auto-consensus/src/utils/sudo.ts b/packages/auto-consensus/src/utils/sudo.ts index beefb27e..b5a725b0 100644 --- a/packages/auto-consensus/src/utils/sudo.ts +++ b/packages/auto-consensus/src/utils/sudo.ts @@ -1,8 +1,11 @@ // file: src/utils/sudo.ts -import { ApiPromise } from '@polkadot/api' -import type { AddressOrPair, SubmittableExtrinsic } from '@polkadot/api/types' -import type { ISubmittableResult } from '@polkadot/types/types' +import type { + AddressOrPair, + ApiPromise, + ISubmittableResult, + SubmittableExtrinsic, +} from '@autonomys/auto-utils' import type { Events } from '../types/events' import { expectSuccessfulTxEvent } from './events' import { signAndSendTx } from './signAndSendTx' diff --git a/packages/auto-consensus/src/utils/validateEvents.ts b/packages/auto-consensus/src/utils/validateEvents.ts index c1c8d92a..814be9bf 100644 --- a/packages/auto-consensus/src/utils/validateEvents.ts +++ b/packages/auto-consensus/src/utils/validateEvents.ts @@ -1,6 +1,6 @@ // file: src/utils/validateEvents.ts -import type { EventRecord } from '@polkadot/types/interfaces' +import type { EventRecord } from '@autonomys/auto-utils' import type { Events } from '../types/events' import type { EventsValidated } from '../types/transaction' import { expectSuccessfulTxEvent } from './events' diff --git a/packages/auto-utils/__test__/wallet.test.ts b/packages/auto-utils/__test__/wallet.test.ts index b04b3f37..bcbe7015 100644 --- a/packages/auto-utils/__test__/wallet.test.ts +++ b/packages/auto-utils/__test__/wallet.test.ts @@ -3,6 +3,7 @@ import { NetworkInput, WalletActivated, activateWallet, + address, mockWallets, networks, setupWallet, @@ -30,22 +31,25 @@ describe('Verify wallet functions', () => { wallets = await mockWallets() aliceWallet = wallets[0].accounts[0] bobWallet = wallets[1].accounts[0] - }) + }, 20000) describe('Test setupWallet()', () => { test('Check setupWallet return a pair with matching address and public key when provided with a mnemonic', async () => { - const pair = await setupWallet({ mnemonic: TEST_MNEMONIC }) - expect(pair.address).toEqual(TEST_ADDRESS) + const wallet = setupWallet({ mnemonic: TEST_MNEMONIC }) + expect(wallet.commonAddress).toEqual(TEST_ADDRESS) + expect(wallet.address).toEqual(address(TEST_ADDRESS)) }) test('Check setupWallet return a pair with matching private key when provided with Alice seed', async () => { - const pair = await setupWallet({ uri: ALICE_URI }) - expect(pair.address).toEqual(aliceWallet.address) + const wallet = setupWallet({ uri: ALICE_URI }) + expect(wallet.commonAddress).toEqual(aliceWallet.address) + expect(wallet.address).toEqual(address(aliceWallet.address)) }) test('Check setupWallet return a pair with matching private key when provided with Bob seed', async () => { - const pair = await setupWallet({ uri: BOB_URI }) - expect(pair.address).toEqual(bobWallet.address) + const wallet = setupWallet({ uri: BOB_URI }) + expect(wallet.commonAddress).toEqual(bobWallet.address) + expect(wallet.address).toEqual(address(bobWallet.address)) }) }) diff --git a/packages/auto-utils/src/address.ts b/packages/auto-utils/src/address.ts new file mode 100644 index 00000000..878220be --- /dev/null +++ b/packages/auto-utils/src/address.ts @@ -0,0 +1,9 @@ +// file: src/address.ts + +import { decodeAddress, encodeAddress } from '@polkadot/keyring' +import { DEFAULT_SS58_FORMAT } from './constants/wallet' + +export const address = (address: string | Uint8Array): string => + encodeAddress(address, DEFAULT_SS58_FORMAT) + +export const decode = (address: string): Uint8Array => decodeAddress(address) diff --git a/packages/auto-utils/src/api.ts b/packages/auto-utils/src/api.ts index 26bc354d..5a244844 100644 --- a/packages/auto-utils/src/api.ts +++ b/packages/auto-utils/src/api.ts @@ -1,50 +1,32 @@ -import { ApiPromise as PolkadotApiPromise, WsProvider } from '@polkadot/api' +import { ApiPromise, WsProvider } from '@polkadot/api' import { getNetworkDomainRpcUrls, getNetworkRpcUrls } from './network' -import type { ApiPromise } from './types/api' import type { DomainInput, NetworkInput } from './types/network' -let provider: WsProvider | null = null -let apiInstance: ApiPromise | null = null - -let domainProvider: WsProvider | null = null -let apiDomainInstance: ApiPromise | null = null - export const activate = async (input?: NetworkInput): Promise => { // Get the first rpc urls for the network const rpcUrl = getNetworkRpcUrls(input) // Create the provider - provider = new WsProvider(rpcUrl[0]) + const provider = new WsProvider(rpcUrl[0]) // Create the API instance - apiInstance = await PolkadotApiPromise.create({ provider }) + const api = await ApiPromise.create({ provider }) + await api.isReady - return apiInstance + return api } export const activateDomain = async (input: DomainInput): Promise => { // Get the first rpc urls for the network const rpcUrl = getNetworkDomainRpcUrls(input) // Create the provider - domainProvider = new WsProvider(rpcUrl[0]) + const provider = new WsProvider(rpcUrl[0]) // Create the API instance - apiDomainInstance = await PolkadotApiPromise.create({ provider: domainProvider }) - - return apiDomainInstance -} + const api = await ApiPromise.create({ provider }) + await api.isReady -export const disconnect = async (): Promise => { - // Disconnect the API instance and the provider if they exist - if (apiInstance) { - await apiInstance.disconnect() - apiInstance = null - provider = null - } + return api } -export const disconnectDomain = async (): Promise => { - // Disconnect the API instance and the provider if they exist - if (apiDomainInstance) { - await apiDomainInstance.disconnect() - apiDomainInstance = null - domainProvider = null - } +export const disconnect = async (api: ApiPromise): Promise => { + // Disconnect the API instance and the provider + await api.disconnect() } diff --git a/packages/auto-utils/src/constants/wallet.ts b/packages/auto-utils/src/constants/wallet.ts index f4e94558..c72fae06 100644 --- a/packages/auto-utils/src/constants/wallet.ts +++ b/packages/auto-utils/src/constants/wallet.ts @@ -12,3 +12,5 @@ export const mockURIs = [ '//Ivy', '//Jacob', ] + +export const DEFAULT_SS58_FORMAT = 2254 diff --git a/packages/auto-utils/src/index.ts b/packages/auto-utils/src/index.ts index 8905004c..ccc7635f 100644 --- a/packages/auto-utils/src/index.ts +++ b/packages/auto-utils/src/index.ts @@ -1,5 +1,6 @@ // file: src/index.ts +export * from './address' export * from './api' export * from './crypto' export * from './network' @@ -10,3 +11,4 @@ export * from './wallet' export * from './constants' export * from './types' +export * from './utils' diff --git a/packages/auto-utils/src/types/api.ts b/packages/auto-utils/src/types/api.ts deleted file mode 100644 index 64f2d029..00000000 --- a/packages/auto-utils/src/types/api.ts +++ /dev/null @@ -1,3 +0,0 @@ -import type { ApiPromise as PolkadotApiPromise } from '@polkadot/api' - -export type ApiPromise = PolkadotApiPromise diff --git a/packages/auto-utils/src/types/event.ts b/packages/auto-utils/src/types/event.ts new file mode 100644 index 00000000..4c8966a0 --- /dev/null +++ b/packages/auto-utils/src/types/event.ts @@ -0,0 +1,3 @@ +import type { EventRecord } from '@polkadot/types/interfaces' + +export type { EventRecord } diff --git a/packages/auto-utils/src/types/extrinsic.ts b/packages/auto-utils/src/types/extrinsic.ts new file mode 100644 index 00000000..56bdd23e --- /dev/null +++ b/packages/auto-utils/src/types/extrinsic.ts @@ -0,0 +1,7 @@ +// file: src/types/extrinsic.ts + +import type { SubmittableExtrinsic, SubmittableModuleExtrinsics } from '@polkadot/api/types' +import type { Hash } from '@polkadot/types/interfaces' +import type { ISubmittableResult } from '@polkadot/types/types' + +export type { Hash, ISubmittableResult, SubmittableExtrinsic, SubmittableModuleExtrinsics } diff --git a/packages/auto-utils/src/types/index.ts b/packages/auto-utils/src/types/index.ts index 1589a64c..724d47f9 100644 --- a/packages/auto-utils/src/types/index.ts +++ b/packages/auto-utils/src/types/index.ts @@ -1,3 +1,8 @@ -export * from './api' +import type { BN } from '@polkadot/util' + +export * from './event' +export * from './extrinsic' export * from './network' export * from './wallet' + +export type { BN } diff --git a/packages/auto-utils/src/types/wallet.ts b/packages/auto-utils/src/types/wallet.ts index c6d71025..c962d3e9 100644 --- a/packages/auto-utils/src/types/wallet.ts +++ b/packages/auto-utils/src/types/wallet.ts @@ -1,5 +1,11 @@ // file: src/types/wallet.ts +import type { ApiPromise } from '@polkadot/api' +import type { AddressOrPair, Signer, SignerResult } from '@polkadot/api/types' +import type { InjectedAccountWithMeta } from '@polkadot/extension-inject/types' +import type { KeyringPair } from '@polkadot/keyring/types' +import type { DomainInput, NetworkInput } from './network' + export type Mnemonic = { mnemonic: string } @@ -13,3 +19,31 @@ export type AppName = { } export type MnemonicOrURI = Mnemonic | URI + +export type Wallet = { + keyringPair?: KeyringPair + injectedAccount?: InjectedAccountWithMeta + address: string + commonAddress: string +} + +export interface GeneratedWallet extends Wallet { + mnemonic: string +} + +export type ActivateWalletInput = (NetworkInput | DomainInput) & MnemonicOrURI & AppName + +export type WalletActivated = { + api: ApiPromise + accounts: InjectedAccountWithMeta[] & KeyringPair[] + address: string +} + +export type { + AddressOrPair, + ApiPromise, + InjectedAccountWithMeta, + KeyringPair, + Signer, + SignerResult, +} diff --git a/packages/auto-utils/src/utils/createType.ts b/packages/auto-utils/src/utils/createType.ts new file mode 100644 index 00000000..b996dc70 --- /dev/null +++ b/packages/auto-utils/src/utils/createType.ts @@ -0,0 +1,5 @@ +import type { ApiPromise } from '@polkadot/api' +import { createType } from '@polkadot/types' + +export const createAccountIdType = (api: ApiPromise, address: string) => + createType(api.registry, 'AccountId', address).toU8a() diff --git a/packages/auto-utils/src/utils/format.ts b/packages/auto-utils/src/utils/format.ts new file mode 100644 index 00000000..ca77bcd5 --- /dev/null +++ b/packages/auto-utils/src/utils/format.ts @@ -0,0 +1,3 @@ +import { stringToU8a, u8aToHex } from '@polkadot/util' + +export { stringToU8a, u8aToHex } diff --git a/packages/auto-utils/src/utils/index.ts b/packages/auto-utils/src/utils/index.ts new file mode 100644 index 00000000..c3654de5 --- /dev/null +++ b/packages/auto-utils/src/utils/index.ts @@ -0,0 +1,5 @@ +export * from './createType' +export * from './format' +export * from './ready' +export * from './sign' +export * from './verify' diff --git a/packages/auto-utils/src/utils/ready.ts b/packages/auto-utils/src/utils/ready.ts new file mode 100644 index 00000000..3f7f43ff --- /dev/null +++ b/packages/auto-utils/src/utils/ready.ts @@ -0,0 +1,3 @@ +import { cryptoWaitReady } from '@polkadot/util-crypto' + +export { cryptoWaitReady } diff --git a/packages/auto-utils/src/utils/sign.ts b/packages/auto-utils/src/utils/sign.ts new file mode 100644 index 00000000..ae85975e --- /dev/null +++ b/packages/auto-utils/src/utils/sign.ts @@ -0,0 +1,17 @@ +import { signatureVerify } from '@polkadot/util-crypto' +import type { Signer } from '../types/wallet' +import { u8aToHex } from './format' + +export const signMessage = async (signer: Signer, address: string, data: string) => { + if (!signer.signRaw) throw new Error('signRaw not available on the signer') + + return await signer.signRaw({ + address, + type: 'bytes', + data, + }) +} + +export const signingKey = async (publicKey: Uint8Array) => u8aToHex(publicKey) + +export { signatureVerify } diff --git a/packages/auto-utils/src/utils/verify.ts b/packages/auto-utils/src/utils/verify.ts new file mode 100644 index 00000000..e81360fb --- /dev/null +++ b/packages/auto-utils/src/utils/verify.ts @@ -0,0 +1,4 @@ +import { isHex } from '@polkadot/util' +import { isAddress } from '@polkadot/util-crypto' + +export { isAddress, isHex } diff --git a/packages/auto-utils/src/wallet.ts b/packages/auto-utils/src/wallet.ts index eec61529..3a8d9a2e 100644 --- a/packages/auto-utils/src/wallet.ts +++ b/packages/auto-utils/src/wallet.ts @@ -1,35 +1,45 @@ // file: src/wallet.ts -import type { ApiPromise } from '@polkadot/api' import { Keyring } from '@polkadot/api' import type { InjectedAccountWithMeta } from '@polkadot/extension-inject/types' import type { KeyringPair } from '@polkadot/keyring/types' -import { cryptoWaitReady } from '@polkadot/util-crypto' +import { mnemonicGenerate } from '@polkadot/util-crypto' +import { address } from './address' import { activate, activateDomain } from './api' import { defaultNetwork } from './constants/network' import { mockURIs } from './constants/wallet' -import type { AppName, DomainInput, Mnemonic, MnemonicOrURI, NetworkInput, URI } from './types' +import type { DomainInput, Mnemonic, MnemonicOrURI, NetworkInput, URI } from './types' +import type { ActivateWalletInput, GeneratedWallet, Wallet, WalletActivated } from './types/wallet' -export const setupWallet = async (input: MnemonicOrURI): Promise => { - await cryptoWaitReady() +export const setupWallet = (input: MnemonicOrURI): Wallet => { const keyring = new Keyring({ type: 'sr25519' }) - let pair: KeyringPair + let keyringPair: Wallet['keyringPair'] if ((input as URI).uri) { // Treat as uri - pair = keyring.addFromUri((input as URI).uri) + keyringPair = keyring.addFromUri((input as URI).uri) } else if ((input as Mnemonic).mnemonic) { // Treat as mnemonic - pair = keyring.addFromUri((input as Mnemonic).mnemonic) + keyringPair = keyring.addFromUri((input as Mnemonic).mnemonic) } else throw new Error('Invalid mnemonic or private key') - return pair + return { + keyringPair, + address: address(keyringPair.address), + commonAddress: keyringPair.address, + } } -export type ActivateWalletInput = (NetworkInput | DomainInput) & MnemonicOrURI & AppName -export type WalletActivated = { - api: ApiPromise - accounts: InjectedAccountWithMeta[] & KeyringPair[] +export const generateWallet = (): GeneratedWallet => { + const mnemonic = mnemonicGenerate() + const { keyringPair, address, commonAddress } = setupWallet({ mnemonic }) + + return { + mnemonic, + keyringPair, + address, + commonAddress, + } } export const activateWallet = async (input: ActivateWalletInput): Promise => { @@ -43,8 +53,8 @@ export const activateWallet = async (input: ActivateWalletInput): Promise