@cef-ebsi/cli 1.3.2 → 1.5.0

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 (202) hide show
  1. package/bin/cli.js +1 -1
  2. package/dist/abi/pilot/DidRegistry.js +568 -0
  3. package/dist/abi/pilot/DidRegistry.js.map +1 -0
  4. package/dist/abi/{test/TirV4.js → pilot/Ptr.js} +255 -299
  5. package/dist/abi/pilot/Ptr.js.map +1 -0
  6. package/dist/abi/pilot/SchemaSCRegistry.js +264 -0
  7. package/dist/abi/pilot/SchemaSCRegistry.js.map +1 -0
  8. package/dist/abi/{test/TprV3.js → pilot/Tcr.js} +269 -261
  9. package/dist/abi/pilot/Tcr.js.map +1 -0
  10. package/dist/abi/pilot/Timestamp.js +658 -0
  11. package/dist/abi/pilot/Timestamp.js.map +1 -0
  12. package/dist/abi/pilot/Tir.js +428 -0
  13. package/dist/abi/pilot/Tir.js.map +1 -0
  14. package/dist/abi/pilot/TnT.js +842 -0
  15. package/dist/abi/pilot/TnT.js.map +1 -0
  16. package/dist/abi/pilot/Tpr.js +492 -0
  17. package/dist/abi/pilot/Tpr.js.map +1 -0
  18. package/dist/abi/test/DidRegistry.js +144 -658
  19. package/dist/abi/test/DidRegistry.js.map +1 -1
  20. package/dist/abi/{pilot/TirV4.js → test/Ptr.js} +255 -299
  21. package/dist/abi/test/Ptr.js.map +1 -0
  22. package/dist/abi/test/SchemaSCRegistry.js +122 -795
  23. package/dist/abi/test/SchemaSCRegistry.js.map +1 -1
  24. package/dist/abi/{pilot/TprV3.js → test/Tcr.js} +269 -261
  25. package/dist/abi/test/Tcr.js.map +1 -0
  26. package/dist/abi/test/Timestamp.js +239 -673
  27. package/dist/abi/test/Timestamp.js.map +1 -1
  28. package/dist/abi/test/Tir.js +167 -636
  29. package/dist/abi/test/Tir.js.map +1 -1
  30. package/dist/abi/test/TnT.js +842 -0
  31. package/dist/abi/test/TnT.js.map +1 -0
  32. package/dist/abi/test/Tpr.js +166 -942
  33. package/dist/abi/test/Tpr.js.map +1 -1
  34. package/dist/app.js +447 -443
  35. package/dist/app.js.map +1 -1
  36. package/dist/buildParam/{didV3.js → didr.js} +210 -204
  37. package/dist/buildParam/didr.js.map +1 -0
  38. package/dist/buildParam/index.js +42 -48
  39. package/dist/buildParam/index.js.map +1 -1
  40. package/dist/buildParam/ptr.js +80 -0
  41. package/dist/buildParam/ptr.js.map +1 -0
  42. package/dist/buildParam/tcr.js +36 -0
  43. package/dist/buildParam/tcr.js.map +1 -0
  44. package/dist/buildParam/{timestampV3.js → timestamp.js} +179 -136
  45. package/dist/buildParam/timestamp.js.map +1 -0
  46. package/dist/buildParam/{tirV3.js → tir.js} +67 -59
  47. package/dist/buildParam/tir.js.map +1 -0
  48. package/dist/buildParam/{tntV1.js → tnt.js} +90 -61
  49. package/dist/buildParam/tnt.js.map +1 -0
  50. package/dist/buildParam/{tprV2.js → tpr.js} +44 -33
  51. package/dist/buildParam/tpr.js.map +1 -0
  52. package/dist/buildParam/{tsrV2.js → tsr.js} +45 -40
  53. package/dist/buildParam/tsr.js.map +1 -0
  54. package/dist/cli.js +1 -0
  55. package/dist/cli.js.map +1 -1
  56. package/dist/commands/{authorisation-v5.js → authorisation.js} +95 -72
  57. package/dist/commands/authorisation.js.map +1 -0
  58. package/dist/commands/compute.js +429 -389
  59. package/dist/commands/compute.js.map +1 -1
  60. package/dist/commands/{conformance-v3.js → conformance.js} +236 -248
  61. package/dist/commands/conformance.js.map +1 -0
  62. package/dist/commands/hardwarewallet.js +5 -5
  63. package/dist/commands/hardwarewallet.js.map +1 -1
  64. package/dist/commands/index.js +4 -4
  65. package/dist/commands/index.js.map +1 -1
  66. package/dist/commands/{ledger-v4.js → ledger.js} +203 -127
  67. package/dist/commands/ledger.js.map +1 -0
  68. package/dist/commands/ptr.js +56 -0
  69. package/dist/commands/ptr.js.map +1 -0
  70. package/dist/commands/tcr.js +51 -0
  71. package/dist/commands/tcr.js.map +1 -0
  72. package/dist/commands/tnl.js +10 -9
  73. package/dist/commands/tnl.js.map +1 -1
  74. package/dist/commands/tsr.js +46 -0
  75. package/dist/commands/tsr.js.map +1 -0
  76. package/dist/commands/view.js +8 -8
  77. package/dist/commands/view.js.map +1 -1
  78. package/dist/config.js +311 -279
  79. package/dist/config.js.map +1 -1
  80. package/dist/index.js +3 -3
  81. package/dist/index.js.map +1 -1
  82. package/dist/interfaces/index.js +1 -1
  83. package/dist/interfaces/index.js.map +1 -1
  84. package/dist/programs/migrateDid.js +33 -33
  85. package/dist/programs/migrateDid.js.map +1 -1
  86. package/dist/programs/migrateDids.js +39 -42
  87. package/dist/programs/migrateDids.js.map +1 -1
  88. package/dist/programs/migrateTsr.js +153 -113
  89. package/dist/programs/migrateTsr.js.map +1 -1
  90. package/dist/scripts/accreditAndAuthorize/conformance/step1 +1 -1
  91. package/dist/scripts/accreditAndAuthorize/test/step1 +1 -1
  92. package/dist/scripts/accreditTI +1 -1
  93. package/dist/scripts/assets/CTRevocableCredential.json +1 -1
  94. package/dist/scripts/assets/CredentialToAttestVerifiableAuthorisationForTrustChain.json +2 -2
  95. package/dist/scripts/assets/TrustedNodesList.json +1 -1
  96. package/dist/scripts/assets/VerifiableAccreditationToAccredit.json +2 -2
  97. package/dist/scripts/assets/VerifiableAccreditationToAttest.json +2 -2
  98. package/dist/scripts/assets/VerifiableAuthorisationForTrustChain.json +1 -1
  99. package/dist/scripts/assets/VerifiableAuthorisationToInvoke.json +24 -0
  100. package/dist/scripts/assets/VerifiableAuthorisationToOnboard.json +1 -1
  101. package/dist/scripts/bootstrap/1-populateTPR +18 -15
  102. package/dist/scripts/bootstrap/6-setupConformanceIssuer +1 -1
  103. package/dist/scripts/issueVcInvoke +10 -0
  104. package/dist/scripts/issueVcOnboard +1 -1
  105. package/dist/scripts/issueVcRootTAO +1 -1
  106. package/dist/scripts/issueVcTAO +2 -2
  107. package/dist/scripts/issueVcTI +2 -2
  108. package/dist/scripts/issueVcTnl +1 -1
  109. package/dist/scripts/issue_CTRevocableCredential +1 -1
  110. package/dist/scripts/issue_SelfAttestationSupportOffice +2 -2
  111. package/dist/scripts/issue_VerifiableAccreditationToAccredit +2 -2
  112. package/dist/scripts/issue_VerifiableAccreditationToAttest +2 -2
  113. package/dist/scripts/issue_VerifiableAuthorisationForTrustChain +1 -1
  114. package/dist/scripts/issue_VerifiableAuthorisationToOnboard +1 -1
  115. package/dist/scripts/updateVcRootTAO +1 -1
  116. package/dist/scripts/updateVcTAO +2 -2
  117. package/dist/scripts/updateVcTI +2 -2
  118. package/dist/scripts/{wctv3 → wct}/accreditAndAuthorize +1 -1
  119. package/dist/tsconfig.build.tsbuildinfo +1 -0
  120. package/dist/utils/Client.js +81 -68
  121. package/dist/utils/Client.js.map +1 -1
  122. package/dist/utils/HardwareWallet.js +169 -165
  123. package/dist/utils/HardwareWallet.js.map +1 -1
  124. package/dist/utils/http.js +24 -27
  125. package/dist/utils/http.js.map +1 -1
  126. package/dist/utils/index.js +3 -3
  127. package/dist/utils/index.js.map +1 -1
  128. package/dist/utils/jsonrpc.js +14 -14
  129. package/dist/utils/jsonrpc.js.map +1 -1
  130. package/dist/utils/print.js +19 -19
  131. package/dist/utils/print.js.map +1 -1
  132. package/dist/utils/utils.js +180 -173
  133. package/dist/utils/utils.js.map +1 -1
  134. package/package.json +70 -46
  135. package/dist/abi/pilot/DidRegistryV3.js +0 -1082
  136. package/dist/abi/pilot/DidRegistryV3.js.map +0 -1
  137. package/dist/abi/pilot/DidRegistryV4.js +0 -942
  138. package/dist/abi/pilot/DidRegistryV4.js.map +0 -1
  139. package/dist/abi/pilot/SchemaSCRegistryV2.js +0 -474
  140. package/dist/abi/pilot/SchemaSCRegistryV2.js.map +0 -1
  141. package/dist/abi/pilot/TimestampV2.js +0 -1128
  142. package/dist/abi/pilot/TimestampV2.js.map +0 -1
  143. package/dist/abi/pilot/TimestampV3.js +0 -995
  144. package/dist/abi/pilot/TimestampV3.js.map +0 -1
  145. package/dist/abi/pilot/TirV3.js +0 -496
  146. package/dist/abi/pilot/TirV3.js.map +0 -1
  147. package/dist/abi/pilot/TirV4.js.map +0 -1
  148. package/dist/abi/pilot/TnTV1.js +0 -1375
  149. package/dist/abi/pilot/TnTV1.js.map +0 -1
  150. package/dist/abi/pilot/TprV2.js +0 -889
  151. package/dist/abi/pilot/TprV2.js.map +0 -1
  152. package/dist/abi/pilot/TprV3.js.map +0 -1
  153. package/dist/abi/pilot/TsrV3.js +0 -571
  154. package/dist/abi/pilot/TsrV3.js.map +0 -1
  155. package/dist/abi/test/DidRegistryV3.js +0 -1089
  156. package/dist/abi/test/DidRegistryV3.js.map +0 -1
  157. package/dist/abi/test/DidRegistryV4.js +0 -960
  158. package/dist/abi/test/DidRegistryV4.js.map +0 -1
  159. package/dist/abi/test/SchemaSCRegistryV2.js +0 -474
  160. package/dist/abi/test/SchemaSCRegistryV2.js.map +0 -1
  161. package/dist/abi/test/TimestampV2.js +0 -1128
  162. package/dist/abi/test/TimestampV2.js.map +0 -1
  163. package/dist/abi/test/TimestampV3.js +0 -995
  164. package/dist/abi/test/TimestampV3.js.map +0 -1
  165. package/dist/abi/test/TirV3.js +0 -496
  166. package/dist/abi/test/TirV3.js.map +0 -1
  167. package/dist/abi/test/TirV4.js.map +0 -1
  168. package/dist/abi/test/TnTV1.js +0 -1375
  169. package/dist/abi/test/TnTV1.js.map +0 -1
  170. package/dist/abi/test/TprV2.js +0 -889
  171. package/dist/abi/test/TprV2.js.map +0 -1
  172. package/dist/abi/test/TprV3.js.map +0 -1
  173. package/dist/abi/test/TsrV3.js +0 -571
  174. package/dist/abi/test/TsrV3.js.map +0 -1
  175. package/dist/buildParam/didV3.js.map +0 -1
  176. package/dist/buildParam/didV4.js +0 -326
  177. package/dist/buildParam/didV4.js.map +0 -1
  178. package/dist/buildParam/timestampV2.js +0 -317
  179. package/dist/buildParam/timestampV2.js.map +0 -1
  180. package/dist/buildParam/timestampV3.js.map +0 -1
  181. package/dist/buildParam/tirV3.js.map +0 -1
  182. package/dist/buildParam/tirV4.js +0 -119
  183. package/dist/buildParam/tirV4.js.map +0 -1
  184. package/dist/buildParam/tntV1.js.map +0 -1
  185. package/dist/buildParam/tntV2.js +0 -185
  186. package/dist/buildParam/tntV2.js.map +0 -1
  187. package/dist/buildParam/tprV2.js.map +0 -1
  188. package/dist/buildParam/tprV3.js +0 -82
  189. package/dist/buildParam/tprV3.js.map +0 -1
  190. package/dist/buildParam/tsrV2.js.map +0 -1
  191. package/dist/buildParam/tsrV3.js +0 -110
  192. package/dist/buildParam/tsrV3.js.map +0 -1
  193. package/dist/commands/authorisation-v4.js +0 -119
  194. package/dist/commands/authorisation-v4.js.map +0 -1
  195. package/dist/commands/authorisation-v5.js.map +0 -1
  196. package/dist/commands/conformance-v3.js.map +0 -1
  197. package/dist/commands/ledger-v4.js.map +0 -1
  198. package/dist/tsconfig.tsbuildinfo +0 -1
  199. /package/dist/scripts/{wctv3 → wct}/holderWallet +0 -0
  200. /package/dist/scripts/{wctv3 → wct}/issueToHolder +0 -0
  201. /package/dist/scripts/{wctv3 → wct}/pda1 +0 -0
  202. /package/dist/scripts/{wctv3 → wct}/verifier +0 -0
@@ -1,24 +1,16 @@
1
1
  import { exportJWK, generateKeyPair, importJWK, SignJWT } from "jose";
2
- import { encodePublicKey } from "./utils.js";
3
- export async function generateKeys(alg) {
4
- const keys = await generateKeyPair(alg);
5
- return {
6
- publicKeyJwk: await exportJWK(keys.publicKey),
7
- privateKeyJwk: await exportJWK(keys.privateKey),
8
- };
9
- }
2
+ import { encodePrivateKey, encodePublicKey } from "./utils.js";
10
3
  export class Client {
11
- keys;
12
- ethWallet;
13
- hardwareWallet;
14
- privateKeyHex;
15
- did;
16
- didVersion;
17
- clientId;
18
4
  accreditationId;
19
5
  accreditationUrl;
20
- proxyId;
6
+ clientId;
7
+ did;
8
+ didVersion;
9
+ ethWallet;
10
+ hardwareWallet;
21
11
  issuerState;
12
+ keys;
13
+ proxyId;
22
14
  constructor() {
23
15
  this.keys = {};
24
16
  }
@@ -26,26 +18,6 @@ export class Client {
26
18
  const { privateKeyJwk } = await generateKeys(alg);
27
19
  this.setJwk(alg, privateKeyJwk);
28
20
  }
29
- setJwk(alg, privateKeyJwk) {
30
- const publicKeyJwk = encodePublicKey({
31
- key: privateKeyJwk,
32
- format: "jwk",
33
- });
34
- const publicKeyPem = encodePublicKey({
35
- key: publicKeyJwk,
36
- alg,
37
- sourceType: "publicJwk",
38
- format: "pem",
39
- });
40
- this.keys[alg] = {
41
- id: "",
42
- kid: "",
43
- privateKeyJwk,
44
- publicKeyJwk,
45
- publicKeyPem,
46
- isHardwareWallet: false,
47
- };
48
- }
49
21
  generateDidDocument() {
50
22
  const context = [
51
23
  "https://www.w3.org/ns/did/v1",
@@ -53,35 +25,87 @@ export class Client {
53
25
  ];
54
26
  const didDocument = {
55
27
  "@context": context,
56
- id: this.did,
57
- verificationMethod: [],
58
- authentication: [],
59
28
  assertionMethod: [],
29
+ authentication: [],
60
30
  capabilityInvocation: [],
31
+ id: this.did,
32
+ verificationMethod: [],
61
33
  };
62
- Object.keys(this.keys).forEach((alg) => {
34
+ for (const alg of Object.keys(this.keys)) {
63
35
  const key = this.keys[alg];
64
36
  if (!key)
65
- return;
37
+ continue;
66
38
  didDocument.verificationMethod.push({
67
- id: key.kid,
68
- type: "JsonWebKey2020",
69
39
  controller: this.did,
40
+ id: key.kid,
70
41
  publicKeyJwk: key.publicKeyJwk,
42
+ type: "JsonWebKey2020",
71
43
  });
72
44
  didDocument.authentication.push(key.kid);
73
45
  didDocument.assertionMethod.push(key.kid);
74
46
  didDocument.capabilityInvocation.push(key.kid);
75
- });
47
+ }
76
48
  return didDocument;
77
49
  }
50
+ privateKeysBase64() {
51
+ const keys = [];
52
+ for (const alg of Object.keys(this.keys)) {
53
+ const key = this.keys[alg];
54
+ if (!key)
55
+ continue;
56
+ keys.push({
57
+ alg,
58
+ id: key.kid,
59
+ privateKeyJwk: key.privateKeyJwk,
60
+ publicKeyJwk: key.publicKeyJwk,
61
+ type: "JsonWebKey2020",
62
+ });
63
+ }
64
+ return Buffer.from(JSON.stringify(keys)).toString("base64");
65
+ }
66
+ setJwk(alg, privateKeyJwk) {
67
+ const publicKeyJwk = encodePublicKey({
68
+ format: "jwk",
69
+ key: privateKeyJwk,
70
+ });
71
+ const publicKeyPem = encodePublicKey({
72
+ alg,
73
+ format: "pem",
74
+ key: publicKeyJwk,
75
+ sourceType: "publicJwk",
76
+ });
77
+ let publicKeyHex = "";
78
+ let privateKeyHex = "";
79
+ if (["ES256", "ES256K"].includes(alg)) {
80
+ privateKeyHex = encodePrivateKey({
81
+ alg,
82
+ format: "hex",
83
+ key: privateKeyJwk,
84
+ });
85
+ publicKeyHex = encodePublicKey({
86
+ alg,
87
+ format: "hex",
88
+ key: publicKeyJwk,
89
+ });
90
+ }
91
+ this.keys[alg] = {
92
+ id: "",
93
+ isHardwareWallet: false,
94
+ kid: "",
95
+ ...(privateKeyHex && { privateKeyHex }),
96
+ privateKeyJwk,
97
+ ...(publicKeyHex && { publicKeyHex }),
98
+ publicKeyJwk,
99
+ publicKeyPem,
100
+ };
101
+ }
78
102
  async signJWT(payload, inputHeader, alg) {
79
103
  if (!this.keys[alg])
80
104
  throw new Error(`There is no key defined for alg ${alg}`);
81
105
  const header = {
82
106
  alg,
83
- typ: "JWT",
84
107
  kid: this.keys[alg].kid,
108
+ typ: "JWT",
85
109
  ...(this.didVersion === 2 && {
86
110
  jwk: this.keys[alg].publicKeyJwk,
87
111
  }),
@@ -98,37 +122,26 @@ export class Client {
98
122
  .sign(privateKey);
99
123
  return jwt;
100
124
  }
101
- privateKeysBase64() {
102
- const keys = [];
103
- Object.keys(this.keys).forEach((alg) => {
104
- const key = this.keys[alg];
105
- if (!key)
106
- return;
107
- keys.push({
108
- type: "JsonWebKey2020",
109
- id: key.kid,
110
- alg,
111
- privateKeyJwk: key.privateKeyJwk,
112
- publicKeyJwk: key.publicKeyJwk,
113
- });
114
- });
115
- return Buffer.from(JSON.stringify(keys)).toString("base64");
116
- }
117
125
  toJSON() {
118
126
  return {
119
- keys: this.keys,
120
- privateKeyHex: this.ethWallet?.privateKey ?? "",
121
- publicKeyHex: this.ethWallet?.publicKey ?? "",
127
+ accreditationId: this.accreditationId,
128
+ accreditationUrl: this.accreditationUrl,
122
129
  address: this.ethWallet?.address ?? "",
130
+ clientId: this.clientId,
123
131
  did: this.did,
124
132
  didVersion: this.didVersion,
125
- clientId: this.clientId,
126
- proxyId: this.proxyId,
127
- accreditationId: this.accreditationId,
128
- accreditationUrl: this.accreditationUrl,
129
133
  issuerState: this.issuerState,
134
+ keys: this.keys,
135
+ proxyId: this.proxyId,
130
136
  };
131
137
  }
132
138
  }
139
+ export async function generateKeys(alg) {
140
+ const keys = await generateKeyPair(alg);
141
+ return {
142
+ privateKeyJwk: await exportJWK(keys.privateKey),
143
+ publicKeyJwk: await exportJWK(keys.publicKey),
144
+ };
145
+ }
133
146
  export default Client;
134
147
  //# sourceMappingURL=Client.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Client.js","sourceRoot":"","sources":["../../src/utils/Client.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAY,eAAe,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAEhF,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAY7C,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,GAAW;IAI5C,MAAM,IAAI,GAAG,MAAM,eAAe,CAAC,GAAG,CAAC,CAAC;IACxC,OAAO;QACL,YAAY,EAAE,MAAM,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC;QAC7C,aAAa,EAAE,MAAM,SAAS,CAAC,IAAI,CAAC,UAAU,CAAC;KAChD,CAAC;AACJ,CAAC;AAED,MAAM,OAAO,MAAM;IACjB,IAAI,CAKF;IAEF,SAAS,CAAgB;IAEzB,cAAc,CAAiB;IAE/B,aAAa,CAAS;IAEtB,GAAG,CAAS;IAEZ,UAAU,CAAS;IAEnB,QAAQ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AACJ,CAAC;AAED,eAAe,MAAM,CAAC"}
@@ -1,48 +1,17 @@
1
1
  import * as asn1 from "asn1js";
2
- import * as pki from "pkijs";
3
- import crypto from "crypto";
4
- import { base64url } from "multiformats/bases/base64";
5
2
  import Joi from "joi";
6
- import { yellow, red } from "./print.js";
3
+ import { base64url } from "multiformats/bases/base64";
4
+ import crypto from "node:crypto";
5
+ import * as pki from "pkijs";
6
+ import { red, yellow } from "./print.js";
7
7
  import { askQuestion } from "./utils.js";
8
- function convertRawSignatureToDER(rawSignature) {
9
- if (rawSignature.length !== 64) {
10
- throw new Error("Raw signature must be 64 bytes (32 bytes for r and 32 bytes for s)");
11
- }
12
- // Split raw signature into r and s
13
- const r = rawSignature.subarray(0, 32);
14
- const s = rawSignature.subarray(32);
15
- // Function to add leading zero if needed
16
- function toDERInteger(value) {
17
- if (value[0] & 0x80) {
18
- // If the first byte is >= 0x80, prepend 0x00 to indicate a positive integer
19
- return Buffer.concat([Buffer.from([0x00]), value]);
20
- }
21
- return value;
22
- }
23
- // Convert r and s to DER integers
24
- const rDER = toDERInteger(r);
25
- const sDER = toDERInteger(s);
26
- // Create DER-encoded SEQUENCE containing the r and s values
27
- const derSignature = Buffer.concat([
28
- Buffer.from([0x30]), // ASN.1 SEQUENCE tag
29
- Buffer.from([rDER.length + sDER.length + 4]), // Total length of SEQUENCE
30
- Buffer.from([0x02]), // ASN.1 INTEGER tag for r
31
- Buffer.from([rDER.length]), // Length of r
32
- rDER, // r value
33
- Buffer.from([0x02]), // ASN.1 INTEGER tag for s
34
- Buffer.from([sDER.length]), // Length of s
35
- sDER, // s value
36
- ]);
37
- return derSignature;
38
- }
39
8
  export class HardwareWallet {
40
9
  graphene;
41
10
  mod;
42
- slots;
11
+ session;
43
12
  slot;
44
13
  slotNumber;
45
- session;
14
+ slots;
46
15
  constructor(library) {
47
16
  this.loadGraphene()
48
17
  .then(() => {
@@ -52,69 +21,31 @@ export class HardwareWallet {
52
21
  console.warn("Failed to load graphene-pk11");
53
22
  });
54
23
  }
55
- async loadGraphene() {
56
- // eslint-disable-next-line import/no-extraneous-dependencies
57
- this.graphene = await import("graphene-pk11");
58
- if (!this.graphene) {
59
- throw new Error("Failed to load graphene-pk11");
60
- }
61
- }
62
- init() {
63
- if (!this.mod) {
64
- throw new Error("Hardware wallet cannot be used because the graphene-pk11 package was not loaded");
65
- }
66
- this.endSession();
67
- this.mod.initialize();
68
- this.slotNumber = 0;
69
- // get slots
70
- this.slots = this.mod.getSlots(true);
71
- if (this.slots.length === 0) {
72
- this.mod.finalize();
73
- throw new Error("No slots found. Make sure your Ledger is unlocked and the OpenPGP app is open.");
74
- }
75
- this.slot = this.slots.items(this.slotNumber);
76
- if (!(this.slot.flags & this.graphene.SlotFlag.TOKEN_PRESENT)) {
77
- this.mod.finalize();
78
- throw new Error(`Slot ${this.slotNumber} is not initialized`);
79
- }
80
- this.session = this.slot.open(this.graphene.SessionFlag.RW_SESSION |
81
- this.graphene.SessionFlag.SERIAL_SESSION);
24
+ static verify2(message, signature, publicKeyJwk) {
25
+ const derFormatSig = convertRawSignatureToDER(signature);
26
+ const isValid = crypto.verify("sha256", Buffer.from(message), {
27
+ format: "jwk",
28
+ key: publicKeyJwk,
29
+ }, derFormatSig);
30
+ return isValid;
82
31
  }
83
- async login(userType, loginMessage) {
84
- let attemptsLeft = 3;
85
- const isAdmin = userType === "admin";
86
- let pin = "";
87
- let validPin = false;
88
- let loginMsg = isAdmin ? "Enter admin pin: " : "Enter pin: ";
89
- if (loginMessage)
90
- loginMsg = loginMessage;
91
- while (!validPin) {
92
- try {
93
- pin = await askQuestion(loginMsg, true);
94
- this.session.login(pin, isAdmin ? this.graphene.UserType.SO : this.graphene.UserType.USER);
95
- validPin = true;
96
- }
97
- catch (error) {
98
- validPin = false;
99
- if (error instanceof Error) {
100
- if (error.message === "CKR_PIN_LEN_RANGE") {
101
- red("Invalid PIN length");
102
- }
103
- else if (error.message === "CKR_USER_NOT_LOGGED_IN") {
104
- attemptsLeft -= 1;
105
- red(`Invalid PIN. You have ${attemptsLeft} attempts before the PIN is locked`);
106
- }
107
- else if (error.message === "CKR_PIN_LOCKED") {
108
- throw new Error(`The PIN is locked. If you want to reset it, open OpenPGP app, go to "Settings" > "Reset" and select "YES"`);
109
- }
110
- else if (error.message === "CKR_GENERAL_ERROR") {
111
- throw new Error(`Hardware wallet error. Make sure your Ledger is unlocked and the OpenPGP app is open`);
112
- }
113
- throw error;
114
- }
115
- }
32
+ static verifyJwt(jwt, publicKeyJwk) {
33
+ const jwtParts = jwt.split(".");
34
+ if (jwtParts.length !== 3)
35
+ throw new Error("Invalid JWT format");
36
+ const [headerBase64url, payloadBase64url, signatureBase64url] = jwtParts;
37
+ const message = `${headerBase64url}.${payloadBase64url}`;
38
+ const headerString = Buffer.from(base64url.baseDecode(headerBase64url)).toString();
39
+ const payloadString = Buffer.from(base64url.baseDecode(payloadBase64url)).toString();
40
+ const signature = Buffer.from(base64url.baseDecode(signatureBase64url));
41
+ const result = HardwareWallet.verify2(message, signature, publicKeyJwk);
42
+ if (!result) {
43
+ throw new Error("Invalid signature");
116
44
  }
117
- return pin;
45
+ return {
46
+ header: JSON.parse(headerString),
47
+ payload: JSON.parse(payloadString),
48
+ };
118
49
  }
119
50
  endSession() {
120
51
  try {
@@ -130,49 +61,6 @@ export class HardwareWallet {
130
61
  // empty
131
62
  }
132
63
  }
133
- async setPin(user) {
134
- Joi.assert(user, Joi.string().valid("user", "admin"));
135
- this.init();
136
- const pin = await this.login(user, `Enter current pin for ${user}: `);
137
- const newPin = await askQuestion("Enter new pin: ", true);
138
- try {
139
- this.session.setPin(pin, newPin);
140
- this.endSession();
141
- yellow(user === "user"
142
- ? "New pin configured!"
143
- : "New pin configured for admin!");
144
- }
145
- catch (error) {
146
- if (error instanceof Error) {
147
- if (error.message === "CKR_PIN_LEN_RANGE") {
148
- throw new Error("Invalid PIN length");
149
- }
150
- else if (error.message === "CKR_GENERAL_ERROR") {
151
- throw new Error(`Hardware wallet error. Make sure your Ledger is unlocked and the OpenPGP app is open`);
152
- }
153
- else {
154
- throw error;
155
- }
156
- }
157
- }
158
- }
159
- getInfo() {
160
- this.init();
161
- const info = {
162
- slot: this.slotNumber,
163
- description: this.slot.slotDescription,
164
- serial: this.slot.getToken().serialNumber,
165
- password: {
166
- min: this.slot.getToken().minPinLen,
167
- max: this.slot.getToken().maxPinLen,
168
- },
169
- isHardware: Boolean(this.slot.flags & this.graphene.SlotFlag.HW_SLOT),
170
- isRemovable: Boolean(this.slot.flags & this.graphene.SlotFlag.REMOVABLE_DEVICE),
171
- isInitialized: Boolean(this.slot.flags & this.graphene.SlotFlag.TOKEN_PRESENT),
172
- };
173
- this.endSession();
174
- return info;
175
- }
176
64
  async generateKeys() {
177
65
  this.init();
178
66
  await this.login("admin");
@@ -218,6 +106,23 @@ export class HardwareWallet {
218
106
  yellow("Key pair generated successfully!");
219
107
  this.endSession();
220
108
  }
109
+ getInfo() {
110
+ this.init();
111
+ const info = {
112
+ description: this.slot.slotDescription,
113
+ isHardware: Boolean(this.slot.flags & this.graphene.SlotFlag.HW_SLOT),
114
+ isInitialized: Boolean(this.slot.flags & this.graphene.SlotFlag.TOKEN_PRESENT),
115
+ isRemovable: Boolean(this.slot.flags & this.graphene.SlotFlag.REMOVABLE_DEVICE),
116
+ password: {
117
+ max: this.slot.getToken().maxPinLen,
118
+ min: this.slot.getToken().minPinLen,
119
+ },
120
+ serial: this.slot.getToken().serialNumber,
121
+ slot: this.slotNumber,
122
+ };
123
+ this.endSession();
124
+ return info;
125
+ }
221
126
  getPublicKey() {
222
127
  this.init();
223
128
  const publicKeys = this.session.find({
@@ -241,6 +146,100 @@ export class HardwareWallet {
241
146
  this.endSession();
242
147
  return { kty: "EC", ...publicKeyDecoded.toJSON() };
243
148
  }
149
+ init() {
150
+ if (!this.mod) {
151
+ throw new Error("Hardware wallet cannot be used because the graphene-pk11 package was not loaded");
152
+ }
153
+ this.endSession();
154
+ this.mod.initialize();
155
+ this.slotNumber = 0;
156
+ // get slots
157
+ this.slots = this.mod.getSlots(true);
158
+ if (this.slots.length === 0) {
159
+ this.mod.finalize();
160
+ throw new Error("No slots found. Make sure your Ledger is unlocked and the OpenPGP app is open.");
161
+ }
162
+ this.slot = this.slots.items(this.slotNumber);
163
+ if (!(this.slot.flags & this.graphene.SlotFlag.TOKEN_PRESENT)) {
164
+ this.mod.finalize();
165
+ throw new Error(`Slot ${this.slotNumber} is not initialized`);
166
+ }
167
+ this.session = this.slot.open(this.graphene.SessionFlag.RW_SESSION |
168
+ this.graphene.SessionFlag.SERIAL_SESSION);
169
+ }
170
+ async loadGraphene() {
171
+ this.graphene = await import("graphene-pk11");
172
+ if (!this.graphene) {
173
+ throw new Error("Failed to load graphene-pk11");
174
+ }
175
+ }
176
+ async login(userType, loginMessage) {
177
+ let attemptsLeft = 3;
178
+ const isAdmin = userType === "admin";
179
+ let pin = "";
180
+ let validPin = false;
181
+ let loginMsg = isAdmin ? "Enter admin pin: " : "Enter pin: ";
182
+ if (loginMessage)
183
+ loginMsg = loginMessage;
184
+ while (!validPin) {
185
+ try {
186
+ pin = await askQuestion(loginMsg, true);
187
+ this.session.login(pin, isAdmin ? this.graphene.UserType.SO : this.graphene.UserType.USER);
188
+ validPin = true;
189
+ }
190
+ catch (error) {
191
+ validPin = false;
192
+ if (error instanceof Error) {
193
+ switch (error.message) {
194
+ case "CKR_PIN_LEN_RANGE": {
195
+ red("Invalid PIN length");
196
+ break;
197
+ }
198
+ case "CKR_USER_NOT_LOGGED_IN": {
199
+ attemptsLeft -= 1;
200
+ red(`Invalid PIN. You have ${attemptsLeft} attempts before the PIN is locked`);
201
+ break;
202
+ }
203
+ case "CKR_PIN_LOCKED": {
204
+ throw new Error(`The PIN is locked. If you want to reset it, open OpenPGP app, go to "Settings" > "Reset" and select "YES"`);
205
+ }
206
+ case "CKR_GENERAL_ERROR": {
207
+ throw new Error(`Hardware wallet error. Make sure your Ledger is unlocked and the OpenPGP app is open`);
208
+ }
209
+ // No default
210
+ }
211
+ throw error;
212
+ }
213
+ }
214
+ }
215
+ return pin;
216
+ }
217
+ async setPin(user) {
218
+ Joi.assert(user, Joi.string().valid("user", "admin"));
219
+ this.init();
220
+ const pin = await this.login(user, `Enter current pin for ${user}: `);
221
+ const newPin = await askQuestion("Enter new pin: ", true);
222
+ try {
223
+ this.session.setPin(pin, newPin);
224
+ this.endSession();
225
+ yellow(user === "user"
226
+ ? "New pin configured!"
227
+ : "New pin configured for admin!");
228
+ }
229
+ catch (error) {
230
+ if (error instanceof Error) {
231
+ if (error.message === "CKR_PIN_LEN_RANGE") {
232
+ throw new Error("Invalid PIN length");
233
+ }
234
+ else if (error.message === "CKR_GENERAL_ERROR") {
235
+ throw new Error(`Hardware wallet error. Make sure your Ledger is unlocked and the OpenPGP app is open`);
236
+ }
237
+ else {
238
+ throw error;
239
+ }
240
+ }
241
+ }
242
+ }
244
243
  async sign(message) {
245
244
  this.init();
246
245
  await this.login("user");
@@ -290,32 +289,37 @@ export class HardwareWallet {
290
289
  this.endSession();
291
290
  return isValid;
292
291
  }
293
- static verify2(message, signature, publicKeyJwk) {
294
- const derFormatSig = convertRawSignatureToDER(signature);
295
- const isValid = crypto.verify("sha256", Buffer.from(message), {
296
- format: "jwk",
297
- key: publicKeyJwk,
298
- }, derFormatSig);
299
- return isValid;
292
+ }
293
+ function convertRawSignatureToDER(rawSignature) {
294
+ if (rawSignature.length !== 64) {
295
+ throw new Error("Raw signature must be 64 bytes (32 bytes for r and 32 bytes for s)");
300
296
  }
301
- static verifyJwt(jwt, publicKeyJwk) {
302
- const jwtParts = jwt.split(".");
303
- if (jwtParts.length !== 3)
304
- throw new Error("Invalid JWT format");
305
- const [headerBase64url, payloadBase64url, signatureBase64url] = jwtParts;
306
- const message = `${headerBase64url}.${payloadBase64url}`;
307
- const headerString = Buffer.from(base64url.baseDecode(headerBase64url)).toString();
308
- const payloadString = Buffer.from(base64url.baseDecode(payloadBase64url)).toString();
309
- const signature = Buffer.from(base64url.baseDecode(signatureBase64url));
310
- const result = HardwareWallet.verify2(message, signature, publicKeyJwk);
311
- if (!result) {
312
- throw new Error("Invalid signature");
313
- }
314
- return {
315
- header: JSON.parse(headerString),
316
- payload: JSON.parse(payloadString),
317
- };
297
+ // Split raw signature into r and s
298
+ const r = rawSignature.subarray(0, 32);
299
+ const s = rawSignature.subarray(32);
300
+ // Convert r and s to DER integers
301
+ const rDER = toDERInteger(r);
302
+ const sDER = toDERInteger(s);
303
+ // Create DER-encoded SEQUENCE containing the r and s values
304
+ const derSignature = Buffer.concat([
305
+ Buffer.from([0x30]), // ASN.1 SEQUENCE tag
306
+ Buffer.from([rDER.length + sDER.length + 4]), // Total length of SEQUENCE
307
+ Buffer.from([0x02]), // ASN.1 INTEGER tag for r
308
+ Buffer.from([rDER.length]), // Length of r
309
+ rDER, // r value
310
+ Buffer.from([0x02]), // ASN.1 INTEGER tag for s
311
+ Buffer.from([sDER.length]), // Length of s
312
+ sDER, // s value
313
+ ]);
314
+ return derSignature;
315
+ }
316
+ // Function to add leading zero if needed
317
+ function toDERInteger(value) {
318
+ if (value[0] & 0x80) {
319
+ // If the first byte is >= 0x80, prepend 0x00 to indicate a positive integer
320
+ return Buffer.concat([Buffer.from([0x00]), value]);
318
321
  }
322
+ return value;
319
323
  }
320
324
  export default HardwareWallet;
321
325
  //# sourceMappingURL=HardwareWallet.js.map