Skip to content

Commit

Permalink
don't require pkcs key format; simplify key loading and add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
sidvishnoi committed Sep 9, 2024
1 parent 9134d36 commit 97c4b29
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 47 deletions.
48 changes: 48 additions & 0 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Contribution Guide

## End-to-end Tests

### How to run

To run end-to-end tests with chromium, run `pnpm test:e2e` in terminal.

**Before you begin**, you need to setup some environment variables/secrets in `tests/.env`.

1. Copy `tests/.env.example` to `tests/.env`
2. Update `tests/.env` with your secrets.

| Environment Variable | Description | Is secret? |
| ---------------------------- | ----------------------------------------------------------- | ---------- |
| `WALLET_URL_ORIGIN` | URL of the wallet (e.g. https://rafiki.money) | No |
| `WALLET_USERNAME` | Login email for the wallet | No |
| `WALLET_PASSWORD` | Login password for the wallet | Yes |
| `CONNECT_WALLET_ADDRESS_URL` | Your wallet address that will be connected to extension | No |
| `CONNECT_KEY_ID` | ID of the key that will be connected to extension (UUID v4) | No |
| `CONNECT_PRIVATE_KEY` | Private key (hex-encoded Ed25519 private key) | Yes |
| `CONNECT_PUBLIC_KEY` | Public key (base64-encoded Ed25519 public key) | No |

To get the `CONNECT_KEY_ID`, `CONNECT_PRIVATE_KEY` and `CONNECT_PUBLIC_KEY`:

1. Load the extension in browser (via `chrome://extensions/`)
- Once the extension is loaded, it'll generate a key-pair that we will need to connect with our wallet.
1. Inspect service worker with "Inspect views service worker"
1. Run following in devtools console to copy keys to your clipboard, and paste it in `tests/.env`:
```js
// 1. Gets generated keys from extension storage.
// 2. Converts result to `CONNECT_{X}="VAL"` format for use in .env file.
// 3. Copies result to clipboard.
copy(
Object.entries(
await chrome.storage.local.get(['privateKey', 'publicKey', 'keyId']),
)
.map(
([k, v]) =>
`CONNECT_${k.replace(/([A-Z])/g, '_$1').toUpperCase()}="${v}"`,
)
.join('\n'),
);
```

Then copy `CONNECT_PUBLIC_KEY` key to https://rafiki.money/settings/developer-keys under your wallet address.

Now you're ready to run the tests.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"format:fix": "prettier . --write --cache --cache-location='node_modules/.cache/prettiercache' --log-level=warn",
"typecheck": "tsc --noEmit",
"test": "jest --maxWorkers=2 --passWithNoTests",
"test:e2e": "playwright test",
"test:e2e": "playwright test --project=chromium",
"test:ci": "pnpm test -- --reporters=default --reporters=github-actions"
},
"dependencies": {
Expand Down Expand Up @@ -76,7 +76,6 @@
"jest-chrome": "^0.8.0",
"jest-environment-jsdom": "^29.7.0",
"jest-transform-stub": "^2.0.0",
"jose": "^5.8.0",
"postcss": "^8.4.41",
"prettier": "^3.3.3",
"prettier-plugin-tailwindcss": "^0.6.6",
Expand Down
8 changes: 0 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions tests/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ WALLET_PASSWORD=some-password

# To connect extension to wallet
CONNECT_WALLET_ADDRESS_URL="https://ilp.rafiki.money/something"
# ...generate a key-pair at https://rafiki.money/settings/developer-keys
# ...we'll make extension use this key-pair consistently

# We'll make extension use this key-pair consistently. See docs/CONTRIBUTING.md
CONNECT_KEY_ID=uuid-v4-key-id
CONNECT_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----
PUBLIC-KEY-CONTENTS
-----END PUBLIC KEY-----"
CONNECT_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----
PRIVATE-KEY-CONTENTS
-----END PRIVATE KEY-----"
CONNECT_PUBLIC_KEY="BASE-64-KEY=="
CONNECT_PRIVATE_KEY="hex-encodedkey"

Check warning on line 15 in tests/.env.example

View workflow job for this annotation

GitHub Actions / Lint

Unknown word (encodedkey)
38 changes: 9 additions & 29 deletions tests/fixtures/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,43 +224,23 @@ export function getExtensionId(browserName: string, background: Worker) {
export type KeyInfo = {
/** UUID-v4 */
keyId: string;
/** Format: -----BEGIN PRIVATE KEY----- */
/** Format: Hex encoded Ed25519 private key */
privateKey: string;
/** Format: -----BEGIN PUBLIC KEY----- */
/** Format: Base64 encoded Ed25519 public key */
publicKey: string;
};

export async function loadKeysToExtension(
background: Background,
keyInfo: KeyInfo,
) {
const { importSPKI, importPKCS8, exportJWK } = await import('jose');
const { bytesToHex } = await import('@noble/hashes/utils');

const keyId = keyInfo.keyId;
const privateKey = await importPKCS8(keyInfo.privateKey, 'Ed25519').then(
(keyLike) => {
const bytes = (keyLike as KeyObject).export({
type: 'pkcs8',
format: 'der',
});
return bytesToHex(bytes);
},
);
const publicKey = await importSPKI(keyInfo.publicKey, 'Ed25519')
.then((r) => exportJWK(r))
.then((r) => btoa(JSON.stringify(r)));

await background.evaluate(
async ({ privateKey, publicKey, keyId }) => {
return await chrome.storage.local.set({
privateKey,
publicKey,
keyId,
});
},
{ keyId, privateKey, publicKey },
);
await background.evaluate(async ({ privateKey, publicKey, keyId }) => {
return await chrome.storage.local.set({
privateKey,
publicKey,
keyId,
});
}, keyInfo);

const res = await background.evaluate(() => {
return chrome.storage.local.get(['privateKey', 'publicKey', 'keyId']);
Expand Down

0 comments on commit 97c4b29

Please sign in to comment.