Skip to content

Commit

Permalink
fix(wallet-connection): more typing and package updates
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Sep 25, 2021
1 parent 81cd2ce commit 623c5e4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 12 deletions.
19 changes: 19 additions & 0 deletions packages/wallet-connection/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// This file can contain .js-specific Typescript compiler config.
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",

"noEmit": true,
/*
// The following flags are for creating .d.ts files:
"noEmit": false,
"declaration": true,
"emitDeclarationOnly": true,
*/
"downlevelIteration": true,
"strictNullChecks": true,
"moduleResolution": "node",
},
"include": ["src/**/*.js", "exported.js"],
}
22 changes: 14 additions & 8 deletions packages/wallet-connection/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@
"name": "@agoric/wallet-connection",
"description": "Webcomponent agoric-wallet-connection following open-wc recommendations",
"license": "MIT",
"author": "agoric-wallet-connection",
"version": "0.0.0",
"author": "Agoric",
"version": "0.1.0",
"main": "index.js",
"module": "index.js",
"scripts": {
"analyze": "cem analyze --litelement",
"build": "exit 0",
"start": "web-dev-server --port 8100",
"lint-check": "yarn lint",
"lint": "eslint --ext .js,.html . --ignore-path .gitignore",
"lint-fix": "eslint --ext .js,.html . --fix --ignore-path .gitignore",
"test": "web-test-runner --coverage",
"test:watch": "web-test-runner --watch"
"test:c8": "exit 0",
"test:xs": "exit 0",
"test:watch": "web-test-runner --watch",
"pretty-fix": "prettier --write '**/*.js'",
"pretty-check": "prettier --check '**/*.js'",
"lint-fix": "yarn lint:eslint --fix && yarn lint:types",
"lint-check": "yarn lint",
"lint": "yarn lint:types && yarn lint:eslint",
"lint:types": "tsc -p jsconfig.json",
"lint:eslint": "eslint --ext .js,.html . --fix --ignore-path .gitignore",
"analyze": "cem analyze --litelement",
"start": "web-dev-server --port 8100"
},
"dependencies": {
"@agoric/assert": "^0.3.11",
Expand Down
9 changes: 8 additions & 1 deletion packages/wallet-connection/src/AgoricIframeMessenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// @ts-check
import { LitElement, html } from 'lit';

import { assert } from '@agoric/assert';

export class AgoricIframeMessenger extends LitElement {
static get properties() {
return { src: { type: String } };
Expand All @@ -11,6 +13,7 @@ export class AgoricIframeMessenger extends LitElement {
super();
this.src = '';
this._contentWindow = null;
this._origin = null;

// Need to bind since these aren't declarative event handlers.
this.send = this.send.bind(this);
Expand Down Expand Up @@ -40,8 +43,10 @@ export class AgoricIframeMessenger extends LitElement {
}

firstUpdated() {
const iframe = this.renderRoot.querySelector('iframe');
assert(iframe);
// Detect the content window of the iframe to verify message sources.
this._contentWindow = this.renderRoot.querySelector('iframe').contentWindow;
this._contentWindow = iframe.contentWindow;
}

_onLoad(event) {
Expand Down Expand Up @@ -71,6 +76,8 @@ export class AgoricIframeMessenger extends LitElement {
}

send(data) {
assert(this._contentWindow);
assert(this._origin);
this._contentWindow.postMessage(data, this._origin);
}
}
13 changes: 10 additions & 3 deletions packages/wallet-connection/src/AgoricWalletConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export class AgoricWalletConnection extends LitElement {
onOpen(ev) {
console.log(this.state, 'open', ev);
if (this.state === 'bridged') {
assert(this._captp);
this._bridgePK.resolve(this._captp.getBootstrap());
}
}
Expand Down Expand Up @@ -145,6 +146,7 @@ export class AgoricWalletConnection extends LitElement {
console.log(this.state, 'bridge received', ev);
const { data } = ev.detail;
if (data && typeof data.type === 'string' && data.type.startsWith('CTP_')) {
assert(this._captp);
this._captp.dispatch(data);
}
}
Expand Down Expand Up @@ -175,8 +177,11 @@ export class AgoricWalletConnection extends LitElement {

_getBridgeURL() {
const { location, suggestedDappPetname } = this.service.context;
assert(location);
const url = new URL(location);
url.searchParams.append('suggestedDappPetname', suggestedDappPetname);
if (suggestedDappPetname) {
url.searchParams.append('suggestedDappPetname', suggestedDappPetname);
}
return url.href;
}

Expand All @@ -203,8 +208,8 @@ export class AgoricWalletConnection extends LitElement {
default:
}

/** @type {import('lit-html').TemplateResult} */
let backend = null;
/** @type {import('lit-html').TemplateResult<1>} */
let backend;
if (src) {
backend = html`
<agoric-iframe-messenger
Expand All @@ -214,6 +219,8 @@ export class AgoricWalletConnection extends LitElement {
@error=${this.onError}
></agoric-iframe-messenger>
`;
} else {
backend = html``;
}

return html`
Expand Down
4 changes: 4 additions & 0 deletions packages/wallet-connection/src/states.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
* @typedef {Object} Context
* @property {any?} error
* @property {string?} location
* @property {string?} suggestedDappPetname
*/

/**
Expand All @@ -20,6 +21,9 @@ import {
*/
const reduce = rawReduce;

/**
* @returns {Context}
*/
const initialContext = () => ({
error: null,
location: null,
Expand Down

0 comments on commit 623c5e4

Please sign in to comment.