Skip to content

Commit 4a61c04

Browse files
Byron HamblyCifko
Byron Hambly
andauthored
feat: create tari web extension application stub (#3535)
* feat: create tari web extension application Co-authored-by: Martin Stefcek <gcifko@gmail.com> * scaffold * docs: minor * wasm hello world Co-authored-by: Martin Stefcek <gcifko@gmail.com>
1 parent f1c1eae commit 4a61c04

24 files changed

+7654
-2
lines changed

Cargo.lock

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist/

applications/tari_web_extension/package-lock.json

+7,434
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "@tari/web-extension",
3+
"version": "0.1.0",
4+
"description": "Tari Web Extension",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "jest",
8+
"build": "webpack",
9+
"serve": "webpack serve"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/tari-project/tari.git"
14+
},
15+
"keywords": [
16+
"tari"
17+
],
18+
"author": "The Tari Development Community",
19+
"license": "MIT",
20+
"bugs": {
21+
"url": "https://github.com/tari-project/tari/issues"
22+
},
23+
"homepage": "https://github.com/tari-project/tari#readme",
24+
"dependencies": {
25+
"wallet-grpc-client": "file:../../clients/wallet_grpc_client"
26+
},
27+
"devDependencies": {
28+
"@wasm-tool/wasm-pack-plugin": "1.5.0",
29+
"html-webpack-plugin": "^5.3.2",
30+
"jest": "^27.3.1",
31+
"text-encoding": "^0.7.0",
32+
"webpack": "^5.49.0",
33+
"webpack-cli": "^4.7.2",
34+
"webpack-dev-server": "^3.11.2"
35+
}
36+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# tari web extension
2+
3+
- `npm i`
4+
- Run tari console wallet with grpc
5+
- `npm test`
6+
7+
todo:
8+
9+
- instructions for how to install and use the web extension
10+
- js bundling
11+
- ux flow
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
console.log("background.js execute");
2+
chrome.runtime.onConnect.addListener(function (port) {
3+
if (port.name === "tari-port") {
4+
port.onMessage.addListener(function (payload) {
5+
console.log(payload);
6+
});
7+
}
8+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
console.log("content.js executed");
2+
3+
function script() {
4+
class Tari {
5+
constructor() {
6+
console.log("initiating tari");
7+
}
8+
login() {
9+
window.postMessage(
10+
{ type: "TARI", payload: { user: "username", password: "password" } },
11+
"*"
12+
);
13+
}
14+
}
15+
window.tari = new Tari();
16+
console.log("injecting tari");
17+
}
18+
19+
function inject(fn) {
20+
const script = document.createElement("script");
21+
script.text = `(${fn.toString()})();`;
22+
document.documentElement.appendChild(script);
23+
}
24+
25+
inject(script);
26+
27+
let port = chrome.runtime.connect({ name: "tari-port" });
28+
window.addEventListener(
29+
"message",
30+
(event) => {
31+
if (event.source != window) {
32+
return;
33+
}
34+
if (event.data.type && event.data.type == "TARI") {
35+
console.log("content received : ", event.data.payload);
36+
port.postMessage(event.data.payload);
37+
}
38+
},
39+
false
40+
);
Loading
Loading
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Note that a dynamic `import` statement here is required due to
2+
// webpack/webpack#6615, but in theory `import { greet } from './pkg';`
3+
// will work here one day as well!
4+
const rust = import("./key_manager");
5+
6+
rust.then((m) => m.greet("World!")).catch(console.error);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// requires a running wallet with grpc enabled
2+
const { Client } = require("wallet-grpc-client");
3+
4+
const host = process.env.HOST || "127.0.0.1";
5+
const port = process.env.PORT || "18143";
6+
const address = `${host}:${port}`;
7+
const wallet = Client.connect(address);
8+
9+
test("identify", async () => {
10+
const { public_key, public_address, node_id } = await wallet.identify();
11+
12+
expect(public_key).toBeDefined();
13+
expect(public_key.length).toBe(64);
14+
15+
expect(public_address).toBeDefined();
16+
expect(typeof public_address).toEqual("string");
17+
18+
expect(node_id).toBeDefined();
19+
expect(node_id.length).toBe(26);
20+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"typeAcquisition": {
3+
"include": [
4+
"chrome"
5+
]
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "Tari Web Extension",
3+
"description": "Tari Web Extension",
4+
"version": "0.1.0",
5+
"manifest_version": 2,
6+
"icons": {
7+
"16": "./icons/icon-16x16.png",
8+
"32": "./icons/icon-32x32.png",
9+
"48": "./icons/icon-48x48.png",
10+
"128": "./icons/icon-128x128.png"
11+
},
12+
"background": {
13+
"scripts": ["./background.js"]
14+
},
15+
"content_scripts": [
16+
{
17+
"matches": ["file://*/*", "http://*/*", "https://*/*"],
18+
"run_at": "document_start",
19+
"js": ["content.js"]
20+
}
21+
],
22+
"options_page": "./options.html",
23+
"browser_action": {
24+
"default_popup": "popup.html"
25+
},
26+
"permissions": ["https://*/*"],
27+
"externally_connectable": {
28+
"ids": ["*"],
29+
"accepts_tls_channel_id": false
30+
}
31+
}

applications/tari_web_extension/src/options.html

Whitespace-only changes.

applications/tari_web_extension/src/popup.html

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const path = require("path");
2+
const HtmlWebpackPlugin = require("html-webpack-plugin");
3+
const webpack = require("webpack");
4+
const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin");
5+
const KEY_MANAGER_PATH = "../../base_layer/key_manager/";
6+
7+
module.exports = {
8+
entry: "./src/index.js",
9+
output: {
10+
path: path.resolve(__dirname, "dist"),
11+
filename: "index.js",
12+
},
13+
plugins: [
14+
new HtmlWebpackPlugin(),
15+
new WasmPackPlugin({
16+
crateDirectory: path.resolve(__dirname, KEY_MANAGER_PATH),
17+
outDir: path.resolve(__dirname, `${KEY_MANAGER_PATH}/pkg`), // https://github.com/wasm-tool/wasm-pack-plugin/issues/93
18+
}),
19+
// Have this example work in Edge which doesn't ship `TextEncoder` or
20+
// `TextDecoder` at this time.
21+
new webpack.ProvidePlugin({
22+
TextDecoder: ["text-encoding", "TextDecoder"],
23+
TextEncoder: ["text-encoding", "TextEncoder"],
24+
}),
25+
],
26+
mode: "development",
27+
experiments: {
28+
asyncWebAssembly: true,
29+
},
30+
};

base_layer/key_manager/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pkg/

base_layer/key_manager/Cargo.toml

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ license = "BSD-3-Clause"
77
version = "0.13.0"
88
edition = "2018"
99

10+
[lib]
11+
crate-type = ["lib", "cdylib"]
12+
1013
[dependencies]
1114
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", branch = "main" }
1215

@@ -17,9 +20,14 @@ chacha20 = "0.7.1"
1720
chrono = { version = "0.4.6", features = ["serde"] }
1821
clear_on_drop = "=0.2.4"
1922
crc32fast = "1.2.1"
20-
rand = "0.8"
2123
digest = "0.9.0"
24+
getrandom = { version = "0.2.3", features = ["js"] }
25+
rand = "0.8"
26+
serde = "1.0.89"
27+
serde_derive = "1.0.89"
28+
serde_json = "1.0.39"
2229
thiserror = "1.0.26"
30+
wasm-bindgen = "0.2"
2331

2432
[dev-dependencies]
2533
sha2 = "0.9.8"

base_layer/key_manager/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# Tari Key manager
1+
# Tari Key manager

base_layer/key_manager/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ pub mod error;
1111
pub mod key_manager;
1212
pub mod mnemonic;
1313
pub mod mnemonic_wordlists;
14+
pub mod wasm;

base_layer/key_manager/src/wasm.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use wasm_bindgen::prelude::*;
2+
3+
#[wasm_bindgen]
4+
extern "C" {
5+
pub unsafe fn alert(s: &str);
6+
}
7+
8+
#[wasm_bindgen]
9+
pub fn greet(name: &str) {
10+
alert(&format!("Hello, {}!", name));
11+
}

0 commit comments

Comments
 (0)