Skip to content

Commit

Permalink
feat(first implementation of user-prompt)
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyjfpender committed Oct 1, 2023
1 parent 7b32f38 commit 3ad1619
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
33 changes: 30 additions & 3 deletions packages/web-provider/src/Mina/MinaProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,33 +89,46 @@ interface MinaProviderOptions {
rpcMap?: MinaRpcMap
pairingTopic?: string
projectId: string
showUserPrompt?: (message: string) => Promise<boolean>
}

interface RequestArguments {
method: RpcMethod
params?: any[] | object
}

async function showUserPrompt(message: string): Promise<boolean> {
return new Promise((resolve) => {
const userResponse = window.confirm(message)
resolve(userResponse)
})
}

export class MinaProvider implements IMinaProvider {
public events = new EventEmitter()
public accounts: string[] = []
public chainId = 'Add Chain ID here'
public signer: MinaWalletWrapper // I think the signer is something other than the wallet, like a wrapper around the wallet, but I'm not sure

private userPrompt: (message: string) => Promise<boolean> // maybe this is the right place for this?

protected rpc: MinaRpcConfig

constructor(wallet: MinaWalletImpl) {
constructor(wallet: MinaWalletImpl, opts: MinaProviderOptions) {
// Initialization logic
this.signer = new MinaWalletWrapper(wallet)
this.rpc = {} as MinaRpcConfig

// Use provided userPrompt function or default to the actual implementation
this.userPrompt = opts.showUserPrompt || showUserPrompt
}

static async init(
opts: MinaProviderOptions,
wallet: MinaWalletImpl
): Promise<MinaProvider> {
const provider = new MinaProvider(wallet)
await provider.initialize(opts)
const provider = new MinaProvider(wallet, opts)
await provider.initialize(opts) // I should include this in the constructor
return provider
}

Expand All @@ -140,6 +153,12 @@ export class MinaProvider implements IMinaProvider {
public async enable(): Promise<string[]> {
// Implement the logic to prompt the user to connect to the wallet
// For example, you could open a modal and wait for the user to click 'Connect'
// Step 0: Prompt user for confirmation
const userConfirmed = await this.userPrompt('Do you want to connect?')
if (!userConfirmed) {
// should this emit an error event?
throw new Error('User denied connection.')
}
await this.connect()
// Once the user has connected, emit an 'accountsChanged' event
this.events.emit('accountsChanged', this.accounts)
Expand Down Expand Up @@ -185,6 +204,14 @@ export class MinaProvider implements IMinaProvider {
}

public async request<T = unknown>(args: RequestArguments): Promise<T> {
// Prompt user for confirmation based on the method type
const userConfirmed = await this.userPrompt(
`Do you want to execute ${args.method}?`
)
if (!userConfirmed) {
// should this emit an error event?
throw new Error('User denied the request.')
}
return await this.signer.request(args, this.chainId)
}

Expand Down
10 changes: 9 additions & 1 deletion packages/web-provider/test/Mina/MinaProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,21 @@ describe('MinaWalletImpl', () => {
const derivedCredential = wallet.getCurrentWallet()
expect(derivedCredential).toBeDefined()

const mockUserPrompt: (message: string) => Promise<boolean> = async (
message: string
) => {
console.log('message:', message)
return true // Always confirm for testing purposes
}

// create MinaProvider
const provider = await MinaProvider.init(
{
chains: [Mina.Networks.BERKELEY],
optionalChains: [],
rpcMap: {}, // TODO
projectId: 'pallad'
projectId: 'pallad',
showUserPrompt: mockUserPrompt
},
wallet
)
Expand Down

0 comments on commit 3ad1619

Please sign in to comment.