mol_crypto_lib 0.0.948 → 0.1.950

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/node.js CHANGED
@@ -928,58 +928,62 @@ var $;
928
928
  "use strict";
929
929
  var $;
930
930
  (function ($) {
931
- const algorithm = {
932
- name: 'RSA-OAEP',
933
- modulusLength: 1024,
934
- publicExponent: new Uint8Array([1, 0, 1]),
935
- hash: 'SHA-256',
936
- };
937
- async function $mol_crypto_cipher_pair() {
938
- const pair = await $mol_crypto_native.subtle.generateKey(algorithm, true, ['encrypt', 'decrypt']);
939
- return {
940
- public: new $mol_crypto_cipher_public(pair.publicKey),
941
- private: new $mol_crypto_cipher_private(pair.privateKey),
942
- };
931
+ })($ || ($ = {}));
932
+ //mol/dom/context/context.ts
933
+ ;
934
+ "use strict";
935
+ var $;
936
+ (function ($) {
937
+ $.$mol_dom_context = new $node.jsdom.JSDOM('', { url: 'https://localhost/' }).window;
938
+ })($ || ($ = {}));
939
+ //mol/dom/context/context.node.ts
940
+ ;
941
+ "use strict";
942
+ var $;
943
+ (function ($) {
944
+ async function $mol_crypto_secret_id() {
945
+ const signed = this.$mol_dom_context.localStorage.getItem('$mol_crypto_secret');
946
+ if (signed === '')
947
+ return await this.$mol_crypto_secret_id_get();
948
+ const id = await this.$mol_crypto_secret_id_new();
949
+ this.$mol_dom_context.localStorage.setItem('$mol_crypto_secret', '');
950
+ return id;
943
951
  }
944
- $.$mol_crypto_cipher_pair = $mol_crypto_cipher_pair;
945
- class $mol_crypto_cipher_public extends Object {
946
- native;
947
- static size = 162;
948
- constructor(native) {
949
- super();
950
- this.native = native;
951
- }
952
- static async from(serial) {
953
- return new this(await $mol_crypto_native.subtle.importKey('spki', serial, algorithm, true, ['encrypt']));
954
- }
955
- async serial() {
956
- return await $mol_crypto_native.subtle.exportKey('spki', this.native);
957
- }
958
- async encrypt(data) {
959
- return await $mol_crypto_native.subtle.encrypt(algorithm, this.native, data);
960
- }
952
+ $.$mol_crypto_secret_id = $mol_crypto_secret_id;
953
+ async function $mol_crypto_secret_id_new() {
954
+ const cred = await this.$mol_dom_context.navigator.credentials.create({
955
+ publicKey: {
956
+ rp: {
957
+ name: "$mol_crypto_id",
958
+ },
959
+ user: {
960
+ id: new Uint8Array([0]),
961
+ name: "",
962
+ displayName: ""
963
+ },
964
+ pubKeyCredParams: [
965
+ { type: "public-key", alg: -7 },
966
+ { type: "public-key", alg: -257 },
967
+ ],
968
+ challenge: new Uint8Array().buffer,
969
+ },
970
+ });
971
+ return $mol_crypto_secret.from(cred.rawId);
961
972
  }
962
- $.$mol_crypto_cipher_public = $mol_crypto_cipher_public;
963
- class $mol_crypto_cipher_private extends Object {
964
- native;
965
- constructor(native) {
966
- super();
967
- this.native = native;
968
- }
969
- static async from(serial) {
970
- return new this(await $mol_crypto_native.subtle.importKey('pkcs8', serial, algorithm, true, ['decrypt']));
971
- }
972
- async serial() {
973
- return await $mol_crypto_native.subtle.exportKey('pkcs8', this.native);
974
- }
975
- async decrypt(data) {
976
- return await $mol_crypto_native.subtle.decrypt(algorithm, this.native, data);
977
- }
973
+ $.$mol_crypto_secret_id_new = $mol_crypto_secret_id_new;
974
+ async function $mol_crypto_secret_id_get() {
975
+ const cred = await this.$mol_dom_context.navigator.credentials.get({
976
+ mediation: 'silent',
977
+ publicKey: {
978
+ userVerification: 'discouraged',
979
+ challenge: new Uint8Array().buffer,
980
+ },
981
+ });
982
+ return $mol_crypto_secret.from(cred.rawId);
978
983
  }
979
- $.$mol_crypto_cipher_private = $mol_crypto_cipher_private;
980
- $.$mol_crypto_cipher_ecrypted_size = 128;
984
+ $.$mol_crypto_secret_id_get = $mol_crypto_secret_id_get;
981
985
  })($ || ($ = {}));
982
- //mol/crypto/cipher/cipher.ts
986
+ //mol/crypto/secret/id/id.ts
983
987
  ;
984
988
  "use strict";
985
989
  var $;
@@ -1031,6 +1035,134 @@ var $;
1031
1035
  ;
1032
1036
  "use strict";
1033
1037
  var $;
1038
+ (function ($) {
1039
+ function $mol_base64_ae_encode(buffer) {
1040
+ return $mol_base64_encode(buffer).replace(/\+/g, 'æ').replace(/\//g, 'Æ').replace(/=/g, '');
1041
+ }
1042
+ $.$mol_base64_ae_encode = $mol_base64_ae_encode;
1043
+ function $mol_base64_ae_decode(str) {
1044
+ return $mol_base64_decode(str.replace(/æ/g, '+').replace(/Æ/g, '/'));
1045
+ }
1046
+ $.$mol_base64_ae_decode = $mol_base64_ae_decode;
1047
+ })($ || ($ = {}));
1048
+ //mol/base64/ae/ae.ts
1049
+ ;
1050
+ "use strict";
1051
+ var $;
1052
+ (function ($) {
1053
+ class $mol_buffer extends DataView {
1054
+ static from(array) {
1055
+ if (typeof array === 'string')
1056
+ array = $mol_base64_ae_decode(array);
1057
+ return new this(array.buffer, array.byteOffset, array.byteLength);
1058
+ }
1059
+ static toString() {
1060
+ return $$.$mol_func_name(this);
1061
+ }
1062
+ getUint48(offset, LE = false) {
1063
+ if (offset % 4) {
1064
+ return this.getUint16(offset, LE) + this.getUint32(offset + 2, LE) * 2 ** 16;
1065
+ }
1066
+ else {
1067
+ return this.getUint32(offset, LE) + this.getUint16(offset + 4, LE) * 2 ** 32;
1068
+ }
1069
+ }
1070
+ setUint48(offset, value, LE = false) {
1071
+ if (offset % 4) {
1072
+ this.setUint16(offset, value & ((1 << 16) - 1), LE);
1073
+ this.setUint32(offset + 2, (value / 2 ** 16) | 0, LE);
1074
+ }
1075
+ else {
1076
+ this.setUint32(offset, value | 0, LE);
1077
+ this.setUint16(offset + 4, (value / 2 ** 32) | 0, LE);
1078
+ }
1079
+ }
1080
+ int8(offset, next) {
1081
+ if (next === undefined)
1082
+ return this.getInt8(offset);
1083
+ if (next >= -(2 ** 7) && next < 2 ** 7)
1084
+ return this.setInt8(offset, next), next;
1085
+ $mol_fail(new Error(`Wrong int8 value ${next}`));
1086
+ }
1087
+ uint8(offset, next) {
1088
+ if (next === undefined)
1089
+ return this.getUint8(offset);
1090
+ if (next >= 0 && next < 2 ** 8)
1091
+ return this.setUint8(offset, next), next;
1092
+ $mol_fail(new Error(`Wrong uint8 value ${next}`));
1093
+ }
1094
+ int16(offset, next) {
1095
+ if (next === undefined)
1096
+ return this.getInt16(offset, !!'LE');
1097
+ if (next >= -(2 ** 15) && next < 2 ** 15)
1098
+ return this.setInt16(offset, next, !!'LE'), next;
1099
+ $mol_fail(new Error(`Wrong int16 value ${next}`));
1100
+ }
1101
+ uint16(offset, next) {
1102
+ if (next === undefined)
1103
+ return this.getUint16(offset, !!'LE');
1104
+ if (next >= 0 && next < 2 ** 16)
1105
+ return this.setUint16(offset, next, !!'LE'), next;
1106
+ $mol_fail(new Error(`Wrong uint16 value ${next}`));
1107
+ }
1108
+ int32(offset, next) {
1109
+ if (next === undefined)
1110
+ return this.getInt32(offset, !!'LE');
1111
+ if (next >= -(2 ** 31) && next < 2 ** 31)
1112
+ return this.setInt32(offset, next, !!'LE'), next;
1113
+ $mol_fail(new Error(`Wrong int32 value ${next}`));
1114
+ }
1115
+ uint32(offset, next) {
1116
+ if (next === undefined)
1117
+ return this.getUint32(offset, !!'LE');
1118
+ if (next >= 0 && next < 2 ** 32)
1119
+ return this.setUint32(offset, next, !!'LE'), next;
1120
+ $mol_fail(new Error(`Wrong uint32 value ${next}`));
1121
+ }
1122
+ uint48(offset, next) {
1123
+ if (next === undefined)
1124
+ return this.getUint48(offset, !!'LE');
1125
+ if (next >= 0 && next < 2 ** 48)
1126
+ return this.setUint48(offset, next, !!'LE'), next;
1127
+ $mol_fail(new Error(`Wrong uint48 value ${next}`));
1128
+ }
1129
+ int64(offset, next) {
1130
+ if (next === undefined)
1131
+ return this.getBigInt64(offset, !!'LE');
1132
+ if (next >= -(2 ** 63) && next < 2 ** 63)
1133
+ return this.setBigInt64(offset, next, !!'LE'), next;
1134
+ $mol_fail(new Error(`Wrong int64 value ${next}`));
1135
+ }
1136
+ uint64(offset, next) {
1137
+ if (next === undefined)
1138
+ return this.getBigUint64(offset, !!'LE');
1139
+ if (next >= 0 && next < 2 ** 64)
1140
+ return this.setBigUint64(offset, next, !!'LE'), next;
1141
+ $mol_fail(new Error(`Wrong uint64 value ${next}`));
1142
+ }
1143
+ float32(offset, next) {
1144
+ if (next !== undefined)
1145
+ this.setFloat32(offset, next, !!'LE');
1146
+ return this.getFloat32(offset, !!'LE');
1147
+ }
1148
+ float64(offset, next) {
1149
+ if (next !== undefined)
1150
+ this.setFloat64(offset, next, !!'LE');
1151
+ return this.getFloat64(offset, !!'LE');
1152
+ }
1153
+ asArray() {
1154
+ return new Uint8Array(this.buffer, this.byteOffset, this.byteLength);
1155
+ }
1156
+ toString() {
1157
+ return $mol_base64_ae_encode(this.asArray());
1158
+ }
1159
+ }
1160
+ $.$mol_buffer = $mol_buffer;
1161
+ })($ || ($ = {}));
1162
+ //mol/buffer/buffer.ts
1163
+ ;
1164
+ "use strict";
1165
+ var $;
1034
1166
  (function ($) {
1035
1167
  function $mol_base64_url_encode(buffer) {
1036
1168
  return $mol_base64_encode(buffer).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
@@ -1045,109 +1177,153 @@ var $;
1045
1177
  ;
1046
1178
  "use strict";
1047
1179
  var $;
1180
+ (function ($) {
1181
+ class $mol_wrapper extends $mol_object2 {
1182
+ static wrap;
1183
+ static run(task) {
1184
+ return this.func(task)();
1185
+ }
1186
+ static func(func) {
1187
+ return this.wrap(func);
1188
+ }
1189
+ static get class() {
1190
+ return (Class) => {
1191
+ const construct = (target, args) => new Class(...args);
1192
+ const handler = {
1193
+ construct: this.func(construct)
1194
+ };
1195
+ handler[Symbol.toStringTag] = Class.name + '#';
1196
+ return new Proxy(Class, handler);
1197
+ };
1198
+ }
1199
+ static get method() {
1200
+ return (obj, name, descr) => {
1201
+ descr.value = this.func(descr.value);
1202
+ return descr;
1203
+ };
1204
+ }
1205
+ static get field() {
1206
+ return (obj, name, descr) => {
1207
+ descr.get = descr.set = this.func(descr.get);
1208
+ return descr;
1209
+ };
1210
+ }
1211
+ }
1212
+ $.$mol_wrapper = $mol_wrapper;
1213
+ })($ || ($ = {}));
1214
+ //mol/wrapper/wrapper.ts
1215
+ ;
1216
+ "use strict";
1217
+ var $;
1218
+ (function ($) {
1219
+ class $mol_memo extends $mol_wrapper {
1220
+ static wrap(task) {
1221
+ const store = new WeakMap();
1222
+ return function (next) {
1223
+ if (next === undefined && store.has(this))
1224
+ return store.get(this);
1225
+ const val = task.call(this, next) ?? next;
1226
+ store.set(this, val);
1227
+ return val;
1228
+ };
1229
+ }
1230
+ }
1231
+ $.$mol_memo = $mol_memo;
1232
+ })($ || ($ = {}));
1233
+ //mol/memo/memo.ts
1234
+ ;
1235
+ "use strict";
1236
+ var $;
1048
1237
  (function ($) {
1049
1238
  const algorithm = {
1050
1239
  name: 'ECDSA',
1051
1240
  hash: 'SHA-256',
1052
1241
  namedCurve: "P-256",
1053
1242
  };
1054
- async function $mol_crypto_auditor_pair() {
1055
- const pair = await $mol_crypto_native.subtle.generateKey(algorithm, true, ['sign', 'verify']);
1056
- return {
1057
- public: new $mol_crypto_auditor_public(pair.publicKey),
1058
- private: new $mol_crypto_auditor_private(pair.privateKey),
1059
- };
1243
+ class $mol_crypto_key extends $mol_buffer {
1244
+ static from(serial) {
1245
+ if (typeof serial === 'string') {
1246
+ serial = new Uint8Array([
1247
+ ...$mol_base64_url_decode(serial.slice(0, 43)),
1248
+ ...$mol_base64_url_decode(serial.slice(43, 86)),
1249
+ ...$mol_base64_url_decode(serial.slice(86, 129)),
1250
+ ]);
1251
+ }
1252
+ return new this(serial.buffer, serial.byteOffset, serial.byteLength);
1253
+ }
1254
+ asArray() {
1255
+ return new Uint8Array(this.buffer, this.byteOffset, this.byteLength);
1256
+ }
1257
+ toString() {
1258
+ const arr = this.asArray();
1259
+ return $mol_base64_url_encode(arr.subarray(0, 32))
1260
+ + $mol_base64_url_encode(arr.subarray(32, 64))
1261
+ + $mol_base64_url_encode(arr.subarray(64));
1262
+ }
1060
1263
  }
1061
- $.$mol_crypto_auditor_pair = $mol_crypto_auditor_pair;
1062
- class $mol_crypto_auditor_public extends Object {
1063
- native;
1264
+ __decorate([
1265
+ $mol_memo.method
1266
+ ], $mol_crypto_key.prototype, "toString", null);
1267
+ $.$mol_crypto_key = $mol_crypto_key;
1268
+ class $mol_crypto_key_public extends $mol_crypto_key {
1064
1269
  static size_str = 86;
1065
1270
  static size_bin = 64;
1066
- constructor(native) {
1067
- super();
1068
- this.native = native;
1069
- }
1070
- static async from(serial) {
1071
- if (typeof serial !== 'string') {
1072
- serial = $mol_base64_url_encode(serial.subarray(0, 32))
1073
- + $mol_base64_url_encode(serial.subarray(32, 64));
1074
- }
1075
- return new this(await $mol_crypto_native.subtle.importKey('jwk', {
1271
+ async native() {
1272
+ const str = this.toString();
1273
+ return $mol_crypto_native.subtle.importKey('jwk', {
1076
1274
  crv: "P-256",
1077
1275
  ext: true,
1078
1276
  key_ops: ['verify'],
1079
1277
  kty: "EC",
1080
- x: serial.slice(0, 43),
1081
- y: serial.slice(43, 86),
1082
- }, algorithm, true, ['verify']));
1083
- }
1084
- async serial() {
1085
- const { x, y } = await $mol_crypto_native.subtle.exportKey('jwk', this.native);
1086
- return x + y;
1087
- }
1088
- async toArray() {
1089
- const { x, y, d } = await $mol_crypto_native.subtle.exportKey('jwk', this.native);
1090
- return new Uint8Array([
1091
- ...$mol_base64_url_decode(x),
1092
- ...$mol_base64_url_decode(y),
1093
- ]);
1278
+ x: str.slice(0, 43),
1279
+ y: str.slice(43, 86),
1280
+ }, algorithm, true, ['verify']);
1094
1281
  }
1095
1282
  async verify(data, sign) {
1096
- return await $mol_crypto_native.subtle.verify(algorithm, this.native, sign, data);
1283
+ return await $mol_crypto_native.subtle.verify(algorithm, await this.native(), sign, data);
1097
1284
  }
1098
1285
  }
1099
- $.$mol_crypto_auditor_public = $mol_crypto_auditor_public;
1100
- class $mol_crypto_auditor_private extends Object {
1101
- native;
1286
+ __decorate([
1287
+ $mol_memo.method
1288
+ ], $mol_crypto_key_public.prototype, "native", null);
1289
+ $.$mol_crypto_key_public = $mol_crypto_key_public;
1290
+ class $mol_crypto_key_private extends $mol_crypto_key {
1102
1291
  static size_str = 129;
1103
1292
  static size_bin = 96;
1104
- constructor(native) {
1105
- super();
1106
- this.native = native;
1293
+ static size_sign = 64;
1294
+ static async generate() {
1295
+ const pair = await $mol_crypto_native.subtle.generateKey(algorithm, true, ['sign', 'verify']);
1296
+ const { x, y, d } = await $mol_crypto_native.subtle.exportKey('jwk', pair.privateKey);
1297
+ return this.from(x + y + d);
1107
1298
  }
1108
- static async from(serial) {
1109
- if (typeof serial !== 'string') {
1110
- serial = $mol_base64_url_encode(serial.subarray(0, 32))
1111
- + $mol_base64_url_encode(serial.subarray(32, 64))
1112
- + $mol_base64_url_encode(serial.subarray(64));
1113
- }
1114
- return new this(await $mol_crypto_native.subtle.importKey('jwk', {
1299
+ async native() {
1300
+ const str = this.toString();
1301
+ return await $mol_crypto_native.subtle.importKey('jwk', {
1115
1302
  crv: "P-256",
1116
1303
  ext: true,
1117
1304
  key_ops: ['sign'],
1118
1305
  kty: "EC",
1119
- x: serial.slice(0, 43),
1120
- y: serial.slice(43, 86),
1121
- d: serial.slice(86, 129),
1122
- }, algorithm, true, ['sign']));
1306
+ x: str.slice(0, 43),
1307
+ y: str.slice(43, 86),
1308
+ d: str.slice(86, 129),
1309
+ }, algorithm, true, ['sign']);
1123
1310
  }
1124
- async serial() {
1125
- const { x, y, d } = await $mol_crypto_native.subtle.exportKey('jwk', this.native);
1126
- return x + y + d;
1127
- }
1128
- async toArray() {
1129
- const { x, y, d } = await $mol_crypto_native.subtle.exportKey('jwk', this.native);
1130
- return new Uint8Array([
1131
- ...$mol_base64_url_decode(x),
1132
- ...$mol_base64_url_decode(y),
1133
- ...$mol_base64_url_decode(d),
1134
- ]);
1311
+ public() {
1312
+ return new $mol_crypto_key_public(this.buffer, this.byteOffset, this.byteOffset + 64);
1135
1313
  }
1136
1314
  async sign(data) {
1137
- return await $mol_crypto_native.subtle.sign(algorithm, this.native, data);
1138
- }
1139
- async public() {
1140
- return await $mol_crypto_auditor_public.from($mol_crypto_auditor_private_to_public(await this.serial()));
1315
+ return await $mol_crypto_native.subtle.sign(algorithm, await this.native(), data);
1141
1316
  }
1142
1317
  }
1143
- $.$mol_crypto_auditor_private = $mol_crypto_auditor_private;
1144
- $.$mol_crypto_auditor_sign_size = 64;
1145
- function $mol_crypto_auditor_private_to_public(serial) {
1146
- return serial.slice(0, 86);
1147
- }
1148
- $.$mol_crypto_auditor_private_to_public = $mol_crypto_auditor_private_to_public;
1318
+ __decorate([
1319
+ $mol_memo.method
1320
+ ], $mol_crypto_key_private.prototype, "native", null);
1321
+ __decorate([
1322
+ $mol_memo.method
1323
+ ], $mol_crypto_key_private.prototype, "public", null);
1324
+ $.$mol_crypto_key_private = $mol_crypto_key_private;
1149
1325
  })($ || ($ = {}));
1150
- //mol/crypto/auditor/auditor.ts
1326
+ //mol/crypto/key/key.ts
1151
1327
  ;
1152
1328
  "use strict";
1153
1329
  var $;