Skip to content

Commit 1b72c33

Browse files
authored
feat: create web-sdk (#7)
* feat: init iframe page * chore: resolve example project dependencies * feat: iframe page init * feat: config web-sdk webpack * chore: fix rollup build error * chore: replace https cert * chore: build iframe dev env * fix: web-sdk build error * feat: parse connect setting * feat: init flow * feat: iframe init * feat: jsbridge connect * feat: format jsbridge message * wip: call api * feat: iframe handshake * wip: parse settings at iframe initialization * feat: webpack copy json data * wip: initialize core in iframe * wip: inject environment related functions * wip: call searchDevices * fix: remove window bridge property
1 parent 7fe34f3 commit 1b72c33

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+3532
-198
lines changed

.eslintignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
packages/**/build/
2+
lib/
3+
dist/

.eslintrc

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
"no-dupe-class-members": "off",
3737
"@typescript-eslint/ban-ts-comment": "off",
3838
"no-use-before-define": "off",
39-
"@typescript-eslint/no-use-before-define": "off"
39+
"@typescript-eslint/no-use-before-define": "off",
40+
// webpack packages should put in devDependencies
41+
"import/no-extraneous-dependencies": "off"
4042
}
4143
}

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ libDev
3131
dist
3232
public
3333
coverage
34+
build

build/rollup.config.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import typescript from '@rollup/plugin-typescript';
2-
import json from '@rollup/plugin-json'
2+
import json from '@rollup/plugin-json';
33
import dts from 'rollup-plugin-dts';
44
import path from 'path';
5+
import commonjs from '@rollup/plugin-commonjs';
56

67
const config = [
78
{
@@ -12,7 +13,7 @@ const config = [
1213
format: 'cjs',
1314
},
1415
],
15-
plugins: [typescript(), json()],
16+
plugins: [typescript(), commonjs(), json()],
1617
},
1718
{
1819
input: path.resolve('./src/index.ts'),

lerna.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"packages": [
3-
"packages/*"
3+
"packages/*",
4+
"packages/connect-examples/*"
45
],
56
"npmClient": "yarn",
67
"useWorkspaces": true,

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919
"@babel/core": "^7.16.0",
2020
"@babel/eslint-parser": "^7.16.3",
2121
"@babel/preset-react": "^7.16.0",
22+
"@babel/preset-typescript": "^7.17.12",
2223
"@rollup/plugin-babel": "^5.3.1",
24+
"@rollup/plugin-commonjs": "^22.0.0",
2325
"@rollup/plugin-json": "^4.1.0",
2426
"@rollup/plugin-typescript": "^8.3.2",
2527
"@types/jest": "^27.5.1",
2628
"@types/node": "^16.11.12",
2729
"@typescript-eslint/eslint-plugin": "^5.6.0",
2830
"@typescript-eslint/parser": "^5.6.0",
31+
"babel-loader": "^8.2.5",
2932
"eslint": "^8.4.1",
3033
"eslint-config-airbnb": "^19.0.2",
3134
"eslint-config-airbnb-typescript": "^16.1.0",
@@ -43,6 +46,8 @@
4346
"rimraf": "^3.0.2",
4447
"rollup": "^2.73.0",
4548
"rollup-plugin-dts": "^4.2.1",
49+
"ts-node": "^10.8.0",
50+
"tsconfig-paths": "^4.0.0",
4651
"typescript": "^4.5.2"
4752
}
4853
}

packages/connect-examples/browser/package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
"react": "^18.1.0",
1010
"react-dom": "^18.1.0",
1111
"react-scripts": "5.0.1",
12-
"web-vitals": "^2.1.4",
13-
"@onekeyfe/hd-core": "file:../../core"
12+
"web-vitals": "^2.1.4"
1413
},
1514
"scripts": {
16-
"start": "export PORT=8088 && HTTPS=true react-scripts start",
15+
"start": "export PORT=8090 && HTTPS=true react-scripts start",
1716
"build": "react-scripts build",
1817
"test": "react-scripts test",
1918
"eject": "react-scripts eject"

packages/connect-examples/browser/src/App.js

+25-14
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
1-
import { useState } from 'react'
2-
import { DeviceList, Core } from '@onekeyfe/hd-core'
1+
import { useState, useEffect } from 'react'
2+
import HardwareWebSdk from '@onekeyfe/hd-web-sdk'
33
import './App.css';
44

55
let core
66

7+
let loaded = false
8+
79
function App() {
10+
console.log(HardwareWebSdk)
811
const [devices, setDevices] = useState([])
912
const [currentDevice, setCurrentDevice] = useState(null)
1013
const [initialize, setInitialize] = useState(false)
1114

1215
const sdkInit = async () => {
13-
core = new Core()
14-
await core.initCore()
15-
await core.initDeviceList()
16-
setInitialize(true)
16+
const settings = {
17+
debug: true,
18+
connectSrc: 'https://localhost:8088/',
19+
}
20+
HardwareWebSdk.init(settings)
21+
}
22+
23+
useEffect(() => {
24+
if (!loaded) {
25+
sdkInit()
26+
}
27+
loaded = true
28+
}, []);
29+
30+
const callAPI = async () => {
31+
const res = await HardwareWebSdk.call({method: 'getFeatures', params: {foo: 'bar'}})
32+
console.log('call ressssssss: ', res)
1733
}
1834

1935
const usbConnect = () => {
@@ -23,17 +39,11 @@ function App() {
2339
}
2440

2541
const getDevicesList = async () => {
26-
if (!initialize) {
27-
alert('请先操作 SDK 初始化')
28-
return
29-
}
30-
const deviceList = new DeviceList()
31-
const devices = await deviceList.getDeviceLists()
32-
setDevices(devices)
42+
HardwareWebSdk.searchDevices()
3343
}
3444

3545
const onGetFeatures = () => {
36-
core.getFeatures()
46+
// HardwareWebSdk.getDevicesList()
3747
}
3848

3949
return (
@@ -44,6 +54,7 @@ function App() {
4454
<button onClick={getDevicesList}>GetDeviceList</button>
4555
<button onClick={usbConnect}>Device Connect</button>
4656
<button onClick={onGetFeatures}>GetFeatures</button>
57+
<button onClick={callAPI}>CallAPI</button>
4758
</div>
4859
<div style={{textAlign: 'left', margin: '20px'}}>
4960
当前选取设备:{currentDevice ? JSON.stringify(currentDevice) : '无'}

packages/connect-examples/browser/yarn.lock

+1-107
Original file line numberDiff line numberDiff line change
@@ -1467,18 +1467,6 @@
14671467
"@nodelib/fs.scandir" "2.1.5"
14681468
fastq "^1.6.0"
14691469

1470-
"@onekeyfe/hd-core@file:../../core":
1471-
version "1.0.0"
1472-
dependencies:
1473-
"@onekeyfe/hd-transport" "file:../../../../../../Library/Caches/Yarn/v6/npm-@onekeyfe-hd-core-1.0.0-5242a91f-4c4a-4b74-a64a-1c33e84237d8-1652948681750/node_modules/@onekeyfe/hd-transport"
1474-
1475-
"@onekeyfe/hd-transport@file:../../hd-transport":
1476-
version "1.0.0"
1477-
dependencies:
1478-
bytebuffer "^5.0.1"
1479-
long "^4.0.0"
1480-
protobufjs "^6.11.2"
1481-
14821470
"@pmmmwh/react-refresh-webpack-plugin@^0.5.3":
14831471
version "0.5.6"
14841472
resolved "https://registry.yarnpkg.com/@pmmmwh/react-refresh-webpack-plugin/-/react-refresh-webpack-plugin-0.5.6.tgz#9ced74cb23dae31ab385f775e237ce4c50422a1d"
@@ -1494,59 +1482,6 @@
14941482
schema-utils "^3.0.0"
14951483
source-map "^0.7.3"
14961484

1497-
"@protobufjs/aspromise@^1.1.1", "@protobufjs/aspromise@^1.1.2":
1498-
version "1.1.2"
1499-
resolved "https://registry.yarnpkg.com/@protobufjs/aspromise/-/aspromise-1.1.2.tgz#9b8b0cc663d669a7d8f6f5d0893a14d348f30fbf"
1500-
integrity sha512-j+gKExEuLmKwvz3OgROXtrJ2UG2x8Ch2YZUxahh+s1F2HZ+wAceUNLkvy6zKCPVRkU++ZWQrdxsUeQXmcg4uoQ==
1501-
1502-
"@protobufjs/base64@^1.1.2":
1503-
version "1.1.2"
1504-
resolved "https://registry.yarnpkg.com/@protobufjs/base64/-/base64-1.1.2.tgz#4c85730e59b9a1f1f349047dbf24296034bb2735"
1505-
integrity sha512-AZkcAA5vnN/v4PDqKyMR5lx7hZttPDgClv83E//FMNhR2TMcLUhfRUBHCmSl0oi9zMgDDqRUJkSxO3wm85+XLg==
1506-
1507-
"@protobufjs/codegen@^2.0.4":
1508-
version "2.0.4"
1509-
resolved "https://registry.yarnpkg.com/@protobufjs/codegen/-/codegen-2.0.4.tgz#7ef37f0d010fb028ad1ad59722e506d9262815cb"
1510-
integrity sha512-YyFaikqM5sH0ziFZCN3xDC7zeGaB/d0IUb9CATugHWbd1FRFwWwt4ld4OYMPWu5a3Xe01mGAULCdqhMlPl29Jg==
1511-
1512-
"@protobufjs/eventemitter@^1.1.0":
1513-
version "1.1.0"
1514-
resolved "https://registry.yarnpkg.com/@protobufjs/eventemitter/-/eventemitter-1.1.0.tgz#355cbc98bafad5978f9ed095f397621f1d066b70"
1515-
integrity sha512-j9ednRT81vYJ9OfVuXG6ERSTdEL1xVsNgqpkxMsbIabzSo3goCjDIveeGv5d03om39ML71RdmrGNjG5SReBP/Q==
1516-
1517-
"@protobufjs/fetch@^1.1.0":
1518-
version "1.1.0"
1519-
resolved "https://registry.yarnpkg.com/@protobufjs/fetch/-/fetch-1.1.0.tgz#ba99fb598614af65700c1619ff06d454b0d84c45"
1520-
integrity sha512-lljVXpqXebpsijW71PZaCYeIcE5on1w5DlQy5WH6GLbFryLUrBD4932W/E2BSpfRJWseIL4v/KPgBFxDOIdKpQ==
1521-
dependencies:
1522-
"@protobufjs/aspromise" "^1.1.1"
1523-
"@protobufjs/inquire" "^1.1.0"
1524-
1525-
"@protobufjs/float@^1.0.2":
1526-
version "1.0.2"
1527-
resolved "https://registry.yarnpkg.com/@protobufjs/float/-/float-1.0.2.tgz#5e9e1abdcb73fc0a7cb8b291df78c8cbd97b87d1"
1528-
integrity sha512-Ddb+kVXlXst9d+R9PfTIxh1EdNkgoRe5tOX6t01f1lYWOvJnSPDBlG241QLzcyPdoNTsblLUdujGSE4RzrTZGQ==
1529-
1530-
"@protobufjs/inquire@^1.1.0":
1531-
version "1.1.0"
1532-
resolved "https://registry.yarnpkg.com/@protobufjs/inquire/-/inquire-1.1.0.tgz#ff200e3e7cf2429e2dcafc1140828e8cc638f089"
1533-
integrity sha512-kdSefcPdruJiFMVSbn801t4vFK7KB/5gd2fYvrxhuJYg8ILrmn9SKSX2tZdV6V+ksulWqS7aXjBcRXl3wHoD9Q==
1534-
1535-
"@protobufjs/path@^1.1.2":
1536-
version "1.1.2"
1537-
resolved "https://registry.yarnpkg.com/@protobufjs/path/-/path-1.1.2.tgz#6cc2b20c5c9ad6ad0dccfd21ca7673d8d7fbf68d"
1538-
integrity sha512-6JOcJ5Tm08dOHAbdR3GrvP+yUUfkjG5ePsHYczMFLq3ZmMkAD98cDgcT2iA1lJ9NVwFd4tH/iSSoe44YWkltEA==
1539-
1540-
"@protobufjs/pool@^1.1.0":
1541-
version "1.1.0"
1542-
resolved "https://registry.yarnpkg.com/@protobufjs/pool/-/pool-1.1.0.tgz#09fd15f2d6d3abfa9b65bc366506d6ad7846ff54"
1543-
integrity sha512-0kELaGSIDBKvcgS4zkjz1PeddatrjYcmMWOlAuAPwAeccUrPHdUqo/J6LiymHHEiJT5NrF1UVwxY14f+fy4WQw==
1544-
1545-
"@protobufjs/utf8@^1.1.0":
1546-
version "1.1.0"
1547-
resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570"
1548-
integrity sha512-Vvn3zZrhQZkkBE8LSuW3em98c0FwgO4nxzv6OdSxPKJIEKY2bGbHn+mhGIPerzI4twdxaP8/0+06HBpwf345Lw==
1549-
15501485
"@rollup/plugin-babel@^5.2.0":
15511486
version "5.3.1"
15521487
resolved "https://registry.yarnpkg.com/@rollup/plugin-babel/-/plugin-babel-5.3.1.tgz#04bc0608f4aa4b2e4b1aebf284344d0f68fda283"
@@ -1953,17 +1888,12 @@
19531888
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
19541889
integrity sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==
19551890

1956-
"@types/long@^4.0.1":
1957-
version "4.0.2"
1958-
resolved "https://registry.yarnpkg.com/@types/long/-/long-4.0.2.tgz#b74129719fc8d11c01868010082d483b7545591a"
1959-
integrity sha512-MqTGEo5bj5t157U6fA/BiDynNkn0YknVdh48CMPkTSpFTVmvao5UQmm7uEF6xBEo7qIMAlY/JSleYaE6VOdpaA==
1960-
19611891
"@types/mime@^1":
19621892
version "1.3.2"
19631893
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-1.3.2.tgz#93e25bf9ee75fe0fd80b594bc4feb0e862111b5a"
19641894
integrity sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw==
19651895

1966-
"@types/node@*", "@types/node@>=13.7.0":
1896+
"@types/node@*":
19671897
version "17.0.34"
19681898
resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.34.tgz#3b0b6a50ff797280b8d000c6281d229f9c538cef"
19691899
integrity sha512-XImEz7XwTvDBtzlTnm8YvMqGW/ErMWBsKZ+hMTvnDIjGCKxwK5Xpc+c/oQjOauwq8M4OS11hEkpjX8rrI/eEgA==
@@ -2871,13 +2801,6 @@ builtin-modules@^3.1.0:
28712801
resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-3.3.0.tgz#cae62812b89801e9656336e46223e030386be7b6"
28722802
integrity sha512-zhaCDicdLuWN5UbN5IMnFqNMhNfo919sH85y2/ea+5Yg9TsTkeZxpL+JLbp6cgYFS4sRLp3YV4S6yDuqVWHYOw==
28732803

2874-
bytebuffer@^5.0.1:
2875-
version "5.0.1"
2876-
resolved "https://registry.yarnpkg.com/bytebuffer/-/bytebuffer-5.0.1.tgz#582eea4b1a873b6d020a48d58df85f0bba6cfddd"
2877-
integrity sha512-IuzSdmADppkZ6DlpycMkm8l9zeEq16fWtLvunEwFiYciR/BHo4E8/xs5piFquG+Za8OWmMqHF8zuRviz2LHvRQ==
2878-
dependencies:
2879-
long "~3"
2880-
28812804
28822805
version "3.0.0"
28832806
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.0.0.tgz#d32815404d689699f85a4ea4fa8755dd13a96048"
@@ -5870,16 +5793,6 @@ lodash@^4.17.15, lodash@^4.17.20, lodash@^4.17.21, lodash@^4.7.0:
58705793
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
58715794
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
58725795

5873-
long@^4.0.0:
5874-
version "4.0.0"
5875-
resolved "https://registry.yarnpkg.com/long/-/long-4.0.0.tgz#9a7b71cfb7d361a194ea555241c92f7468d5bf28"
5876-
integrity sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==
5877-
5878-
long@~3:
5879-
version "3.2.0"
5880-
resolved "https://registry.yarnpkg.com/long/-/long-3.2.0.tgz#d821b7138ca1cb581c172990ef14db200b5c474b"
5881-
integrity sha1-2CG3E4yhy1gcFymQ7xTbIAtcR0s=
5882-
58835796
loose-envify@^1.1.0, loose-envify@^1.4.0:
58845797
version "1.4.0"
58855798
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"
@@ -7084,25 +6997,6 @@ prop-types@^15.8.1:
70846997
object-assign "^4.1.1"
70856998
react-is "^16.13.1"
70866999

7087-
protobufjs@^6.11.2:
7088-
version "6.11.2"
7089-
resolved "https://registry.yarnpkg.com/protobufjs/-/protobufjs-6.11.2.tgz#de39fabd4ed32beaa08e9bb1e30d08544c1edf8b"
7090-
integrity sha512-4BQJoPooKJl2G9j3XftkIXjoC9C0Av2NOrWmbLWT1vH32GcSUHjM0Arra6UfTsVyfMAuFzaLucXn1sadxJydAw==
7091-
dependencies:
7092-
"@protobufjs/aspromise" "^1.1.2"
7093-
"@protobufjs/base64" "^1.1.2"
7094-
"@protobufjs/codegen" "^2.0.4"
7095-
"@protobufjs/eventemitter" "^1.1.0"
7096-
"@protobufjs/fetch" "^1.1.0"
7097-
"@protobufjs/float" "^1.0.2"
7098-
"@protobufjs/inquire" "^1.1.0"
7099-
"@protobufjs/path" "^1.1.2"
7100-
"@protobufjs/pool" "^1.1.0"
7101-
"@protobufjs/utf8" "^1.1.0"
7102-
"@types/long" "^4.0.1"
7103-
"@types/node" ">=13.7.0"
7104-
long "^4.0.0"
7105-
71067000
proxy-addr@~2.0.7:
71077001
version "2.0.7"
71087002
resolved "https://registry.yarnpkg.com/proxy-addr/-/proxy-addr-2.0.7.tgz#f19fe69ceab311eeb94b42e70e8c2070f9ba1025"

packages/core/package.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@
2222
"url": "https://github.com/OneKeyHQ/hardware-js-sdk/issues"
2323
},
2424
"dependencies": {
25-
"@onekeyfe/hd-transport": "file:../hd-transport",
26-
"@onekeyfe/hd-transport-http": "file:../hd-transport-http",
25+
"@onekeyfe/hd-transport": "*",
26+
"@onekeyfe/hd-transport-http": "*",
2727
"@onekeyfe/rollout": "1.0.6",
28-
"cross-fetch": "^3.1.5"
28+
"cross-fetch": "^3.1.5",
29+
"parse-uri": "^1.0.7"
30+
},
31+
"devDependencies": {
32+
"@types/parse-uri": "^1.0.0"
2933
}
3034
}

packages/core/src/api/BaseMethod.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { ERRORS } from '../constants';
2-
import { TypedError } from '../constants/errors';
31
import { UI_REQUEST } from '../constants/ui-request';
42
import { Device } from '../device/Device';
53
import type { FirmwareRange } from '../types';

packages/core/src/constants/errors.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
export const ERROR_CODES = {
2+
// methods error
23
Method_InvalidParameter: '',
3-
Runtime: '',
4-
Transport_InvalidProtobuf: '',
54
Call_API: '',
5+
Call_NotResponse: 'No response data',
6+
7+
// device error
8+
Transport_InvalidProtobuf: '',
69
Device_FwException: '',
710
Device_UnexpectedMode: '',
811
Device_CallInProgress: '',
912
Device_InitializeFailed: '',
1013
Not_Use_Onekey_Device: 'Please use onekey device',
14+
15+
// runtime error
16+
Runtime: '',
17+
18+
// iframe error
19+
Init_IframeBlocked: 'Init_IframeBlocked',
20+
Init_IframeTimeout: 'Init_IframeTimeout',
21+
Init_AlreadyInitialized: 'Init_AlreadyInitialized',
22+
Init_IframeLoadFail: 'Init_IframeLoadFail',
1123
};
1224

1325
export class OnekeyError extends Error {

packages/core/src/constants/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export * as ERRORS from './errors';
2-
export * as PROTO from '@onekeyfe/hd-transport/dist/types/messages';
2+
export { Messages as PROTO } from '@onekeyfe/hd-transport';

0 commit comments

Comments
 (0)