@ukeyfe/hardware-transport 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.
Files changed (72) hide show
  1. package/.eslintrc +24 -0
  2. package/README.md +29 -0
  3. package/__tests__/build-receive.test.js +117 -0
  4. package/__tests__/decode-features.test.js +72 -0
  5. package/__tests__/encode-decode-basic.test.js +272 -0
  6. package/__tests__/encode-decode.test.js +532 -0
  7. package/__tests__/messages.test.js +86 -0
  8. package/dist/constants.d.ts +6 -0
  9. package/dist/constants.d.ts.map +1 -0
  10. package/dist/index.d.ts +5203 -0
  11. package/dist/index.d.ts.map +1 -0
  12. package/dist/index.js +942 -0
  13. package/dist/serialization/index.d.ts +6 -0
  14. package/dist/serialization/index.d.ts.map +1 -0
  15. package/dist/serialization/protobuf/decode.d.ts +6 -0
  16. package/dist/serialization/protobuf/decode.d.ts.map +1 -0
  17. package/dist/serialization/protobuf/encode.d.ts +5 -0
  18. package/dist/serialization/protobuf/encode.d.ts.map +1 -0
  19. package/dist/serialization/protobuf/index.d.ts +4 -0
  20. package/dist/serialization/protobuf/index.d.ts.map +1 -0
  21. package/dist/serialization/protobuf/messages.d.ts +11 -0
  22. package/dist/serialization/protobuf/messages.d.ts.map +1 -0
  23. package/dist/serialization/protocol/decode.d.ts +11 -0
  24. package/dist/serialization/protocol/decode.d.ts.map +1 -0
  25. package/dist/serialization/protocol/encode.d.ts +11 -0
  26. package/dist/serialization/protocol/encode.d.ts.map +1 -0
  27. package/dist/serialization/protocol/index.d.ts +3 -0
  28. package/dist/serialization/protocol/index.d.ts.map +1 -0
  29. package/dist/serialization/receive.d.ts +8 -0
  30. package/dist/serialization/receive.d.ts.map +1 -0
  31. package/dist/serialization/send.d.ts +7 -0
  32. package/dist/serialization/send.d.ts.map +1 -0
  33. package/dist/types/index.d.ts +3 -0
  34. package/dist/types/index.d.ts.map +1 -0
  35. package/dist/types/messages.d.ts +3762 -0
  36. package/dist/types/messages.d.ts.map +1 -0
  37. package/dist/types/transport.d.ts +62 -0
  38. package/dist/types/transport.d.ts.map +1 -0
  39. package/dist/utils/highlevel-checks.d.ts +10 -0
  40. package/dist/utils/highlevel-checks.d.ts.map +1 -0
  41. package/dist/utils/logBlockCommand.d.ts +2 -0
  42. package/dist/utils/logBlockCommand.d.ts.map +1 -0
  43. package/dist/utils/protobuf.d.ts +2 -0
  44. package/dist/utils/protobuf.d.ts.map +1 -0
  45. package/jest.config.js +7 -0
  46. package/package.json +31 -0
  47. package/protocol.md +21 -0
  48. package/scripts/protobuf-build.sh +58 -0
  49. package/scripts/protobuf-patches/TxAck.js +44 -0
  50. package/scripts/protobuf-patches/TxInputType.js +49 -0
  51. package/scripts/protobuf-patches/TxOutputType.js +50 -0
  52. package/scripts/protobuf-patches/index.js +274 -0
  53. package/scripts/protobuf-types.js +283 -0
  54. package/src/constants.ts +8 -0
  55. package/src/index.ts +41 -0
  56. package/src/serialization/index.ts +8 -0
  57. package/src/serialization/protobuf/decode.ts +95 -0
  58. package/src/serialization/protobuf/encode.ts +79 -0
  59. package/src/serialization/protobuf/index.ts +3 -0
  60. package/src/serialization/protobuf/messages.ts +37 -0
  61. package/src/serialization/protocol/decode.ts +48 -0
  62. package/src/serialization/protocol/encode.ts +59 -0
  63. package/src/serialization/protocol/index.ts +2 -0
  64. package/src/serialization/receive.ts +18 -0
  65. package/src/serialization/send.ts +56 -0
  66. package/src/types/index.ts +2 -0
  67. package/src/types/messages.ts +4864 -0
  68. package/src/types/transport.ts +71 -0
  69. package/src/utils/highlevel-checks.ts +88 -0
  70. package/src/utils/logBlockCommand.ts +1 -0
  71. package/src/utils/protobuf.ts +24 -0
  72. package/tsconfig.json +11 -0
@@ -0,0 +1,71 @@
1
+ import type EventEmitter from 'events';
2
+
3
+ export type UKeyUsbDeviceInfo = {
4
+ path: string;
5
+ };
6
+
7
+ export type UKeyDeviceInfoWithSession = UKeyUsbDeviceInfo & {
8
+ session?: string | null;
9
+ debugSession?: string | null;
10
+ debug: boolean;
11
+ };
12
+
13
+ export type UKeyMobileDeviceInfo = {
14
+ id: string;
15
+ name: string | null;
16
+ };
17
+
18
+ export type UKeyDeviceInfo = UKeyDeviceInfoWithSession & UKeyMobileDeviceInfo;
19
+
20
+ export type AcquireInput = {
21
+ path?: string;
22
+ previous?: string | null;
23
+ uuid?: string;
24
+ forceCleanRunPromise?: boolean;
25
+ };
26
+
27
+ export type MessageFromUKey = { type: string; message: Record<string, any> };
28
+
29
+ type ITransportInitFn = (
30
+ logger?: any,
31
+ emitter?: EventEmitter,
32
+ plugin?: LowlevelTransportSharedPlugin
33
+ ) => Promise<string>;
34
+
35
+ export type Transport = {
36
+ enumerate(): Promise<Array<UKeyDeviceInfo>>;
37
+ listen(old?: Array<UKeyDeviceInfo>): Promise<Array<UKeyDeviceInfo>>;
38
+ acquire(input: AcquireInput): Promise<string>;
39
+ release(session: string, onclose: boolean): Promise<void>;
40
+ configure(signedData: JSON | string): Promise<void>;
41
+ call(session: string, name: string, data: Record<string, any>): Promise<MessageFromUKey>;
42
+ post(session: string, name: string, data: Record<string, any>): Promise<void>;
43
+ read(session: string): Promise<MessageFromUKey>;
44
+ cancel(): Promise<void>;
45
+
46
+ // web-usb, web-bluetooth request device
47
+ promptDeviceAccess?: () => Promise<USBDevice | BluetoothDevice | null>;
48
+
49
+ // resolves when the transport can be used; rejects when it cannot
50
+ init: ITransportInitFn;
51
+ stop(): void;
52
+
53
+ configured: boolean;
54
+ version: string;
55
+ name: string;
56
+ activeName?: string;
57
+
58
+ isOutdated: boolean;
59
+ };
60
+
61
+ export type LowLevelDevice = { id: string; name: string };
62
+ export type LowlevelTransportSharedPlugin = {
63
+ enumerate: () => Promise<LowLevelDevice[]>;
64
+ send: (uuid: string, data: string) => Promise<void>;
65
+ receive: () => Promise<string>;
66
+ connect: (uuid: string) => Promise<void>;
67
+ disconnect: (uuid: string) => Promise<void>;
68
+
69
+ init: () => Promise<void>;
70
+ version: string;
71
+ };
@@ -0,0 +1,88 @@
1
+ // input checks for high-level transports
2
+
3
+ import type { UKeyDeviceInfoWithSession, MessageFromUKey } from '../types';
4
+
5
+ const ERROR = 'Wrong result type.';
6
+
7
+ export function info(res: any) {
8
+ if (typeof res !== 'object' || res == null) {
9
+ throw new Error('Wrong result type.');
10
+ }
11
+ const { version } = res;
12
+ if (typeof version !== 'string') {
13
+ throw new Error(ERROR);
14
+ }
15
+ const configured = !!res.configured;
16
+ return { version, configured };
17
+ }
18
+
19
+ export function version(version: any) {
20
+ if (typeof version !== 'string') {
21
+ throw new Error(ERROR);
22
+ }
23
+ return version.trim();
24
+ }
25
+
26
+ function convertSession(r: any) {
27
+ if (r == null) {
28
+ return null;
29
+ }
30
+ if (typeof r !== 'string') {
31
+ throw new Error(ERROR);
32
+ }
33
+ return r;
34
+ }
35
+
36
+ export function devices(res: any): Array<UKeyDeviceInfoWithSession> {
37
+ if (typeof res !== 'object') {
38
+ throw new Error(ERROR);
39
+ }
40
+ if (!(res instanceof Array)) {
41
+ throw new Error(ERROR);
42
+ }
43
+ return res.map((o: any): UKeyDeviceInfoWithSession => {
44
+ if (typeof o !== 'object' || o == null) {
45
+ throw new Error(ERROR);
46
+ }
47
+ const { path } = o;
48
+ if (typeof path !== 'string') {
49
+ throw new Error(ERROR);
50
+ }
51
+ const pathS = path.toString();
52
+ return {
53
+ path: pathS,
54
+ session: convertSession(o.session),
55
+ debugSession: convertSession(o.debugSession),
56
+ // @ts-ignore
57
+ product: o.product,
58
+ vendor: o.vendor,
59
+ debug: !!o.debug,
60
+ };
61
+ });
62
+ }
63
+
64
+ export function acquire(res: any) {
65
+ if (typeof res !== 'object' || res == null) {
66
+ throw new Error(ERROR);
67
+ }
68
+ const { session } = res;
69
+ if (typeof session !== 'string' && typeof session !== 'number') {
70
+ throw new Error(ERROR);
71
+ }
72
+ return session.toString();
73
+ }
74
+
75
+ export function call(res: any): MessageFromUKey {
76
+ if (typeof res !== 'object' || res == null) {
77
+ throw new Error(ERROR);
78
+ }
79
+ const { type } = res;
80
+ if (typeof type !== 'string') {
81
+ throw new Error(ERROR);
82
+ }
83
+ const { message } = res;
84
+ if (typeof message !== 'object' || message == null) {
85
+ throw new Error(ERROR);
86
+ }
87
+ return { type, message };
88
+ }
@@ -0,0 +1 @@
1
+ export const LogBlockCommand = new Set(['PassphraseAck', 'PinMatrixAck']);
@@ -0,0 +1,24 @@
1
+ const primitiveTypes = [
2
+ 'bool',
3
+ 'string',
4
+ 'bytes',
5
+ 'int32',
6
+ 'int64',
7
+ 'uint32',
8
+ 'uint64',
9
+ 'sint32',
10
+ 'sint64',
11
+ 'fixed32',
12
+ 'fixed64',
13
+ 'sfixed32',
14
+ 'sfixed64',
15
+ 'double',
16
+ 'float',
17
+ ];
18
+
19
+ /**
20
+ * Determines whether given field is "primitive"
21
+ * bool, strings, uint32 => true
22
+ * HDNodeType => false
23
+ */
24
+ export const isPrimitiveField = (field: any) => primitiveTypes.includes(field);
package/tsconfig.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "extends": "../../tsconfig.lib.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "types": [
6
+ "w3c-web-usb",
7
+ "web-bluetooth"
8
+ ]
9
+ },
10
+ "include": ["./src"]
11
+ }