@ukeyfe/hardware-transport-lowlevel 1.1.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +131 -0
- package/dist/types.d.ts +4 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +25 -0
- package/src/index.ts +106 -0
- package/src/types.ts +3 -0
- package/tsconfig.json +7 -0
package/README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# `@ukeyfe/hardware-transport-lowlevel`
|
|
2
|
+
|
|
3
|
+
`@ukeyfe/hardware-transport-http` is a library that implements transport communication using http.
|
|
4
|
+
|
|
5
|
+
## What is the purpose
|
|
6
|
+
|
|
7
|
+
- translate JSON payloads to binary messages using protobuf definitions comprehensible to UKey devices
|
|
8
|
+
- chunking and reading chunked messages according to the [UKey protocol](./protocol.md)
|
|
9
|
+
- exposing single API for various transport methods:
|
|
10
|
+
- Http Transport
|
|
11
|
+
- React Native Transport
|
|
12
|
+
- WebUSB
|
|
13
|
+
- Create and expose typescript definitions based on protobuf definitions.
|
|
14
|
+
|
|
15
|
+
### The short version
|
|
16
|
+
|
|
17
|
+
In order to be able to use new features of ukey-firmware you need to update protobuf definitions.
|
|
18
|
+
|
|
19
|
+
1. `git submodule update --init --recursive` to initialize git submodules.
|
|
20
|
+
1. `yarn update-submodules` to update firmware submodule
|
|
21
|
+
1. `yarn update:protobuf` to generate new `./messages.json` and `./src/types/messages.ts`
|
|
22
|
+
|
|
23
|
+
git submodule update --init --recursive to initialize firmware submodule
|
|
24
|
+
yarn update-submodules to update firmware submodule
|
|
25
|
+
yarn update:protobuf to generate new ./messages.json and ./src/types/messages.ts
|
|
26
|
+
|
|
27
|
+
## Docs
|
|
28
|
+
|
|
29
|
+
Documentation is available [hardware-js-sdk](https://developer.ukey.so/connect-to-hardware/hardware-sdk/start)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import * as _ukeyfe_hardware_transport from '@ukeyfe/hardware-transport';
|
|
2
|
+
import _ukeyfe_hardware_transport__default, { LowlevelTransportSharedPlugin } from '@ukeyfe/hardware-transport';
|
|
3
|
+
import EventEmitter from 'events';
|
|
4
|
+
|
|
5
|
+
type LowLevelAcquireInput = {
|
|
6
|
+
uuid: string;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
declare class LowlevelTransport {
|
|
10
|
+
_messages: ReturnType<typeof _ukeyfe_hardware_transport__default.parseConfigure> | undefined;
|
|
11
|
+
configured: boolean;
|
|
12
|
+
Log?: any;
|
|
13
|
+
emitter?: EventEmitter;
|
|
14
|
+
plugin: LowlevelTransportSharedPlugin;
|
|
15
|
+
init(logger: any, emitter: EventEmitter, plugin: LowlevelTransportSharedPlugin): void;
|
|
16
|
+
configure(signedData: any): void;
|
|
17
|
+
listen(): void;
|
|
18
|
+
enumerate(): Promise<_ukeyfe_hardware_transport.LowLevelDevice[]>;
|
|
19
|
+
acquire(input: LowLevelAcquireInput): Promise<{
|
|
20
|
+
uuid: string;
|
|
21
|
+
}>;
|
|
22
|
+
release(uuid: string): Promise<boolean>;
|
|
23
|
+
call(uuid: string, name: string, data: Record<string, unknown>): Promise<_ukeyfe_hardware_transport.MessageFromUKey>;
|
|
24
|
+
cancel(): void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { LowlevelTransport as default };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA,OAAO,SAA8B,MAAM,4BAA4B,CAAC;AACxE,OAAO,KAAK,YAAY,MAAM,QAAQ,CAAC;AACvC,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,4BAA4B,CAAC;AAChF,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAIpD,MAAM,CAAC,OAAO,OAAO,iBAAiB;IACpC,SAAS,EAAE,UAAU,CAAC,OAAO,SAAS,CAAC,cAAc,CAAC,GAAG,SAAS,CAAC;IAEnE,UAAU,UAAS;IAEnB,GAAG,CAAC,EAAE,GAAG,CAAC;IAEV,OAAO,CAAC,EAAE,YAAY,CAAC;IAEvB,MAAM,EAAE,6BAA6B,CAAuC;IAE5E,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,6BAA6B;IAO9E,SAAS,CAAC,UAAU,EAAE,GAAG;IAMzB,MAAM;IAIN,SAAS;IAIH,OAAO,CAAC,KAAK,EAAE,oBAAoB;;;IAanC,OAAO,CAAC,IAAI,EAAE,MAAM;IAUpB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAuCpE,MAAM;CAGP"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var hardwareShared = require('@ukeyfe/hardware-shared');
|
|
4
|
+
var transport = require('@ukeyfe/hardware-transport');
|
|
5
|
+
|
|
6
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
7
|
+
|
|
8
|
+
var transport__default = /*#__PURE__*/_interopDefaultLegacy(transport);
|
|
9
|
+
|
|
10
|
+
/******************************************************************************
|
|
11
|
+
Copyright (c) Microsoft Corporation.
|
|
12
|
+
|
|
13
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
14
|
+
purpose with or without fee is hereby granted.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
17
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
18
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
19
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
20
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
21
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
22
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
23
|
+
***************************************************************************** */
|
|
24
|
+
|
|
25
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
36
|
+
var e = new Error(message);
|
|
37
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const { check, buildBuffers, receiveOne, parseConfigure } = transport__default["default"];
|
|
41
|
+
class LowlevelTransport {
|
|
42
|
+
constructor() {
|
|
43
|
+
this.configured = false;
|
|
44
|
+
this.plugin = {};
|
|
45
|
+
}
|
|
46
|
+
init(logger, emitter, plugin) {
|
|
47
|
+
this.Log = logger;
|
|
48
|
+
this.emitter = emitter;
|
|
49
|
+
this.plugin = plugin;
|
|
50
|
+
this.plugin.init();
|
|
51
|
+
}
|
|
52
|
+
configure(signedData) {
|
|
53
|
+
const messages = parseConfigure(signedData);
|
|
54
|
+
this.configured = true;
|
|
55
|
+
this._messages = messages;
|
|
56
|
+
}
|
|
57
|
+
listen() {
|
|
58
|
+
}
|
|
59
|
+
enumerate() {
|
|
60
|
+
return this.plugin.enumerate();
|
|
61
|
+
}
|
|
62
|
+
acquire(input) {
|
|
63
|
+
var _a;
|
|
64
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
65
|
+
try {
|
|
66
|
+
yield this.plugin.connect(input.uuid);
|
|
67
|
+
return { uuid: input.uuid };
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
this.Log.debug('lowlelvel transport connect error: ', error);
|
|
71
|
+
throw hardwareShared.ERRORS.TypedError(hardwareShared.HardwareErrorCode.LowlevelTrasnportConnectError, (_a = error.message) !== null && _a !== void 0 ? _a : error);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
release(uuid) {
|
|
76
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
77
|
+
try {
|
|
78
|
+
yield this.plugin.disconnect(uuid);
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
this.Log.debug('lowlelvel transport disconnect error: ', error);
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
call(uuid, name, data) {
|
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
if (this._messages === null || !this._messages) {
|
|
90
|
+
throw hardwareShared.ERRORS.TypedError(hardwareShared.HardwareErrorCode.TransportNotConfigured);
|
|
91
|
+
}
|
|
92
|
+
const messages = this._messages;
|
|
93
|
+
if (transport.LogBlockCommand.has(name)) {
|
|
94
|
+
this.Log.debug('lowlevel-transport', 'call-', ' name: ', name);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
this.Log.debug('lowlevel-transport', 'call-', ' name: ', name, ' data: ', data);
|
|
98
|
+
}
|
|
99
|
+
const buffers = buildBuffers(messages, name, data);
|
|
100
|
+
for (const o of buffers) {
|
|
101
|
+
const outData = o.toString('hex');
|
|
102
|
+
this.Log.debug('send hex strting: ', outData);
|
|
103
|
+
try {
|
|
104
|
+
yield this.plugin.send(uuid, outData);
|
|
105
|
+
}
|
|
106
|
+
catch (e) {
|
|
107
|
+
this.Log.debug('lowlevel transport send error: ', e);
|
|
108
|
+
throw hardwareShared.ERRORS.TypedError(hardwareShared.HardwareErrorCode.BleWriteCharacteristicError, e.reason);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
const response = yield this.plugin.receive();
|
|
113
|
+
if (typeof response !== 'string') {
|
|
114
|
+
throw new Error('Returning data is not string');
|
|
115
|
+
}
|
|
116
|
+
this.Log.debug('receive data: ', response);
|
|
117
|
+
const jsonData = receiveOne(messages, response);
|
|
118
|
+
return check.call(jsonData);
|
|
119
|
+
}
|
|
120
|
+
catch (e) {
|
|
121
|
+
this.Log.error('lowlevel call error: ', e);
|
|
122
|
+
throw e;
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
cancel() {
|
|
127
|
+
this.Log.debug('lowlevel-transport', 'cancel');
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
module.exports = LowlevelTransport;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,oBAAoB,GAAG;IACjC,IAAI,EAAE,MAAM,CAAC;CACd,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ukeyfe/hardware-transport-lowlevel",
|
|
3
|
+
"version": "1.1.13",
|
|
4
|
+
"homepage": "https://github.com/UKeyHQ/hardware-js-sdk#readme",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/bestyourwallet/hardware-js-sdk"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"dev": "rimraf dist && rollup -c ../../build/rollup.config.js -w",
|
|
17
|
+
"build": "rimraf dist && rollup -c ../../build/rollup.config.js",
|
|
18
|
+
"lint": "eslint .",
|
|
19
|
+
"lint:fix": "eslint . --fix"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@ukeyfe/hardware-shared": "1.1.13",
|
|
23
|
+
"@ukeyfe/hardware-transport": "1.1.13"
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { ERRORS, HardwareErrorCode } from '@ukeyfe/hardware-shared';
|
|
2
|
+
import transport, { LogBlockCommand } from '@ukeyfe/hardware-transport';
|
|
3
|
+
import type EventEmitter from 'events';
|
|
4
|
+
import type { LowlevelTransportSharedPlugin } from '@ukeyfe/hardware-transport';
|
|
5
|
+
import type { LowLevelAcquireInput } from './types';
|
|
6
|
+
|
|
7
|
+
const { check, buildBuffers, receiveOne, parseConfigure } = transport;
|
|
8
|
+
|
|
9
|
+
export default class LowlevelTransport {
|
|
10
|
+
_messages: ReturnType<typeof transport.parseConfigure> | undefined;
|
|
11
|
+
|
|
12
|
+
configured = false;
|
|
13
|
+
|
|
14
|
+
Log?: any;
|
|
15
|
+
|
|
16
|
+
emitter?: EventEmitter;
|
|
17
|
+
|
|
18
|
+
plugin: LowlevelTransportSharedPlugin = {} as LowlevelTransportSharedPlugin;
|
|
19
|
+
|
|
20
|
+
init(logger: any, emitter: EventEmitter, plugin: LowlevelTransportSharedPlugin) {
|
|
21
|
+
this.Log = logger;
|
|
22
|
+
this.emitter = emitter;
|
|
23
|
+
this.plugin = plugin;
|
|
24
|
+
this.plugin.init();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
configure(signedData: any) {
|
|
28
|
+
const messages = parseConfigure(signedData);
|
|
29
|
+
this.configured = true;
|
|
30
|
+
this._messages = messages;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
listen() {
|
|
34
|
+
// empty
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
enumerate() {
|
|
38
|
+
return this.plugin.enumerate();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async acquire(input: LowLevelAcquireInput) {
|
|
42
|
+
try {
|
|
43
|
+
await this.plugin.connect(input.uuid);
|
|
44
|
+
return { uuid: input.uuid };
|
|
45
|
+
} catch (error) {
|
|
46
|
+
this.Log.debug('lowlelvel transport connect error: ', error);
|
|
47
|
+
throw ERRORS.TypedError(
|
|
48
|
+
HardwareErrorCode.LowlevelTrasnportConnectError,
|
|
49
|
+
error.message ?? error
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async release(uuid: string) {
|
|
55
|
+
try {
|
|
56
|
+
await this.plugin.disconnect(uuid);
|
|
57
|
+
return true;
|
|
58
|
+
} catch (error) {
|
|
59
|
+
this.Log.debug('lowlelvel transport disconnect error: ', error);
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async call(uuid: string, name: string, data: Record<string, unknown>) {
|
|
65
|
+
if (this._messages === null || !this._messages) {
|
|
66
|
+
throw ERRORS.TypedError(HardwareErrorCode.TransportNotConfigured);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const messages = this._messages;
|
|
70
|
+
if (LogBlockCommand.has(name)) {
|
|
71
|
+
this.Log.debug('lowlevel-transport', 'call-', ' name: ', name);
|
|
72
|
+
} else {
|
|
73
|
+
this.Log.debug('lowlevel-transport', 'call-', ' name: ', name, ' data: ', data);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const buffers = buildBuffers(messages, name, data);
|
|
77
|
+
for (const o of buffers) {
|
|
78
|
+
const outData = o.toString('hex');
|
|
79
|
+
// Upload resources on low-end phones may OOM
|
|
80
|
+
this.Log.debug('send hex strting: ', outData);
|
|
81
|
+
try {
|
|
82
|
+
await this.plugin.send(uuid, outData);
|
|
83
|
+
} catch (e) {
|
|
84
|
+
this.Log.debug('lowlevel transport send error: ', e);
|
|
85
|
+
throw ERRORS.TypedError(HardwareErrorCode.BleWriteCharacteristicError, e.reason);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
const response = await this.plugin.receive();
|
|
91
|
+
if (typeof response !== 'string') {
|
|
92
|
+
throw new Error('Returning data is not string');
|
|
93
|
+
}
|
|
94
|
+
this.Log.debug('receive data: ', response);
|
|
95
|
+
const jsonData = receiveOne(messages, response);
|
|
96
|
+
return check.call(jsonData);
|
|
97
|
+
} catch (e) {
|
|
98
|
+
this.Log.error('lowlevel call error: ', e);
|
|
99
|
+
throw e;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
cancel() {
|
|
104
|
+
this.Log.debug('lowlevel-transport', 'cancel');
|
|
105
|
+
}
|
|
106
|
+
}
|
package/src/types.ts
ADDED