@onekeyfe/hd-transport 0.0.1

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 (56) hide show
  1. package/.eslintrc +21 -0
  2. package/README.md +3 -0
  3. package/dist/constants.d.ts +5 -0
  4. package/dist/constants.d.ts.map +1 -0
  5. package/dist/index.d.ts +2725 -0
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +742 -0
  8. package/dist/serialization/index.d.ts +5 -0
  9. package/dist/serialization/index.d.ts.map +1 -0
  10. package/dist/serialization/protobuf/decode.d.ts +6 -0
  11. package/dist/serialization/protobuf/decode.d.ts.map +1 -0
  12. package/dist/serialization/protobuf/encode.d.ts +5 -0
  13. package/dist/serialization/protobuf/encode.d.ts.map +1 -0
  14. package/dist/serialization/protobuf/index.d.ts +4 -0
  15. package/dist/serialization/protobuf/index.d.ts.map +1 -0
  16. package/dist/serialization/protobuf/messages.d.ts +11 -0
  17. package/dist/serialization/protobuf/messages.d.ts.map +1 -0
  18. package/dist/serialization/protocol/decode.d.ts +11 -0
  19. package/dist/serialization/protocol/decode.d.ts.map +1 -0
  20. package/dist/serialization/protocol/encode.d.ts +11 -0
  21. package/dist/serialization/protocol/encode.d.ts.map +1 -0
  22. package/dist/serialization/protocol/index.d.ts +3 -0
  23. package/dist/serialization/protocol/index.d.ts.map +1 -0
  24. package/dist/serialization/receive.d.ts +8 -0
  25. package/dist/serialization/receive.d.ts.map +1 -0
  26. package/dist/serialization/send.d.ts +6 -0
  27. package/dist/serialization/send.d.ts.map +1 -0
  28. package/dist/types/index.d.ts +3 -0
  29. package/dist/types/index.d.ts.map +1 -0
  30. package/dist/types/messages.d.ts +1954 -0
  31. package/dist/types/messages.d.ts.map +1 -0
  32. package/dist/types/transport.d.ts +42 -0
  33. package/dist/types/transport.d.ts.map +1 -0
  34. package/dist/utils/highlevel-checks.d.ts +10 -0
  35. package/dist/utils/highlevel-checks.d.ts.map +1 -0
  36. package/dist/utils/protobuf.d.ts +2 -0
  37. package/dist/utils/protobuf.d.ts.map +1 -0
  38. package/package.json +27 -0
  39. package/src/constants.ts +4 -0
  40. package/src/index.ts +28 -0
  41. package/src/serialization/index.ts +6 -0
  42. package/src/serialization/protobuf/decode.ts +83 -0
  43. package/src/serialization/protobuf/encode.ts +79 -0
  44. package/src/serialization/protobuf/index.ts +3 -0
  45. package/src/serialization/protobuf/messages.ts +37 -0
  46. package/src/serialization/protocol/decode.ts +48 -0
  47. package/src/serialization/protocol/encode.ts +59 -0
  48. package/src/serialization/protocol/index.ts +2 -0
  49. package/src/serialization/receive.ts +18 -0
  50. package/src/serialization/send.ts +39 -0
  51. package/src/types/index.ts +2 -0
  52. package/src/types/messages.ts +2489 -0
  53. package/src/types/transport.ts +51 -0
  54. package/src/utils/highlevel-checks.ts +88 -0
  55. package/src/utils/protobuf.ts +24 -0
  56. package/tsconfig.json +7 -0
@@ -0,0 +1,51 @@
1
+ export type OneKeyUsbDeviceInfo = {
2
+ path: string;
3
+ };
4
+
5
+ export type OneKeyDeviceInfoWithSession = OneKeyUsbDeviceInfo & {
6
+ session?: string | null;
7
+ debugSession?: string | null;
8
+ debug: boolean;
9
+ };
10
+
11
+ export type OneKeyMobileDeviceInfo = {
12
+ id: string;
13
+ name: string | null;
14
+ };
15
+
16
+ export type OneKeyDeviceInfo = OneKeyDeviceInfoWithSession & OneKeyMobileDeviceInfo;
17
+
18
+ export type AcquireInput = {
19
+ path?: string;
20
+ previous?: string | null;
21
+ uuid?: string;
22
+ };
23
+
24
+ export type MessageFromOneKey = { type: string; message: Record<string, any> };
25
+
26
+ export type Transport = {
27
+ enumerate(): Promise<Array<OneKeyDeviceInfo>>;
28
+ listen(old?: Array<OneKeyDeviceInfo>): Promise<Array<OneKeyDeviceInfo>>;
29
+ acquire(input: AcquireInput): Promise<string>;
30
+ release(session: string, onclose: boolean): Promise<void>;
31
+ configure(signedData: JSON | string): Promise<void>;
32
+ call(session: string, name: string, data: Record<string, any>): Promise<MessageFromOneKey>;
33
+ post(session: string, name: string, data: Record<string, any>): Promise<void>;
34
+ read(session: string): Promise<MessageFromOneKey>;
35
+
36
+ // resolves when the transport can be used; rejects when it cannot
37
+ init(): Promise<string>;
38
+ stop(): void;
39
+
40
+ configured: boolean;
41
+ version: string;
42
+ name: string;
43
+ activeName?: string;
44
+
45
+ // webusb has a different model, where you have to
46
+ // request device connection
47
+ requestDevice: () => Promise<void>;
48
+ requestNeeded: boolean;
49
+
50
+ isOutdated: boolean;
51
+ };
@@ -0,0 +1,88 @@
1
+ // input checks for high-level transports
2
+
3
+ import type { OneKeyDeviceInfoWithSession, MessageFromOneKey } 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<OneKeyDeviceInfoWithSession> {
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): OneKeyDeviceInfoWithSession => {
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): MessageFromOneKey {
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,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,7 @@
1
+ {
2
+ "extends": "../../tsconfig.lib.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ },
6
+ "include": ["./src"]
7
+ }