Skip to content

Commit

Permalink
Merge pull request #140 from palladians/feat/web-provider-fun
Browse files Browse the repository at this point in the history
Feat: Implement web-provider package
  • Loading branch information
teddyjfpender authored Feb 15, 2024
2 parents ed77d3d + 6b60bee commit 13d7f0e
Show file tree
Hide file tree
Showing 57 changed files with 2,442 additions and 378 deletions.
6 changes: 5 additions & 1 deletion apps/extension/manifest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ export default defineManifest(async (env) => ({
],
web_accessible_resources: [
{
resources: ['pallad_rpc.js'],
resources: ['pallad_rpc.js', 'prompt.html', 'prompt.js'],
matches: ['https://*/*']
}
],
host_permissions: [
'https://*/*'
// Add other URLs or patterns as needed
]
}))
1 change: 1 addition & 0 deletions apps/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@palladxyz/features": "^0.1.0",
"@palladxyz/key-management": "^0.0.1",
"@palladxyz/persistence": "^1.0.0",
"@palladxyz/web-provider": "^0.0.1",
"@plasmohq/messaging": "^0.6.1",
"buffer": "^6.0.3",
"dotenv": "^16.3.1",
Expand Down
20 changes: 18 additions & 2 deletions apps/extension/public/pallad_rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,24 @@ const callPalladAsync = ({ method, payload }) =>
})
const init = () => {
window.mina = {
request: ({ method, params }) =>
callPalladAsync({ method, payload: params })
request: async ({ method, params }) =>
await callPalladAsync({ method, payload: params }),
enable: async () => {
const response = await callPalladAsync({
method: 'enable',
payload: {}
})
return response
},
isPallad: true,
isConnected: async () => {
const response = await callPalladAsync({
method: 'isConnected',
payload: {}
})
return response
},

}
}
init()
47 changes: 47 additions & 0 deletions apps/extension/public/prompt.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f5f5f5;
color: #333;
}

#prompt-container {
background-color: #fff;
padding: 15px;
border-radius: 5px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}

#message {
margin-bottom: 20px;
}

#input-section, #confirm-section {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10px;
}

#user-input {
width: 70%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}

button {
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
background-color: #4285f4;
color: white;
min-width: 80px;
}

button:hover {
background-color: #357ae8;
}
24 changes: 24 additions & 0 deletions apps/extension/public/prompt.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>
<head>
<title>User Prompt</title>
<link rel="stylesheet" type="text/css" href="prompt.css">
</head>
<body>
<div id="prompt-container">
<p id="message"></p>

<div id="confirm-section" style="display: none;">
<button id="confirm-yes">Yes</button>
<button id="confirm-no">No</button>
</div>

<div id="input-section" style="display: none;">
<input type="text" id="user-input" />
<button id="submit-button">Submit</button>
<button id="cancel-button">Cancel</button>
</div>
</div>
<script src="prompt.js"></script>
</body>
</html>
53 changes: 53 additions & 0 deletions apps/extension/public/prompt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* eslint-disable */
document.addEventListener('DOMContentLoaded', () => {
const params = new URLSearchParams(window.location.search)
const message = params.get('message')
const inputType = params.get('inputType')
const windowId = parseInt(params.get('windowId'));

document.getElementById('message').textContent = message

const userInput = document.getElementById('user-input')
const confirmSection = document.getElementById('confirm-section')
const inputSection = document.getElementById('input-section')
const submitButton = document.getElementById('submit-button')

if (inputType === 'text' || inputType === 'password') {
userInput.setAttribute('type', inputType)
inputSection.style.display = 'flex' // Show input section
} else if (inputType === 'confirmation') {
confirmSection.style.display = 'flex' // Show confirmation section
}

submitButton.addEventListener('click', () => {
chrome.runtime.sendMessage({
windowId: windowId,
userInput: userInput.value
})
window.close()
})

document.getElementById('confirm-yes').addEventListener('click', () => {
chrome.runtime.sendMessage({
windowId: windowId,
userConfirmed: true
})
window.close()
})

document.getElementById('confirm-no').addEventListener('click', () => {
chrome.runtime.sendMessage({
windowId: windowId,
userConfirmed: false
})
window.close()
})

document.getElementById('cancel-button').addEventListener('click', () => {
chrome.runtime.sendMessage({
windowId: windowId,
userRejected: true
})
window.close()
})
})
87 changes: 85 additions & 2 deletions apps/extension/src/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,91 @@
import { MinaProvider } from '@palladxyz/web-provider'
import { onMessage } from 'webext-bridge/background'
import { runtime } from 'webextension-polyfill'

// options should be defined by user
const opts = {
projectId: 'test',
chains: ['Mina - Berkeley']
}
const provider = await MinaProvider.init(opts, [])

// Does this do anything?
runtime.onInstalled.addListener(() => {
onMessage('mina_chainId', () => {
return 'Berkeley'
onMessage('enable', async () => {
console.log('test enable method')
return await provider.enable()
})
})

// Register message handlers directly, not inside any other callback
onMessage('enable', async () => {
console.log('test enable method')
return await provider.enable()
})

onMessage('mina_setState', async () => {
console.log('test mina_setState method')
//return await provider.request({ method: 'mina_setState', params: data })
return 'TODO: implement mina_setState'
})

onMessage('mina_getState', async () => {
console.log('test mina_getState method')
//return await provider.request({ method: 'mina_getState' })
return {
credential: {
name: 'Pallad',
proof: {
publicInputs: [1],
publicOutputs: [100],
proof: '1hjdjf83...fjf'
}
}
}
})

onMessage('isConnected', async () => {
console.log('test isConnected method')
return await provider.isConnected()
})

onMessage('mina_chainId', async () => {
console.log('test mina_chainId method')
return await provider.request({ method: 'mina_chainId' })
})

onMessage('mina_accounts', async () => {
console.log('test mina_accounts method')
return await provider.request({ method: 'mina_accounts' })
})

onMessage('mina_sign', async (data) => {
console.log('test mina_sign method')
return await provider.request({ method: 'mina_sign', params: data })
})

onMessage('mina_signFields', async (data) => {
console.log('test mina_signFields method')
return await provider.request({ method: 'mina_signFields', params: data })
})

onMessage('mina_signTransaction', async (data) => {
console.log('test mina_signTransaction method')
return await provider.request({
method: 'mina_signTransaction',
params: data
})
})

onMessage('mina_getBalance', async () => {
console.log('test mina_getBalance method')
return await provider.request({ method: 'mina_getBalance' })
})

onMessage('mina_createNullifier', async (data) => {
console.log('test mina_createNullifier method')
return await provider.request({
method: 'mina_createNullifier',
params: data
})
})
4 changes: 3 additions & 1 deletion packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export const baseTsupConfig = {
export const baseVitestConfig = {
test: {
environment: 'happy-dom',
globals: true
globals: true,
testTimeout: 30000,
hookTimeout: 30000
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { zodResolver } from '@hookform/resolvers/zod'
import { ChainOperationArgs } from '@palladxyz/key-management'
import { Mina } from '@palladxyz/mina-core'
import { Multichain } from '@palladxyz/multi-chain-core'
import { useVault } from '@palladxyz/vault'
Expand Down Expand Up @@ -80,8 +81,13 @@ export const ConfirmTransactionForm = () => {
)
const getPassphrase = async () => Buffer.from(data.spendingPassword)
let signedTx
const operationArgs: ChainOperationArgs = {
operation: 'mina_signTransaction',
network: 'Mina',
networkType: 'testnet'
}
try {
signedTx = await sign(constructedTx as any, getPassphrase)
signedTx = await sign(constructedTx as any, operationArgs, getPassphrase)

Check warning on line 90 in packages/features/src/send/components/confirm-transaction-form.tsx

View workflow job for this annotation

GitHub Actions / Build and test

Unexpected any. Specify a different type
} catch (error: unknown) {
if (error instanceof Error) {
if (error.name === 'AuthenticationError')
Expand Down
2 changes: 1 addition & 1 deletion packages/multi-chain-core/test/Multichain/Provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Mina } from '@palladxyz/mina-core'

import { MultiChainProvider } from '../../src/Multichain/Provider'

describe('MultiChainProvider', () => {
describe.skip('MultiChainProvider', () => {
it('should create a mina provider and query the network', async () => {
const provider = new MultiChainProvider(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const nodeUrl =
const publicKey =
process.env['PUBLIC_KEY'] ||
'B62qkAqbeE4h1M5hop288jtVYxK1MsHVMMcBpaWo8qdsAztgXaHH1xq'

describe('Mina Explorer Account Info Provider (Functional)', () => {
// TODO: change this to local network
describe.skip('Mina Explorer Account Info Provider (Functional)', () => {
let provider: ReturnType<typeof MinaExplorer.createAccountInfoProvider>
let tokenMap: TokenIdMap

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const nodeUrl =
process.env['ARCHIVE_NODE_URL'] || 'https://berkeley.graphql.minaexplorer.com'
const publicKey =
process.env['PUBLIC_KEY'] ||
'B62qkAqbeE4h1M5hop288jtVYxK1MsHVMMcBpaWo8qdsAztgXaHH1xq'
'B62qjsV6WQwTeEWrNrRRBP6VaaLvQhwWTnFi4WP4LQjGvpfZEumXzxb'

describe('Mina Explorer Chain History Provider (Functional)', () => {
let provider: ReturnType<typeof MinaExplorer.createChainHistoryProvider>
Expand All @@ -33,6 +33,7 @@ describe('Mina Explorer Chain History Provider (Functional)', () => {
console.log('Mina Explorer Chain History Provider Response', response)
// TODO: investigate pagination
const transaction = response[0]
console.log('Mina Explorer Chain History Provider Response', transaction)

expect(transaction).toHaveProperty('amount')
expect(transaction).toHaveProperty('blockHeight')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ const publicKey =
const params = {
passphrase: 'passphrase'
}
const getPassphrase = async () => Buffer.from(params.passphrase)

const getPassphrase = async () => Buffer.from(params.passphrase)
// TODO: change this to local network
// TODO: use different mnemonic for this test -- else there are two duplicate transactions with the unified provider tests
describe.skip('Mina Explorer Submit Transaction Provider (Functional)', () => {
let provider: ReturnType<typeof MinaExplorer.createTxSubmitProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const nodeUrl =
'https://mina-berkeley.obscura.build/v1/bfce6350-4f7a-4b63-be9b-8981dec92050/graphql'
const publicKey =
process.env['PUBLIC_KEY'] ||
'B62qjsV6WQwTeEWrNrRRBP6VaaLvQhwWTnFi4WP4LQjGvpfZEumXzxb'
'B62qmHMUwiyNfv81NNTumW7Hv8SfRAGLXceGK3ZpyzXgmg2FLqmVhmA'
//'B62qjsV6WQwTeEWrNrRRBP6VaaLvQhwWTnFi4WP4LQjGvpfZEumXzxb'

describe('Obscura Account Info Provider (Functional)', () => {
let provider: ReturnType<typeof Obscura.createAccountInfoProvider>
Expand All @@ -16,7 +17,8 @@ describe('Obscura Account Info Provider (Functional)', () => {
beforeEach(() => {
provider = Obscura.createAccountInfoProvider(nodeUrl)
tokenMap = {
MINA: '1'
MINA: '1',
WETH: 'xaU5YZyGmvztCyXRFXnfrsp5E8wUVXmtyXBYrw5PF4WXGwPvme'
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Unified Account Info Provider (Functional)', () => {
}
})

describe('Mina Explorer Configuration', () => {
describe.skip('Mina Explorer Configuration', () => {
beforeEach(() => {
configMinaExplorer = {
nodeEndpoint: {
Expand Down
Loading

0 comments on commit 13d7f0e

Please sign in to comment.