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

getWallet no longer returns a nested signer #263

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 8 additions & 17 deletions src/keys/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
keyFromPath,
keyExtractPath,
} from '@polkadot/util-crypto'
import { Keypair } from '@polkadot/util-crypto/types'
import { Keyring } from '@polkadot/keyring'
import { KeyringPair } from '@polkadot/keyring/types'
import { hexToU8a } from '@polkadot/util'
Expand Down Expand Up @@ -36,30 +37,20 @@ export function isValidPair (pair: Signer): boolean {
* @returns A function that takes a `Signer` or seed string and returns a Promise resolving to an object containing the wallet and its associated `Signer`.
*/

function setupGetWallet (): (input: Signer | string) => Promise<{ wallet: KeyringPair, pair: Signer }> | undefined {
function setupGetWallet (): (input: string) => Promise<Signer> | undefined {
const keyring = new Keyring({ type: 'sr25519' })

return async (input: Signer | string) => {
let processedPair: Signer
return async (input: Keypair | string) => {

// do a string typecheck
if (typeof input === 'string') {
await cryptoWaitReady()
const seed = hexToU8a(input)
const sr25519Pair = sr25519PairFromSeed(seed)
const keyringPair = keyring.addFromPair(sr25519Pair)
processedPair = { wallet: keyringPair, pair: sr25519Pair }
} else if (input && 'pair' in input) {
// If input is already a Signer object use it
processedPair = input
const pair = sr25519PairFromSeed(seed)
const wallet = keyring.addFromPair(pair)
return { wallet, pair }
} else {
return undefined
}

const wallet = keyring.addFromPair(processedPair.pair)
return {
wallet,
pair: processedPair,
throw new Error('input is not a string')
}
}
}
Expand All @@ -71,7 +62,7 @@ function setupGetWallet (): (input: Signer | string) => Promise<{ wallet: Keyrin
* @returns A Promise resolving to an object containing the wallet and its associated `Signer`, or undefined if the input is invalid.
*/

export const getWallet: (pair: Signer | string) => Promise<{ wallet: KeyringPair, pair: Signer }> | undefined = setupGetWallet()
export const getWallet: (pair: Signer | string) => Promise<{ wallet: KeyringPair, pair: Keypair }> | undefined = setupGetWallet()

/**
* Generates a new mnemonic phrase or derives a wallet from an existing mnemonic and an optional derivation path.
Expand Down
33 changes: 2 additions & 31 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ describe('Core Tests', () => {
const signer = await getWallet(charlieStashSeed)

const entropyAccount: EntropyAccount = {
sigRequestKey: signer.pair,
programModKey: signer.pair
sigRequestKey: signer,
programModKey: signer
}

await sleep(30000)
Expand Down Expand Up @@ -162,37 +162,8 @@ describe('Core Tests', () => {

expect(unauthorizedErrorCaught).toBeTruthy()

// signing attempts should fail cause we haven't set constraints yet
/* const no_constraint: any = await entropy.sign({
sigRequestHash: keccak256(ethers.utils.serializeTransaction(whitelisted_test_tx_req)),
freeTx: false,
retries: 3
})
expect(no_constraint.length).toBe(0)

// set user's constraints on-chain
const charlieStashEntropy = new Entropy({
seed: charlieStashSeed
})

// signing should fail with a non-whitelisted tx requests
const wrong_constraint: any = await entropy.sign({
sigRequestHash: keccak256(ethers.utils.serializeTransaction(non_whitelisted_test_tx_req)),
freeTx: false,
retries: 3
})
expect(wrong_constraint.length).toBe(0)
*/
// signing should work for whitelisted tx requests
console.log("signing test")

// const serializedTx = await preSign(whitelisted_test_tx_req)

// const signature: Uint8Array = await entropy.sign({
// sigRequestHash: serializedTx,
// })



const signature = await entropy.signTransaction({txParams: whitelisted_test_tx_req, type: 'eth'}) as string

Expand Down
4 changes: 2 additions & 2 deletions tests/programs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ describe('Programs Tests', () => {

const signer = await getWallet(charlieStashSeed)
const entropyAccount: EntropyAccount = {
sigRequestKey: signer.pair,
programModKey: signer.pair
sigRequestKey: signer,
programModKey: signer
}

await sleep(30000)
Expand Down
4 changes: 2 additions & 2 deletions tests/register.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ describe('Register Tests', () => {
const signer = await getWallet(charlieStashSeed)

const entropyAccount: EntropyAccount = {
sigRequestKey: signer.pair,
programModKey: signer.pair
sigRequestKey: signer,
programModKey: signer
}

await sleep(30000)
Expand Down
Loading