Skip to content

Commit

Permalink
fix: remove programatic private key export and add more info to the U…
Browse files Browse the repository at this point in the history
…I elements

closes #67
  • Loading branch information
hugomrdias committed Sep 7, 2023
1 parent eec9d17 commit c88a9ee
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 144 deletions.
2 changes: 1 addition & 1 deletion examples/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"filsnap-adapter-react": "workspace:^",
"iso-base": "^2.0.1",
"iso-filecoin": "^3.0.0",
"metamask-testing-tools": "^1.1.5",
"metamask-testing-tools": "^1.1.6",
"preact": "^10.17.1",
"react-hook-form": "^7.46.1",
"viem": "^1.10.4",
Expand Down
14 changes: 2 additions & 12 deletions examples/demo/src/components/rpc.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ const Account = () => {
const [publicKey, setPublicKey] = useState(
/** @type {string | undefined| null} */ (undefined)
)
const [privateKey, setPrivateKey] = useState(
/** @type {string | undefined | null} */ (undefined)
)

const handleGetAddress = async () => {
const addressResponse = await snap?.getAddress()
Expand Down Expand Up @@ -52,8 +49,7 @@ const Account = () => {

const handleExportPrivateKey = async () => {
const response = await snap?.exportPrivateKey()
if (response) {
setPrivateKey(response.result)
if (response && response.error) {
setError(response.error?.message)
}
}
Expand Down Expand Up @@ -90,14 +86,9 @@ const Account = () => {
</button>
<code data-testid="public-key-result">{publicKey}</code>
<br />
<button
data-testid="get-private-key"
onClick={handleExportPrivateKey}
disabled={isLoading}
>
<button data-testid="get-private-key" onClick={handleExportPrivateKey}>
Get Private Key
</button>
<code data-testid="private-key-result">{privateKey}</code>

<br />
<button
Expand All @@ -107,7 +98,6 @@ const Account = () => {
>
Add FEVM Mainnet
</button>
<code data-testid="private-key-result">{privateKey}</code>
</div>
)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"devDependencies": {
"@playwright/test": "^1.37.1",
"@types/node": "^20.5.9",
"metamask-testing-tools": "^1.1.5",
"metamask-testing-tools": "^1.1.6",
"typescript": "5.2.2"
},
"publishConfig": {
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"chai-as-promised": "^7.1.1",
"chai-parentheses": "^0.0.2",
"chai-subset": "^1.6.0",
"metamask-testing-tools": "^1.1.5",
"metamask-testing-tools": "^1.1.6",
"mocha": "^10.2.0",
"playwright-test": "^12.3.0",
"rollup": "^3.29.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/filecoin-project/filsnap.git"
},
"source": {
"shasum": "GXC5VmE+TTVTGTrdmpgF4IOYqRDqNOjUec5htMvfVUI=",
"shasum": "oIh502LTt8CXdrbV6xqWD72sHQctPHEjPMHD2hjMuW4=",
"location": {
"npm": {
"filePath": "dist/snap.js",
Expand Down
26 changes: 19 additions & 7 deletions packages/snap/src/rpc/export-private-key.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { heading, panel } from '@metamask/snaps-ui'
import { copyable, heading, panel, text } from '@metamask/snaps-ui'
import type { SnapContext, SnapResponse } from '../types'
import { serializeError, snapDialog } from '../utils'
import { base64pad } from 'iso-base/rfc4648'
import { parseDerivationPath } from 'iso-filecoin/utils'

// Types
export type ExportPrivateKeyResponse = SnapResponse<string>
export type ExportPrivateKeyResponse = SnapResponse<boolean>

export interface ExportPrivateKeyRequest {
method: 'fil_exportPrivateKey'
Expand All @@ -19,16 +20,27 @@ export interface ExportPrivateKeyRequest {
export async function exportPrivateKey(
ctx: SnapContext
): Promise<ExportPrivateKeyResponse> {
const { account } = parseDerivationPath(ctx.config.derivationPath)
const conf = await snapDialog(ctx.snap, {
type: 'confirmation',
content: panel([heading(`Do you want to export your private key?`)]),
content: panel([
heading(`Do you want to export your private key?`),
text(
'Warning: Never disclose this key. Anyone with your private keys can steal any assets held in your account.'
),
]),
})

if (conf) {
return {
result: base64pad.encode(ctx.account.privateKey),
error: null,
}
await snapDialog(ctx.snap, {
type: 'alert',
content: panel([
text(`Private key for Account ${account}`),
copyable(base64pad.encode(ctx.account.privateKey)),
]),
})

return { result: true, error: null }
}
return serializeError('User denied private key export')
}
15 changes: 0 additions & 15 deletions packages/snap/test/e2e/mainnet.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createFixture } from 'metamask-testing-tools'
import { type ExportPrivateKeyResponse } from '../../src/rpc/export-private-key'
import { type GetMessagesResponse } from '../../src/rpc/get-messages'
import {
type GetAddressResponse,
Expand Down Expand Up @@ -42,20 +41,6 @@ test.describe('filsnap mainnet api', () => {
)
})

test('should get private key', async ({ metamask, page }) => {
metamask.on('notification', (page) => {
void page.getByRole('button').filter({ hasText: 'Approve' }).click()
})
const { result } = await metamask.invokeSnap<ExportPrivateKeyResponse>({
request: {
method: 'fil_exportPrivateKey',
},
page,
})

expect(result).toBe('IwTEHN6u6qxR76Lf8nkIm/JKLl9lRUAL0ulE80wOl/M=')
})

test('should get messages', async ({ metamask, page }) => {
const { result } = await metamask.invokeSnap<GetMessagesResponse>({
request: {
Expand Down
9 changes: 6 additions & 3 deletions packages/snap/test/e2e/methods.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,14 @@ test.describe('filsnap testnet', () => {
page,
})

const dialog = await metamask.waitForDialog('confirmation')
await dialog.getByRole('button').filter({ hasText: 'Approve' }).click()
const dialog1 = await metamask.waitForDialog('confirmation')
await dialog1.getByRole('button').filter({ hasText: 'Approve' }).click()

const dialog2 = await metamask.waitForDialog('confirmation')
await dialog2.locator('.confirmation-footer__actions > button').click()
const { result } = await privateKey

expect(result).toBe('oUultedTWcsLGiRYLrBi/d7WoVvAxFedERBPTNKgBTo=')
expect(result).toBe(true)
})

test('should get messages', async ({ metamask, page }) => {
Expand Down
18 changes: 13 additions & 5 deletions packages/snap/test/unit/rpc/export-seed.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { expect } from '../../utils'
import { exportPrivateKey } from '../../../src/rpc/export-private-key'
import { mockSnapProvider } from '../wallet-mock'
import { testPrivateKeyBase64 } from './fixtures'
import { getKeyPair } from '../../../src/keypair'
import * as Constants from '../../../src/constants'

describe('Test rpc handler function: exportSeed', function () {
const walletStub = mockSnapProvider()
Expand All @@ -16,21 +16,29 @@ describe('Test rpc handler function: exportSeed', function () {
walletStub.prepareFoKeyPair()
const account = await getKeyPair(walletStub)
// @ts-expect-error - test code
const result = await exportPrivateKey({ snap: walletStub, account })
const result = await exportPrivateKey({
snap: walletStub,
account,
config: Constants.mainnetConfig,
})

expect(walletStub.rpcStubs.snap_dialog).to.have.been.calledOnce()
expect(walletStub.rpcStubs.snap_dialog).to.have.been.calledTwice()
expect(walletStub.rpcStubs.snap_manageState).to.have.been.calledOnce()
expect(walletStub.rpcStubs.snap_getBip44Entropy).to.have.been.calledOnce()

expect(result.result).to.be.eq(testPrivateKeyBase64)
expect(result.result).to.be.eq(true)
})

it('should not return seed on negative prompt confirmation', async function () {
walletStub.rpcStubs.snap_dialog.resolves(false)
walletStub.prepareFoKeyPair()
const account = await getKeyPair(walletStub)
// @ts-expect-error - test code
const result = await exportPrivateKey({ snap: walletStub, account })
const result = await exportPrivateKey({
snap: walletStub,
account,
config: Constants.mainnetConfig,
})

expect(walletStub.rpcStubs.snap_dialog).to.have.been.calledOnce()
expect(result.result).to.be.null()
Expand Down
Loading

0 comments on commit c88a9ee

Please sign in to comment.