@substrate-system/keys 0.2.9 → 0.2.11
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/dist/_base.cjs +2 -1
- package/dist/_base.cjs.map +2 -2
- package/dist/_base.d.ts.map +1 -1
- package/dist/_base.js +1 -1
- package/dist/_base.js.map +2 -2
- package/dist/_base.min.cjs +1 -1
- package/dist/_base.min.cjs.map +3 -3
- package/dist/_base.min.js +2 -2
- package/dist/_base.min.js.map +4 -4
- package/dist/aes/index.cjs +3 -2
- package/dist/aes/index.cjs.map +2 -2
- package/dist/aes/index.js +1 -1
- package/dist/aes/index.js.map +2 -2
- package/dist/aes/index.min.cjs +1 -1
- package/dist/aes/index.min.cjs.map +3 -3
- package/dist/aes/index.min.js +2 -2
- package/dist/aes/index.min.js.map +4 -4
- package/dist/browser/index.js +5069 -0
- package/dist/browser/index.min.js +3 -0
- package/dist/crypto.cjs +196 -0
- package/dist/crypto.cjs.map +7 -0
- package/dist/crypto.d.ts +46 -0
- package/dist/crypto.d.ts.map +1 -0
- package/dist/crypto.js +181 -0
- package/dist/crypto.js.map +7 -0
- package/dist/crypto.min.cjs +4 -0
- package/dist/crypto.min.cjs.map +7 -0
- package/dist/crypto.min.js +4 -0
- package/dist/crypto.min.js.map +7 -0
- package/dist/ecc/index.min.js +2 -2
- package/dist/ecc/index.min.js.map +4 -4
- package/dist/index.cjs +0 -2
- package/dist/index.cjs.map +2 -2
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -5063
- package/dist/index.js.map +2 -2
- package/dist/index.min.cjs +1 -1
- package/dist/index.min.cjs.map +3 -3
- package/dist/index.min.js +3 -2
- package/dist/index.min.js.map +4 -4
- package/dist/meta.json +105 -35
- package/dist/rsa/index.cjs +8 -7
- package/dist/rsa/index.cjs.map +2 -2
- package/dist/rsa/index.d.ts.map +1 -1
- package/dist/rsa/index.js +5 -3
- package/dist/rsa/index.js.map +2 -2
- package/dist/rsa/index.min.cjs +1 -1
- package/dist/rsa/index.min.cjs.map +3 -3
- package/dist/rsa/index.min.js +2 -2
- package/dist/rsa/index.min.js.map +4 -4
- package/dist/util.cjs +8 -152
- package/dist/util.cjs.map +3 -3
- package/dist/util.d.ts +2 -43
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js +5 -153
- package/dist/util.js.map +3 -3
- package/dist/util.min.cjs +1 -3
- package/dist/util.min.cjs.map +3 -3
- package/dist/util.min.js +2 -2
- package/dist/util.min.js.map +4 -4
- package/package.json +9 -3
package/dist/util.cjs
CHANGED
|
@@ -29,12 +29,8 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
29
29
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
30
|
var util_exports = {};
|
|
31
31
|
__export(util_exports, {
|
|
32
|
-
InvalidKeyUse: () => InvalidKeyUse,
|
|
33
|
-
InvalidMaxValue: () => InvalidMaxValue,
|
|
34
32
|
base64ToArrBuf: () => base64ToArrBuf,
|
|
35
|
-
checkValidKeyUse: () => checkValidKeyUse,
|
|
36
33
|
createDeviceName: () => createDeviceName,
|
|
37
|
-
did: () => did,
|
|
38
34
|
didToPublicKey: () => didToPublicKey,
|
|
39
35
|
ed25519Verify: () => ed25519Verify,
|
|
40
36
|
fromBase64: () => fromBase64,
|
|
@@ -42,9 +38,6 @@ __export(util_exports, {
|
|
|
42
38
|
getPublicKeyAsArrayBuffer: () => getPublicKeyAsArrayBuffer,
|
|
43
39
|
getPublicKeyAsUint8Array: () => getPublicKeyAsUint8Array,
|
|
44
40
|
importKey: () => importKey,
|
|
45
|
-
importPublicKey: () => importPublicKey,
|
|
46
|
-
importRsaKey: () => importRsaKey,
|
|
47
|
-
isCryptoKey: () => isCryptoKey,
|
|
48
41
|
joinBufs: () => joinBufs,
|
|
49
42
|
makeEccKeypair: () => makeEccKeypair,
|
|
50
43
|
makeRSAKeypair: () => makeRSAKeypair,
|
|
@@ -55,10 +48,9 @@ __export(util_exports, {
|
|
|
55
48
|
normalizeUtf8ToBuf: () => normalizeUtf8ToBuf,
|
|
56
49
|
publicKeyToDid: () => publicKeyToDid,
|
|
57
50
|
randomBuf: () => randomBuf,
|
|
58
|
-
rsaOperations: () => rsaOperations,
|
|
59
|
-
rsaVerify: () => rsaVerify2,
|
|
60
51
|
sha256: () => sha256,
|
|
61
52
|
strToArrBuf: () => strToArrBuf,
|
|
53
|
+
toArrayBuffer: () => toArrayBuffer,
|
|
62
54
|
toBase64: () => toBase64
|
|
63
55
|
});
|
|
64
56
|
module.exports = __toCommonJS(util_exports);
|
|
@@ -66,29 +58,12 @@ var import_one_webcrypto = require("@substrate-system/one-webcrypto");
|
|
|
66
58
|
var import_uint8arrays = require("uint8arrays");
|
|
67
59
|
var import_tweetnacl = __toESM(require("tweetnacl"), 1);
|
|
68
60
|
var import_types = require("./types.js");
|
|
61
|
+
var import_crypto = require("./crypto.js");
|
|
69
62
|
var import_constants = require("./constants.js");
|
|
70
63
|
function toArrayBuffer(data) {
|
|
71
64
|
return new Uint8Array(data).buffer;
|
|
72
65
|
}
|
|
73
66
|
__name(toArrayBuffer, "toArrayBuffer");
|
|
74
|
-
const did = {
|
|
75
|
-
keyTypes: {
|
|
76
|
-
"bls12-381": {
|
|
77
|
-
magicBytes: new Uint8Array([234, 1]),
|
|
78
|
-
verify: /* @__PURE__ */ __name(() => {
|
|
79
|
-
throw new Error("Not implemented");
|
|
80
|
-
}, "verify")
|
|
81
|
-
},
|
|
82
|
-
rsa: {
|
|
83
|
-
magicBytes: new Uint8Array([0, 245, 2]),
|
|
84
|
-
verify: rsaVerify2
|
|
85
|
-
},
|
|
86
|
-
ed25519: {
|
|
87
|
-
magicBytes: new Uint8Array([237, 1]),
|
|
88
|
-
verify: ed25519Verify
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
67
|
async function ed25519Verify({
|
|
93
68
|
message,
|
|
94
69
|
publicKey,
|
|
@@ -113,116 +88,14 @@ async function sha256(bytes) {
|
|
|
113
88
|
__name(sha256, "sha256");
|
|
114
89
|
async function publicKeyToDid(_publicKey, keyType = "rsa") {
|
|
115
90
|
const publicKey = _publicKey instanceof CryptoKey ? new Uint8Array(await getPublicKeyAsArrayBuffer(_publicKey)) : _publicKey;
|
|
116
|
-
const prefix = did.keyTypes[keyType]?.magicBytes;
|
|
91
|
+
const prefix = import_crypto.did.keyTypes[keyType]?.magicBytes;
|
|
117
92
|
if (!prefix) {
|
|
118
|
-
throw new Error(`Key type '${keyType}' not supported, available types: ${Object.keys(did.keyTypes).join(", ")}`);
|
|
93
|
+
throw new Error(`Key type '${keyType}' not supported, available types: ${Object.keys(import_crypto.did.keyTypes).join(", ")}`);
|
|
119
94
|
}
|
|
120
95
|
const prefixedBuf = (0, import_uint8arrays.concat)([prefix, publicKey]);
|
|
121
96
|
return import_constants.BASE58_DID_PREFIX + (0, import_uint8arrays.toString)(prefixedBuf, "base58btc");
|
|
122
97
|
}
|
|
123
98
|
__name(publicKeyToDid, "publicKeyToDid");
|
|
124
|
-
const rsaOperations = {
|
|
125
|
-
verify: /* @__PURE__ */ __name(async function rsaVerify(msg, sig, publicKey, charSize = import_constants.DEFAULT_CHAR_SIZE, hashAlg = import_constants.DEFAULT_HASH_ALGORITHM) {
|
|
126
|
-
return import_one_webcrypto.webcrypto.subtle.verify(
|
|
127
|
-
{
|
|
128
|
-
name: import_constants.RSA_SIGN_ALGORITHM,
|
|
129
|
-
saltLength: import_constants.SALT_LENGTH
|
|
130
|
-
},
|
|
131
|
-
typeof publicKey === "string" ? await importPublicKey(publicKey, hashAlg, import_types.KeyUse.Sign) : publicKey,
|
|
132
|
-
normalizeBase64ToBuf(sig),
|
|
133
|
-
normalizeUnicodeToBuf(msg, charSize)
|
|
134
|
-
);
|
|
135
|
-
}, "rsaVerify"),
|
|
136
|
-
sign: /* @__PURE__ */ __name(async function sign(msg, privateKey, charSize = import_constants.DEFAULT_CHAR_SIZE) {
|
|
137
|
-
return import_one_webcrypto.webcrypto.subtle.sign(
|
|
138
|
-
{ name: import_constants.RSA_SIGN_ALGORITHM, saltLength: import_constants.SALT_LENGTH },
|
|
139
|
-
privateKey,
|
|
140
|
-
normalizeUnicodeToBuf(msg, charSize)
|
|
141
|
-
);
|
|
142
|
-
}, "sign"),
|
|
143
|
-
/**
|
|
144
|
-
* Encrypt the given AES key *to* a given public key.
|
|
145
|
-
*/
|
|
146
|
-
encrypt: /* @__PURE__ */ __name(async function rsaEncrypt(msg, publicKey, charSize = import_constants.DEFAULT_CHAR_SIZE, hashAlg = import_constants.DEFAULT_HASH_ALGORITHM) {
|
|
147
|
-
let pubKey;
|
|
148
|
-
if (typeof publicKey === "string") {
|
|
149
|
-
pubKey = await importPublicKey(publicKey, hashAlg, import_types.KeyUse.Exchange);
|
|
150
|
-
} else {
|
|
151
|
-
pubKey = publicKey instanceof Uint8Array ? await importPublicKey(toArrayBuffer(publicKey), hashAlg, import_types.KeyUse.Exchange) : publicKey;
|
|
152
|
-
}
|
|
153
|
-
return import_one_webcrypto.webcrypto.subtle.encrypt(
|
|
154
|
-
{ name: import_constants.RSA_ALGORITHM },
|
|
155
|
-
pubKey,
|
|
156
|
-
normalizeUnicodeToBuf(msg, charSize)
|
|
157
|
-
);
|
|
158
|
-
}, "rsaEncrypt"),
|
|
159
|
-
/**
|
|
160
|
-
* Use RSA to decrypt the given data.
|
|
161
|
-
*/
|
|
162
|
-
decrypt: /* @__PURE__ */ __name(async function rsaDecrypt(_data, privateKey) {
|
|
163
|
-
const key = isCryptoKey(privateKey) ? privateKey : await importRsaKey(privateKey, ["decrypt"]);
|
|
164
|
-
let data;
|
|
165
|
-
if (typeof _data === "string") {
|
|
166
|
-
data = (0, import_uint8arrays.fromString)(_data, "base64pad");
|
|
167
|
-
} else {
|
|
168
|
-
data = _data;
|
|
169
|
-
}
|
|
170
|
-
const arrayBuffer = await import_one_webcrypto.webcrypto.subtle.decrypt(
|
|
171
|
-
{ name: import_constants.RSA_ALGORITHM },
|
|
172
|
-
key,
|
|
173
|
-
data instanceof Uint8Array ? toArrayBuffer(data) : data
|
|
174
|
-
);
|
|
175
|
-
const arr = new Uint8Array(arrayBuffer);
|
|
176
|
-
return arr;
|
|
177
|
-
}, "rsaDecrypt")
|
|
178
|
-
};
|
|
179
|
-
async function rsaVerify2({
|
|
180
|
-
message,
|
|
181
|
-
publicKey,
|
|
182
|
-
signature
|
|
183
|
-
}) {
|
|
184
|
-
return rsaOperations.verify(
|
|
185
|
-
message,
|
|
186
|
-
signature,
|
|
187
|
-
await import_one_webcrypto.webcrypto.subtle.importKey(
|
|
188
|
-
"spki",
|
|
189
|
-
toArrayBuffer(publicKey),
|
|
190
|
-
{ name: import_constants.RSA_SIGN_ALGORITHM, hash: import_constants.RSA_HASHING_ALGORITHM },
|
|
191
|
-
false,
|
|
192
|
-
["verify"]
|
|
193
|
-
),
|
|
194
|
-
8
|
|
195
|
-
);
|
|
196
|
-
}
|
|
197
|
-
__name(rsaVerify2, "rsaVerify");
|
|
198
|
-
async function importPublicKey(base64Key, hashAlg, use) {
|
|
199
|
-
checkValidKeyUse(use);
|
|
200
|
-
const alg = use === import_types.KeyUse.Exchange ? import_constants.RSA_ALGORITHM : import_constants.RSA_SIGN_ALGORITHM;
|
|
201
|
-
const uses = use === import_types.KeyUse.Exchange ? ["encrypt"] : ["verify"];
|
|
202
|
-
const buf = typeof base64Key === "string" ? base64ToArrBuf(stripKeyHeader(base64Key)) : base64Key;
|
|
203
|
-
return import_one_webcrypto.webcrypto.subtle.importKey("spki", buf, {
|
|
204
|
-
name: alg,
|
|
205
|
-
hash: { name: hashAlg }
|
|
206
|
-
}, true, uses);
|
|
207
|
-
}
|
|
208
|
-
__name(importPublicKey, "importPublicKey");
|
|
209
|
-
const InvalidKeyUse = new Error("Invalid key use. Please use 'encryption' or 'signing");
|
|
210
|
-
const InvalidMaxValue = new Error("Max must be less than 256 and greater than 0");
|
|
211
|
-
function checkValidKeyUse(use) {
|
|
212
|
-
checkValid(use, [import_types.KeyUse.Sign, import_types.KeyUse.Exchange], InvalidKeyUse);
|
|
213
|
-
}
|
|
214
|
-
__name(checkValidKeyUse, "checkValidKeyUse");
|
|
215
|
-
function checkValid(toCheck, opts, error) {
|
|
216
|
-
const match = opts.some((opt) => opt === toCheck);
|
|
217
|
-
if (!match) {
|
|
218
|
-
throw error;
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
__name(checkValid, "checkValid");
|
|
222
|
-
function stripKeyHeader(base64Key) {
|
|
223
|
-
return base64Key.replace("-----BEGIN PUBLIC KEY-----\n", "").replace("\n-----END PUBLIC KEY-----", "");
|
|
224
|
-
}
|
|
225
|
-
__name(stripKeyHeader, "stripKeyHeader");
|
|
226
99
|
function base64ToArrBuf(string) {
|
|
227
100
|
const uint8 = (0, import_uint8arrays.fromString)(string, "base64pad");
|
|
228
101
|
return toArrayBuffer(uint8);
|
|
@@ -265,24 +138,6 @@ function normalizeUnicodeToBuf(msg, charSize) {
|
|
|
265
138
|
}
|
|
266
139
|
}
|
|
267
140
|
__name(normalizeUnicodeToBuf, "normalizeUnicodeToBuf");
|
|
268
|
-
function importRsaKey(key, keyUsages) {
|
|
269
|
-
return import_one_webcrypto.webcrypto.subtle.importKey(
|
|
270
|
-
"spki",
|
|
271
|
-
key instanceof Uint8Array ? toArrayBuffer(key) : key,
|
|
272
|
-
{ name: import_constants.RSA_ALGORITHM, hash: import_constants.RSA_HASHING_ALGORITHM },
|
|
273
|
-
false,
|
|
274
|
-
keyUsages
|
|
275
|
-
);
|
|
276
|
-
}
|
|
277
|
-
__name(importRsaKey, "importRsaKey");
|
|
278
|
-
function isCryptoKey(val) {
|
|
279
|
-
return hasProp(val, "algorithm") && hasProp(val, "extractable") && hasProp(val, "type");
|
|
280
|
-
}
|
|
281
|
-
__name(isCryptoKey, "isCryptoKey");
|
|
282
|
-
function hasProp(data, prop) {
|
|
283
|
-
return typeof data === "object" && data != null && prop in data;
|
|
284
|
-
}
|
|
285
|
-
__name(hasProp, "hasProp");
|
|
286
141
|
async function getPublicKeyAsArrayBuffer(keypair) {
|
|
287
142
|
const spki = keypair instanceof CryptoKey ? await import_one_webcrypto.webcrypto.subtle.exportKey(
|
|
288
143
|
"spki",
|
|
@@ -376,7 +231,7 @@ async function importKey(key, opts) {
|
|
|
376
231
|
__name(importKey, "importKey");
|
|
377
232
|
function randomBuf(length, { max } = { max: 255 }) {
|
|
378
233
|
if (max < 1 || max > 255) {
|
|
379
|
-
throw InvalidMaxValue;
|
|
234
|
+
throw import_crypto.InvalidMaxValue;
|
|
380
235
|
}
|
|
381
236
|
const arr = new Uint8Array(length);
|
|
382
237
|
if (max === 255) {
|
|
@@ -417,7 +272,8 @@ async function makeEccKeypair(curve, uses) {
|
|
|
417
272
|
return keys;
|
|
418
273
|
}
|
|
419
274
|
__name(makeEccKeypair, "makeEccKeypair");
|
|
420
|
-
async function makeRSAKeypair(size, hashAlg, use) {
|
|
275
|
+
async function makeRSAKeypair(size, hashAlg, use, extractable) {
|
|
276
|
+
const isExtractable = extractable || false;
|
|
421
277
|
if (!Object.values(import_types.KeyUse).includes(use)) {
|
|
422
278
|
throw new Error("invalid key use");
|
|
423
279
|
}
|
|
@@ -428,7 +284,7 @@ async function makeRSAKeypair(size, hashAlg, use) {
|
|
|
428
284
|
modulusLength: size,
|
|
429
285
|
publicExponent: publicExponent(),
|
|
430
286
|
hash: { name: hashAlg }
|
|
431
|
-
},
|
|
287
|
+
}, isExtractable, uses);
|
|
432
288
|
}
|
|
433
289
|
__name(makeRSAKeypair, "makeRSAKeypair");
|
|
434
290
|
function publicExponent() {
|
package/dist/util.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/util.ts"],
|
|
4
|
-
"sourcesContent": ["import { webcrypto } from '@substrate-system/one-webcrypto'\nimport { fromString, concat, toString as uToString } from 'uint8arrays'\nimport tweetnacl from 'tweetnacl'\nimport type {\n DID,\n Msg,\n HashAlg,\n SymmKeyOpts,\n SymmKey,\n RsaSize\n} from './types.js'\nimport {\n KeyUse,\n CharSize,\n} from './types.js'\nimport {\n BASE58_DID_PREFIX,\n KEY_USE,\n RSA_SIGN_ALGORITHM,\n RSA_ALGORITHM,\n DEFAULT_HASH_ALGORITHM,\n DEFAULT_CHAR_SIZE,\n SALT_LENGTH,\n RSA_HASHING_ALGORITHM,\n RSA_DID_PREFIX,\n KEY_TYPE,\n EDWARDS_DID_PREFIX,\n BLS_DID_PREFIX,\n DEFAULT_SYMM_ALGORITHM,\n DEFAULT_SYMM_LENGTH,\n} from './constants.js'\n\n// Helper function to ensure proper ArrayBuffer type\nfunction toArrayBuffer (data: Uint8Array): ArrayBuffer {\n return new Uint8Array(data).buffer\n}\n\nexport type VerifyArgs = {\n message:Uint8Array\n publicKey:Uint8Array\n signature:Uint8Array\n}\n\n/**\n * Using the key type as the record property name (ie. string = key type)\n *\n * The magic bytes are the `code` found in {@link https://github.com/multiformats/multicodec/blob/master/table.csv}\n * encoded as a variable integer (more info about that at {@link https://github.com/multiformats/unsigned-varint)}.\n *\n * The key type is also found in that table.\n * It's the name of the codec minus the `-pub` suffix.\n *\n * Example\n * -------\n * Ed25519 public key\n * Key type: \"ed25519\"\n * Magic bytes: [ 0xed, 0x01 ]\n */\ntype KeyTypes = Record<string, {\n magicBytes:Uint8Array\n verify:(args:VerifyArgs)=>Promise<boolean>\n}>\n\nexport const did:{ keyTypes:KeyTypes } = {\n keyTypes: {\n 'bls12-381': {\n magicBytes: new Uint8Array([0xea, 0x01]),\n verify: () => { throw new Error('Not implemented') },\n },\n rsa: {\n magicBytes: new Uint8Array([0x00, 0xf5, 0x02]),\n verify: rsaVerify,\n },\n ed25519: {\n magicBytes: new Uint8Array([0xed, 0x01]),\n verify: ed25519Verify\n },\n }\n}\n\nexport async function ed25519Verify ({\n message,\n publicKey,\n signature\n}:VerifyArgs):Promise<boolean> {\n return tweetnacl.sign.detached.verify(message, signature, publicKey)\n}\n\n/**\n * Create a 32 character, DNS-friendly hash of the given DID.\n *\n * @param {DID} did String representation of the DID for the device\n * @returns {string} The 32 character, DNS friendly hash\n */\nexport async function createDeviceName (did:DID):Promise<string> {\n const normalizedDid = did.normalize('NFD')\n const hashedUsername = await sha256(\n new TextEncoder().encode(normalizedDid)\n )\n return uToString(hashedUsername, 'base32').slice(0, 32)\n}\n\nexport async function sha256 (bytes:Uint8Array):Promise<Uint8Array> {\n return new Uint8Array(\n await webcrypto.subtle.digest('sha-256', bytes.buffer as ArrayBuffer)\n )\n}\n\n/**\n * Convert a public key to a DID format string.\n *\n * @param {Uint8Array|CryptoKey|CryptoKeyPair} publicKey Public key as Uint8Array\n * @param {'rsa'} [keyType] 'rsa' or 'ecc'\n * @returns {DID} A DID format string\n */\nexport async function publicKeyToDid (\n _publicKey:Uint8Array|CryptoKey,\n keyType:'rsa'|'ed25519' = 'rsa'\n):Promise<DID> {\n const publicKey = ((_publicKey instanceof CryptoKey) ?\n new Uint8Array(await getPublicKeyAsArrayBuffer(_publicKey)) :\n _publicKey\n )\n\n // Prefix public-write key\n const prefix = did.keyTypes[keyType]?.magicBytes\n if (!prefix) {\n throw new Error(`Key type '${keyType}' not supported, ` +\n `available types: ${Object.keys(did.keyTypes).join(', ')}`)\n }\n\n const prefixedBuf = concat([prefix, publicKey])\n\n return (BASE58_DID_PREFIX + uToString(prefixedBuf, 'base58btc')) as DID\n}\n\nexport const rsaOperations = {\n verify: async function rsaVerify (\n msg:Msg,\n sig:Msg,\n publicKey:string|CryptoKey,\n charSize:CharSize = DEFAULT_CHAR_SIZE,\n hashAlg:HashAlg = DEFAULT_HASH_ALGORITHM\n ):Promise<boolean> {\n return webcrypto.subtle.verify(\n {\n name: RSA_SIGN_ALGORITHM,\n saltLength: SALT_LENGTH\n },\n (typeof publicKey === 'string' ?\n await importPublicKey(publicKey, hashAlg, KeyUse.Sign) :\n publicKey),\n normalizeBase64ToBuf(sig),\n normalizeUnicodeToBuf(msg, charSize)\n )\n },\n\n sign: async function sign (\n msg:Msg,\n privateKey:CryptoKey,\n charSize:CharSize = DEFAULT_CHAR_SIZE\n ):Promise<ArrayBuffer> {\n return webcrypto.subtle.sign(\n { name: RSA_SIGN_ALGORITHM, saltLength: SALT_LENGTH },\n privateKey,\n normalizeUnicodeToBuf(msg, charSize)\n )\n },\n\n /**\n * Encrypt the given AES key *to* a given public key.\n */\n encrypt: async function rsaEncrypt (\n msg:Msg,\n publicKey:string|CryptoKey|Uint8Array,\n charSize:CharSize = DEFAULT_CHAR_SIZE,\n hashAlg:HashAlg = DEFAULT_HASH_ALGORITHM\n ):Promise<ArrayBuffer> {\n let pubKey:CryptoKey\n if (typeof publicKey === 'string') {\n pubKey = await importPublicKey(publicKey, hashAlg, KeyUse.Exchange)\n } else {\n pubKey = publicKey instanceof Uint8Array ?\n await importPublicKey(toArrayBuffer(publicKey), hashAlg, KeyUse.Exchange) :\n publicKey\n }\n\n return webcrypto.subtle.encrypt(\n { name: RSA_ALGORITHM },\n pubKey,\n normalizeUnicodeToBuf(msg, charSize)\n )\n },\n\n /**\n * Use RSA to decrypt the given data.\n */\n decrypt: async function rsaDecrypt (\n _data:Uint8Array|string|ArrayBuffer,\n privateKey:CryptoKey|Uint8Array|ArrayBuffer\n ):Promise<Uint8Array> {\n const key = isCryptoKey(privateKey) ?\n privateKey :\n await importRsaKey(privateKey, ['decrypt'])\n\n let data:Uint8Array|ArrayBuffer\n if (typeof _data === 'string') {\n data = fromString(_data, 'base64pad')\n } else {\n data = _data\n }\n\n const arrayBuffer = await webcrypto.subtle.decrypt(\n { name: RSA_ALGORITHM },\n key,\n data instanceof Uint8Array ? toArrayBuffer(data) : data\n )\n\n const arr = new Uint8Array(arrayBuffer)\n\n return arr\n }\n}\n\nexport async function rsaVerify ({\n message,\n publicKey,\n signature\n}:VerifyArgs):Promise<boolean> {\n return rsaOperations.verify(\n message,\n signature,\n await webcrypto.subtle.importKey(\n 'spki',\n toArrayBuffer(publicKey),\n { name: RSA_SIGN_ALGORITHM, hash: RSA_HASHING_ALGORITHM },\n false,\n ['verify']\n ),\n 8\n )\n}\n\nexport async function importPublicKey (\n base64Key:string|ArrayBuffer,\n hashAlg:HashAlg,\n use:KeyUse\n):Promise<CryptoKey> {\n checkValidKeyUse(use)\n const alg = (use === KeyUse.Exchange ? RSA_ALGORITHM : RSA_SIGN_ALGORITHM)\n const uses:KeyUsage[] = use === KeyUse.Exchange ?\n ['encrypt'] :\n ['verify']\n const buf = typeof base64Key === 'string' ?\n base64ToArrBuf(stripKeyHeader(base64Key)) :\n base64Key\n\n return webcrypto.subtle.importKey('spki', buf, {\n name: alg,\n hash: { name: hashAlg }\n }, true, uses)\n}\n\nexport const InvalidKeyUse = new Error(\"Invalid key use. Please use 'encryption' or 'signing\")\nexport const InvalidMaxValue = new Error('Max must be less than 256 and greater than 0')\n\nexport function checkValidKeyUse (use:KeyUse):void {\n checkValid(use, [KeyUse.Sign, KeyUse.Exchange], InvalidKeyUse)\n}\n\nfunction checkValid<T> (toCheck: T, opts: T[], error: Error): void {\n const match = opts.some(opt => opt === toCheck)\n if (!match) {\n throw error\n }\n}\n\nfunction stripKeyHeader (base64Key:string):string {\n return base64Key\n .replace('-----BEGIN PUBLIC KEY-----\\n', '')\n .replace('\\n-----END PUBLIC KEY-----', '')\n}\n\nexport function base64ToArrBuf (string:string):ArrayBuffer {\n const uint8 = fromString(string, 'base64pad')\n return toArrayBuffer(uint8)\n}\n\nexport const normalizeToBuf = (\n msg:Msg,\n strConv:(str:string)=>ArrayBuffer\n):ArrayBuffer => {\n if (typeof msg === 'string') {\n return strConv(msg)\n } else if (typeof msg === 'object' && msg.byteLength !== undefined) {\n // this is the best runtime check I could find for ArrayBuffer/Uint8Array\n const temp = new Uint8Array(msg)\n return temp.buffer\n } else {\n throw new Error('Improper value. Must be a string, ArrayBuffer, Uint8Array')\n }\n}\n\nexport function normalizeBase64ToBuf (msg:Msg):ArrayBuffer {\n return normalizeToBuf(msg, base64ToArrBuf)\n}\n\nexport const normalizeUtf8ToBuf = (msg:Msg): ArrayBuffer => {\n return normalizeToBuf(msg, (str) => strToArrBuf(str, CharSize.B8))\n}\n\nexport function strToArrBuf (str:string, charSize:CharSize):ArrayBuffer {\n const view = charSize === 8 ?\n new Uint8Array(str.length) :\n new Uint16Array(str.length)\n\n for (let i = 0, strLen = str.length; i < strLen; i++) {\n view[i] = str.charCodeAt(i)\n }\n\n return view.buffer\n}\n\nexport const normalizeUtf16ToBuf = (msg:Msg): ArrayBuffer => {\n return normalizeToBuf(msg, (str) => strToArrBuf(str, CharSize.B16))\n}\n\nexport function normalizeUnicodeToBuf (msg:Msg, charSize:CharSize) {\n switch (charSize) {\n case 8: return normalizeUtf8ToBuf(msg)\n default: return normalizeUtf16ToBuf(msg)\n }\n}\n\nexport function importRsaKey (\n key:Uint8Array|ArrayBuffer,\n keyUsages:KeyUsage[]\n):Promise<CryptoKey> {\n return webcrypto.subtle.importKey(\n 'spki',\n key instanceof Uint8Array ? toArrayBuffer(key) : key,\n { name: RSA_ALGORITHM, hash: RSA_HASHING_ALGORITHM },\n false,\n keyUsages\n )\n}\n\nexport function isCryptoKey (val:unknown):val is CryptoKey {\n return (\n hasProp(val, 'algorithm') &&\n hasProp(val, 'extractable') &&\n hasProp(val, 'type')\n )\n}\n\nfunction hasProp<K extends PropertyKey> (\n data:unknown,\n prop:K\n): data is Record<K, unknown> {\n return (typeof data === 'object' && data != null && prop in data)\n}\n\nexport async function getPublicKeyAsArrayBuffer (\n keypair:CryptoKeyPair|CryptoKey\n):Promise<ArrayBuffer> {\n const spki = (keypair instanceof CryptoKey ?\n await webcrypto.subtle.exportKey(\n 'spki',\n keypair\n ) :\n await webcrypto.subtle.exportKey(\n 'spki',\n keypair.publicKey\n )\n )\n\n return spki\n}\n\nexport async function getPublicKeyAsUint8Array (\n keypair:CryptoKeyPair|CryptoKey\n):Promise<Uint8Array> {\n const arr = await getPublicKeyAsArrayBuffer(keypair)\n return new Uint8Array(arr)\n}\n\nexport function didToPublicKey (did:string):({\n publicKey:Uint8Array,\n type:'rsa' | 'ed25519' | 'bls12-381'\n}) {\n if (!did.startsWith(BASE58_DID_PREFIX)) {\n throw new Error(\n 'Please use a base58-encoded DID formatted `did:key:z...`')\n }\n\n const didWithoutPrefix = ('' + did.substring(BASE58_DID_PREFIX.length))\n const magicalBuf = fromString(didWithoutPrefix, 'base58btc')\n const { keyBuffer, type } = parseMagicBytes(toArrayBuffer(magicalBuf))\n\n return {\n publicKey: new Uint8Array(keyBuffer),\n type\n }\n}\n\n/**\n * Parse magic bytes on prefixed key-buffer\n * to determine cryptosystem & the unprefixed key-buffer.\n */\nfunction parseMagicBytes (prefixedKey:ArrayBuffer) {\n // RSA\n if (hasPrefix(prefixedKey, toArrayBuffer(RSA_DID_PREFIX))) {\n return {\n keyBuffer: prefixedKey.slice(RSA_DID_PREFIX.byteLength),\n type: KEY_TYPE.RSA\n }\n // EDWARDS\n } else if (hasPrefix(prefixedKey, toArrayBuffer(EDWARDS_DID_PREFIX))) {\n return {\n keyBuffer: prefixedKey.slice(EDWARDS_DID_PREFIX.byteLength),\n type: KEY_TYPE.Edwards\n }\n // BLS\n } else if (hasPrefix(prefixedKey, toArrayBuffer(BLS_DID_PREFIX))) {\n return {\n keyBuffer: prefixedKey.slice(BLS_DID_PREFIX.byteLength),\n type: KEY_TYPE.BLS\n }\n }\n\n throw new Error('Unsupported key algorithm. Try using RSA.')\n}\n\nconst arrBufs = {\n equal: (aBuf:ArrayBuffer, bBuf:ArrayBuffer) => {\n const a = new Uint8Array(aBuf)\n const b = new Uint8Array(bBuf)\n if (a.length !== b.length) return false\n for (let i = 0; i < a.length; i++) {\n if (a[i] !== b[i]) return false\n }\n return true\n }\n}\n\nfunction hasPrefix (prefixedKey:ArrayBuffer, prefix:ArrayBuffer) {\n return arrBufs.equal(prefix, prefixedKey.slice(0, prefix.byteLength))\n}\n\nexport function toBase64 (arr:Uint8Array|ArrayBuffer) {\n return uToString(\n arr instanceof ArrayBuffer ?\n new Uint8Array(arr) :\n arr,\n 'base64pad'\n )\n}\n\nexport function fromBase64 (str:string) {\n return fromString(str, 'base64pad')\n}\n\nexport async function importKey (\n key:string|Uint8Array,\n opts?:Partial<SymmKeyOpts>\n):Promise<SymmKey> {\n const buf = typeof key === 'string' ? base64ToArrBuf(key) : toArrayBuffer(key)\n\n return webcrypto.subtle.importKey(\n 'raw',\n buf,\n {\n name: opts?.alg || DEFAULT_SYMM_ALGORITHM,\n length: opts?.length || DEFAULT_SYMM_LENGTH,\n },\n true,\n ['encrypt', 'decrypt']\n )\n}\n\nexport function randomBuf (\n length:number,\n { max }:{ max:number } = { max: 255 }\n):ArrayBuffer {\n if (max < 1 || max > 255) {\n throw InvalidMaxValue\n }\n\n const arr = new Uint8Array(length)\n\n if (max === 255) {\n webcrypto.getRandomValues(arr)\n return arr.buffer\n }\n\n let index = 0\n const interval = max + 1\n const divisibleMax = Math.floor(256 / interval) * interval\n const tmp = new Uint8Array(1)\n\n while (index < arr.length) {\n webcrypto.getRandomValues(tmp)\n if (tmp[0] < divisibleMax) {\n arr[index] = tmp[0] % interval\n index++\n }\n }\n\n return arr.buffer\n}\n\nexport function joinBufs (fst:ArrayBuffer|Uint8Array, snd:ArrayBuffer|Uint8Array):ArrayBuffer {\n const view1 = fst instanceof ArrayBuffer ? new Uint8Array(fst) : new Uint8Array(toArrayBuffer(fst))\n const view2 = snd instanceof ArrayBuffer ? new Uint8Array(snd) : new Uint8Array(toArrayBuffer(snd))\n const joined = new Uint8Array(view1.length + view2.length)\n joined.set(view1)\n joined.set(view2, view1.length)\n return joined.buffer\n}\n\nexport async function makeEccKeypair (\n curve:'X25519'|'ECDSA',\n uses:'encyrpt'|'sign'\n):Promise<CryptoKeyPair> {\n const keys = await webcrypto.subtle.generateKey(\n { name: curve }, // X25519 or ECDSA\n false, // extractable\n KEY_USE[uses]\n ) as CryptoKeyPair\n\n return keys\n}\n\nexport async function makeRSAKeypair (\n size:RsaSize,\n hashAlg:HashAlg,\n use:KeyUse\n):Promise<CryptoKeyPair> {\n if (!(Object.values(KeyUse).includes(use))) {\n throw new Error('invalid key use')\n }\n const alg = use === KeyUse.Exchange ? RSA_ALGORITHM : RSA_SIGN_ALGORITHM\n const uses:KeyUsage[] = (use === KeyUse.Exchange ?\n ['encrypt', 'decrypt'] :\n ['sign', 'verify'])\n\n return webcrypto.subtle.generateKey({\n name: alg,\n modulusLength: size,\n publicExponent: publicExponent(),\n hash: { name: hashAlg }\n } as RsaHashedKeyGenParams, false, uses)\n}\n\nfunction publicExponent ():Uint8Array {\n return new Uint8Array([0x01, 0x00, 0x01])\n}\n\nexport async function getDeviceName (did:DID|string) {\n const hashedUsername = await sha256(\n new TextEncoder().encode(did.normalize('NFD'))\n )\n\n return uToString(hashedUsername, 'base32').slice(0, 32)\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA
|
|
6
|
-
"names": ["
|
|
4
|
+
"sourcesContent": ["import { webcrypto } from '@substrate-system/one-webcrypto'\nimport { fromString, concat, toString as uToString } from 'uint8arrays'\nimport tweetnacl from 'tweetnacl'\nimport type {\n DID,\n Msg,\n HashAlg,\n SymmKeyOpts,\n SymmKey,\n RsaSize\n} from './types.js'\nimport {\n KeyUse,\n CharSize,\n} from './types.js'\nimport { did, InvalidMaxValue } from './crypto.js'\nimport {\n BASE58_DID_PREFIX,\n KEY_USE,\n RSA_SIGN_ALGORITHM,\n RSA_ALGORITHM,\n RSA_DID_PREFIX,\n KEY_TYPE,\n EDWARDS_DID_PREFIX,\n BLS_DID_PREFIX,\n DEFAULT_SYMM_ALGORITHM,\n DEFAULT_SYMM_LENGTH,\n} from './constants.js'\n\n// Helper function to ensure proper ArrayBuffer type\nexport function toArrayBuffer (data: Uint8Array): ArrayBuffer {\n return new Uint8Array(data).buffer\n}\n\nexport type VerifyArgs = {\n message:Uint8Array\n publicKey:Uint8Array\n signature:Uint8Array\n}\n\nexport async function ed25519Verify ({\n message,\n publicKey,\n signature\n}:VerifyArgs):Promise<boolean> {\n return tweetnacl.sign.detached.verify(message, signature, publicKey)\n}\n\n/**\n * Create a 32 character, DNS-friendly hash of the given DID.\n *\n * @param {DID} did String representation of the DID for the device\n * @returns {string} The 32 character, DNS friendly hash\n */\nexport async function createDeviceName (did:DID):Promise<string> {\n const normalizedDid = did.normalize('NFD')\n const hashedUsername = await sha256(\n new TextEncoder().encode(normalizedDid)\n )\n return uToString(hashedUsername, 'base32').slice(0, 32)\n}\n\nexport async function sha256 (bytes:Uint8Array):Promise<Uint8Array> {\n return new Uint8Array(\n await webcrypto.subtle.digest('sha-256', bytes.buffer as ArrayBuffer)\n )\n}\n\n/**\n * Convert a public key to a DID format string.\n *\n * @param {Uint8Array|CryptoKey|CryptoKeyPair} publicKey Public key as Uint8Array\n * @param {'rsa'} [keyType] 'rsa' or 'ecc'\n * @returns {DID} A DID format string\n */\nexport async function publicKeyToDid (\n _publicKey:Uint8Array|CryptoKey,\n keyType:'rsa'|'ed25519' = 'rsa'\n):Promise<DID> {\n const publicKey = ((_publicKey instanceof CryptoKey) ?\n new Uint8Array(await getPublicKeyAsArrayBuffer(_publicKey)) :\n _publicKey\n )\n\n // Prefix public-write key\n const prefix = did.keyTypes[keyType]?.magicBytes\n if (!prefix) {\n throw new Error(`Key type '${keyType}' not supported, ` +\n `available types: ${Object.keys(did.keyTypes).join(', ')}`)\n }\n\n const prefixedBuf = concat([prefix, publicKey])\n\n return (BASE58_DID_PREFIX + uToString(prefixedBuf, 'base58btc')) as DID\n}\n\nexport function base64ToArrBuf (string:string):ArrayBuffer {\n const uint8 = fromString(string, 'base64pad')\n return toArrayBuffer(uint8)\n}\n\nexport const normalizeToBuf = (\n msg:Msg,\n strConv:(str:string)=>ArrayBuffer\n):ArrayBuffer => {\n if (typeof msg === 'string') {\n return strConv(msg)\n } else if (typeof msg === 'object' && msg.byteLength !== undefined) {\n // this is the best runtime check I could find for ArrayBuffer/Uint8Array\n const temp = new Uint8Array(msg)\n return temp.buffer\n } else {\n throw new Error('Improper value. Must be a string, ArrayBuffer, Uint8Array')\n }\n}\n\nexport function normalizeBase64ToBuf (msg:Msg):ArrayBuffer {\n return normalizeToBuf(msg, base64ToArrBuf)\n}\n\nexport const normalizeUtf8ToBuf = (msg:Msg): ArrayBuffer => {\n return normalizeToBuf(msg, (str) => strToArrBuf(str, CharSize.B8))\n}\n\nexport function strToArrBuf (str:string, charSize:CharSize):ArrayBuffer {\n const view = charSize === 8 ?\n new Uint8Array(str.length) :\n new Uint16Array(str.length)\n\n for (let i = 0, strLen = str.length; i < strLen; i++) {\n view[i] = str.charCodeAt(i)\n }\n\n return view.buffer\n}\n\nexport const normalizeUtf16ToBuf = (msg:Msg): ArrayBuffer => {\n return normalizeToBuf(msg, (str) => strToArrBuf(str, CharSize.B16))\n}\n\nexport function normalizeUnicodeToBuf (msg:Msg, charSize:CharSize) {\n switch (charSize) {\n case 8: return normalizeUtf8ToBuf(msg)\n default: return normalizeUtf16ToBuf(msg)\n }\n}\n\nexport async function getPublicKeyAsArrayBuffer (\n keypair:CryptoKeyPair|CryptoKey\n):Promise<ArrayBuffer> {\n const spki = (keypair instanceof CryptoKey ?\n await webcrypto.subtle.exportKey(\n 'spki',\n keypair\n ) :\n await webcrypto.subtle.exportKey(\n 'spki',\n keypair.publicKey\n )\n )\n\n return spki\n}\n\nexport async function getPublicKeyAsUint8Array (\n keypair:CryptoKeyPair|CryptoKey\n):Promise<Uint8Array> {\n const arr = await getPublicKeyAsArrayBuffer(keypair)\n return new Uint8Array(arr)\n}\n\nexport function didToPublicKey (did:string):({\n publicKey:Uint8Array,\n type:'rsa' | 'ed25519' | 'bls12-381'\n}) {\n if (!did.startsWith(BASE58_DID_PREFIX)) {\n throw new Error(\n 'Please use a base58-encoded DID formatted `did:key:z...`')\n }\n\n const didWithoutPrefix = ('' + did.substring(BASE58_DID_PREFIX.length))\n const magicalBuf = fromString(didWithoutPrefix, 'base58btc')\n const { keyBuffer, type } = parseMagicBytes(toArrayBuffer(magicalBuf))\n\n return {\n publicKey: new Uint8Array(keyBuffer),\n type\n }\n}\n\n/**\n * Parse magic bytes on prefixed key-buffer\n * to determine cryptosystem & the unprefixed key-buffer.\n */\nfunction parseMagicBytes (prefixedKey:ArrayBuffer) {\n // RSA\n if (hasPrefix(prefixedKey, toArrayBuffer(RSA_DID_PREFIX))) {\n return {\n keyBuffer: prefixedKey.slice(RSA_DID_PREFIX.byteLength),\n type: KEY_TYPE.RSA\n }\n // EDWARDS\n } else if (hasPrefix(prefixedKey, toArrayBuffer(EDWARDS_DID_PREFIX))) {\n return {\n keyBuffer: prefixedKey.slice(EDWARDS_DID_PREFIX.byteLength),\n type: KEY_TYPE.Edwards\n }\n // BLS\n } else if (hasPrefix(prefixedKey, toArrayBuffer(BLS_DID_PREFIX))) {\n return {\n keyBuffer: prefixedKey.slice(BLS_DID_PREFIX.byteLength),\n type: KEY_TYPE.BLS\n }\n }\n\n throw new Error('Unsupported key algorithm. Try using RSA.')\n}\n\nconst arrBufs = {\n equal: (aBuf:ArrayBuffer, bBuf:ArrayBuffer) => {\n const a = new Uint8Array(aBuf)\n const b = new Uint8Array(bBuf)\n if (a.length !== b.length) return false\n for (let i = 0; i < a.length; i++) {\n if (a[i] !== b[i]) return false\n }\n return true\n }\n}\n\nfunction hasPrefix (prefixedKey:ArrayBuffer, prefix:ArrayBuffer) {\n return arrBufs.equal(prefix, prefixedKey.slice(0, prefix.byteLength))\n}\n\nexport function toBase64 (arr:Uint8Array|ArrayBuffer) {\n return uToString(\n arr instanceof ArrayBuffer ?\n new Uint8Array(arr) :\n arr,\n 'base64pad'\n )\n}\n\nexport function fromBase64 (str:string) {\n return fromString(str, 'base64pad')\n}\n\nexport async function importKey (\n key:string|Uint8Array,\n opts?:Partial<SymmKeyOpts>\n):Promise<SymmKey> {\n const buf = typeof key === 'string' ? base64ToArrBuf(key) : toArrayBuffer(key)\n\n return webcrypto.subtle.importKey(\n 'raw',\n buf,\n {\n name: opts?.alg || DEFAULT_SYMM_ALGORITHM,\n length: opts?.length || DEFAULT_SYMM_LENGTH,\n },\n true,\n ['encrypt', 'decrypt']\n )\n}\n\nexport function randomBuf (\n length:number,\n { max }:{ max:number } = { max: 255 }\n):ArrayBuffer {\n if (max < 1 || max > 255) {\n throw InvalidMaxValue\n }\n\n const arr = new Uint8Array(length)\n\n if (max === 255) {\n webcrypto.getRandomValues(arr)\n return arr.buffer\n }\n\n let index = 0\n const interval = max + 1\n const divisibleMax = Math.floor(256 / interval) * interval\n const tmp = new Uint8Array(1)\n\n while (index < arr.length) {\n webcrypto.getRandomValues(tmp)\n if (tmp[0] < divisibleMax) {\n arr[index] = tmp[0] % interval\n index++\n }\n }\n\n return arr.buffer\n}\n\nexport function joinBufs (fst:ArrayBuffer|Uint8Array, snd:ArrayBuffer|Uint8Array):ArrayBuffer {\n const view1 = fst instanceof ArrayBuffer ? new Uint8Array(fst) : new Uint8Array(toArrayBuffer(fst))\n const view2 = snd instanceof ArrayBuffer ? new Uint8Array(snd) : new Uint8Array(toArrayBuffer(snd))\n const joined = new Uint8Array(view1.length + view2.length)\n joined.set(view1)\n joined.set(view2, view1.length)\n return joined.buffer\n}\n\nexport async function makeEccKeypair (\n curve:'X25519'|'ECDSA',\n uses:'encyrpt'|'sign'\n):Promise<CryptoKeyPair> {\n const keys = await webcrypto.subtle.generateKey(\n { name: curve }, // X25519 or ECDSA\n false, // extractable\n KEY_USE[uses]\n ) as CryptoKeyPair\n\n return keys\n}\n\nexport async function makeRSAKeypair (\n size:RsaSize,\n hashAlg:HashAlg,\n use:KeyUse,\n extractable?:boolean\n):Promise<CryptoKeyPair> {\n const isExtractable = extractable || false\n if (!(Object.values(KeyUse).includes(use))) {\n throw new Error('invalid key use')\n }\n const alg = use === KeyUse.Exchange ? RSA_ALGORITHM : RSA_SIGN_ALGORITHM\n const uses:KeyUsage[] = (use === KeyUse.Exchange ?\n ['encrypt', 'decrypt'] :\n ['sign', 'verify'])\n\n return webcrypto.subtle.generateKey({\n name: alg,\n modulusLength: size,\n publicExponent: publicExponent(),\n hash: { name: hashAlg }\n } as RsaHashedKeyGenParams, isExtractable, uses)\n}\n\nfunction publicExponent ():Uint8Array {\n return new Uint8Array([0x01, 0x00, 0x01])\n}\n\nexport async function getDeviceName (did:DID|string) {\n const hashedUsername = await sha256(\n new TextEncoder().encode(did.normalize('NFD'))\n )\n\n return uToString(hashedUsername, 'base32').slice(0, 32)\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,2BAA0B;AAC1B,yBAA0D;AAC1D,uBAAsB;AAStB,mBAGO;AACP,oBAAqC;AACrC,uBAWO;AAGA,SAAS,cAAe,MAA+B;AAC1D,SAAO,IAAI,WAAW,IAAI,EAAE;AAChC;AAFgB;AAUhB,eAAsB,cAAe;AAAA,EACjC;AAAA,EACA;AAAA,EACA;AACJ,GAA+B;AAC3B,SAAO,iBAAAA,QAAU,KAAK,SAAS,OAAO,SAAS,WAAW,SAAS;AACvE;AANsB;AActB,eAAsB,iBAAkBC,MAAyB;AAC7D,QAAM,gBAAgBA,KAAI,UAAU,KAAK;AACzC,QAAM,iBAAiB,MAAM;AAAA,IACzB,IAAI,YAAY,EAAE,OAAO,aAAa;AAAA,EAC1C;AACA,aAAO,mBAAAC,UAAU,gBAAgB,QAAQ,EAAE,MAAM,GAAG,EAAE;AAC1D;AANsB;AAQtB,eAAsB,OAAQ,OAAsC;AAChE,SAAO,IAAI;AA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BAAgB,eAAe;AACrC,MAAI,CAAE,OAAO,OAAO,mBAAM,EAAE,SAAS,GAAG,GAAI;AACxC,UAAM,IAAI,MAAM,iBAAiB;AAAA,EACrC;AACA,QAAM,MAAM,QAAQ,oBAAO,WAAW,iCAAgB;AACtD,QAAM,OAAmB,QAAQ,oBAAO,WACpC,CAAC,WAAW,SAAS,IACrB,CAAC,QAAQ,QAAQ;AAErB,SAAO,+BAAU,OAAO,YAAY;AAAA,IAChC,MAAM;AAAA,IACN,eAAe;AAAA,IACf,gBAAgB,eAAe;AAAA,IAC/B,MAAM,EAAE,MAAM,QAAQ;AAAA,EAC1B,GAA4B,eAAe,IAAI;AACnD;AArBsB;AAuBtB,SAAS,iBAA6B;AAClC,SAAO,IAAI,WAAW,CAAC,GAAM,GAAM,CAAI,CAAC;AAC5C;AAFS;AAIT,eAAsB,cAAeD,MAAgB;AACjD,QAAM,iBAAiB,MAAM;AAAA,IACzB,IAAI,YAAY,EAAE,OAAOA,KAAI,UAAU,KAAK,CAAC;AAAA,EACjD;AAEA,aAAO,mBAAAC,UAAU,gBAAgB,QAAQ,EAAE,MAAM,GAAG,EAAE;AAC1D;AANsB;",
|
|
6
|
+
"names": ["tweetnacl", "did", "uToString"]
|
|
7
7
|
}
|
package/dist/util.d.ts
CHANGED
|
@@ -1,32 +1,11 @@
|
|
|
1
1
|
import type { DID, Msg, HashAlg, SymmKeyOpts, SymmKey, RsaSize } from './types.js';
|
|
2
2
|
import { KeyUse, CharSize } from './types.js';
|
|
3
|
+
export declare function toArrayBuffer(data: Uint8Array): ArrayBuffer;
|
|
3
4
|
export type VerifyArgs = {
|
|
4
5
|
message: Uint8Array;
|
|
5
6
|
publicKey: Uint8Array;
|
|
6
7
|
signature: Uint8Array;
|
|
7
8
|
};
|
|
8
|
-
/**
|
|
9
|
-
* Using the key type as the record property name (ie. string = key type)
|
|
10
|
-
*
|
|
11
|
-
* The magic bytes are the `code` found in {@link https://github.com/multiformats/multicodec/blob/master/table.csv}
|
|
12
|
-
* encoded as a variable integer (more info about that at {@link https://github.com/multiformats/unsigned-varint)}.
|
|
13
|
-
*
|
|
14
|
-
* The key type is also found in that table.
|
|
15
|
-
* It's the name of the codec minus the `-pub` suffix.
|
|
16
|
-
*
|
|
17
|
-
* Example
|
|
18
|
-
* -------
|
|
19
|
-
* Ed25519 public key
|
|
20
|
-
* Key type: "ed25519"
|
|
21
|
-
* Magic bytes: [ 0xed, 0x01 ]
|
|
22
|
-
*/
|
|
23
|
-
type KeyTypes = Record<string, {
|
|
24
|
-
magicBytes: Uint8Array;
|
|
25
|
-
verify: (args: VerifyArgs) => Promise<boolean>;
|
|
26
|
-
}>;
|
|
27
|
-
export declare const did: {
|
|
28
|
-
keyTypes: KeyTypes;
|
|
29
|
-
};
|
|
30
9
|
export declare function ed25519Verify({ message, publicKey, signature }: VerifyArgs): Promise<boolean>;
|
|
31
10
|
/**
|
|
32
11
|
* Create a 32 character, DNS-friendly hash of the given DID.
|
|
@@ -44,23 +23,6 @@ export declare function sha256(bytes: Uint8Array): Promise<Uint8Array>;
|
|
|
44
23
|
* @returns {DID} A DID format string
|
|
45
24
|
*/
|
|
46
25
|
export declare function publicKeyToDid(_publicKey: Uint8Array | CryptoKey, keyType?: 'rsa' | 'ed25519'): Promise<DID>;
|
|
47
|
-
export declare const rsaOperations: {
|
|
48
|
-
verify: (msg: Msg, sig: Msg, publicKey: string | CryptoKey, charSize?: CharSize, hashAlg?: HashAlg) => Promise<boolean>;
|
|
49
|
-
sign: (msg: Msg, privateKey: CryptoKey, charSize?: CharSize) => Promise<ArrayBuffer>;
|
|
50
|
-
/**
|
|
51
|
-
* Encrypt the given AES key *to* a given public key.
|
|
52
|
-
*/
|
|
53
|
-
encrypt: (msg: Msg, publicKey: string | CryptoKey | Uint8Array, charSize?: CharSize, hashAlg?: HashAlg) => Promise<ArrayBuffer>;
|
|
54
|
-
/**
|
|
55
|
-
* Use RSA to decrypt the given data.
|
|
56
|
-
*/
|
|
57
|
-
decrypt: (_data: Uint8Array | string | ArrayBuffer, privateKey: CryptoKey | Uint8Array | ArrayBuffer) => Promise<Uint8Array>;
|
|
58
|
-
};
|
|
59
|
-
export declare function rsaVerify({ message, publicKey, signature }: VerifyArgs): Promise<boolean>;
|
|
60
|
-
export declare function importPublicKey(base64Key: string | ArrayBuffer, hashAlg: HashAlg, use: KeyUse): Promise<CryptoKey>;
|
|
61
|
-
export declare const InvalidKeyUse: Error;
|
|
62
|
-
export declare const InvalidMaxValue: Error;
|
|
63
|
-
export declare function checkValidKeyUse(use: KeyUse): void;
|
|
64
26
|
export declare function base64ToArrBuf(string: string): ArrayBuffer;
|
|
65
27
|
export declare const normalizeToBuf: (msg: Msg, strConv: (str: string) => ArrayBuffer) => ArrayBuffer;
|
|
66
28
|
export declare function normalizeBase64ToBuf(msg: Msg): ArrayBuffer;
|
|
@@ -68,8 +30,6 @@ export declare const normalizeUtf8ToBuf: (msg: Msg) => ArrayBuffer;
|
|
|
68
30
|
export declare function strToArrBuf(str: string, charSize: CharSize): ArrayBuffer;
|
|
69
31
|
export declare const normalizeUtf16ToBuf: (msg: Msg) => ArrayBuffer;
|
|
70
32
|
export declare function normalizeUnicodeToBuf(msg: Msg, charSize: CharSize): ArrayBuffer;
|
|
71
|
-
export declare function importRsaKey(key: Uint8Array | ArrayBuffer, keyUsages: KeyUsage[]): Promise<CryptoKey>;
|
|
72
|
-
export declare function isCryptoKey(val: unknown): val is CryptoKey;
|
|
73
33
|
export declare function getPublicKeyAsArrayBuffer(keypair: CryptoKeyPair | CryptoKey): Promise<ArrayBuffer>;
|
|
74
34
|
export declare function getPublicKeyAsUint8Array(keypair: CryptoKeyPair | CryptoKey): Promise<Uint8Array>;
|
|
75
35
|
export declare function didToPublicKey(did: string): ({
|
|
@@ -84,7 +44,6 @@ export declare function randomBuf(length: number, { max }?: {
|
|
|
84
44
|
}): ArrayBuffer;
|
|
85
45
|
export declare function joinBufs(fst: ArrayBuffer | Uint8Array, snd: ArrayBuffer | Uint8Array): ArrayBuffer;
|
|
86
46
|
export declare function makeEccKeypair(curve: 'X25519' | 'ECDSA', uses: 'encyrpt' | 'sign'): Promise<CryptoKeyPair>;
|
|
87
|
-
export declare function makeRSAKeypair(size: RsaSize, hashAlg: HashAlg, use: KeyUse): Promise<CryptoKeyPair>;
|
|
47
|
+
export declare function makeRSAKeypair(size: RsaSize, hashAlg: HashAlg, use: KeyUse, extractable?: boolean): Promise<CryptoKeyPair>;
|
|
88
48
|
export declare function getDeviceName(did: DID | string): Promise<string>;
|
|
89
|
-
export {};
|
|
90
49
|
//# sourceMappingURL=util.d.ts.map
|
package/dist/util.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACR,GAAG,EACH,GAAG,EACH,OAAO,EACP,WAAW,EACX,OAAO,EACP,OAAO,EACV,MAAM,YAAY,CAAA;AACnB,OAAO,EACH,MAAM,EACN,QAAQ,EACX,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACR,GAAG,EACH,GAAG,EACH,OAAO,EACP,WAAW,EACX,OAAO,EACP,OAAO,EACV,MAAM,YAAY,CAAA;AACnB,OAAO,EACH,MAAM,EACN,QAAQ,EACX,MAAM,YAAY,CAAA;AAgBnB,wBAAgB,aAAa,CAAE,IAAI,EAAE,UAAU,GAAG,WAAW,CAE5D;AAED,MAAM,MAAM,UAAU,GAAG;IACrB,OAAO,EAAC,UAAU,CAAA;IAClB,SAAS,EAAC,UAAU,CAAA;IACpB,SAAS,EAAC,UAAU,CAAA;CACvB,CAAA;AAED,wBAAsB,aAAa,CAAE,EACjC,OAAO,EACP,SAAS,EACT,SAAS,EACZ,EAAC,UAAU,GAAE,OAAO,CAAC,OAAO,CAAC,CAE7B;AAED;;;;;GAKG;AACH,wBAAsB,gBAA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OnD;AAED,wBAAgB,UAAU,CAAE,GAAG,EAAC,MAAM,+BAErC;AAED,wBAAsB,SAAS,CAC3B,GAAG,EAAC,MAAM,GAAC,UAAU,EACrB,IAAI,CAAC,EAAC,OAAO,CAAC,WAAW,CAAC,GAC5B,OAAO,CAAC,OAAO,CAAC,CAajB;AAED,wBAAgB,SAAS,CACrB,MAAM,EAAC,MAAM,EACb,EAAE,GAAG,EAAE,GAAC;IAAE,GAAG,EAAC,MAAM,CAAA;CAAiB,GACvC,WAAW,CA0BZ;AAED,wBAAgB,QAAQ,CAAE,GAAG,EAAC,WAAW,GAAC,UAAU,EAAE,GAAG,EAAC,WAAW,GAAC,UAAU,GAAE,WAAW,CAO5F;AAED,wBAAsB,cAAc,CAChC,KAAK,EAAC,QAAQ,GAAC,OAAO,EACtB,IAAI,EAAC,SAAS,GAAC,MAAM,GACvB,OAAO,CAAC,aAAa,CAAC,CAQvB;AAED,wBAAsB,cAAc,CAChC,IAAI,EAAC,OAAO,EACZ,OAAO,EAAC,OAAO,EACf,GAAG,EAAC,MAAM,EACV,WAAW,CAAC,EAAC,OAAO,GACtB,OAAO,CAAC,aAAa,CAAC,CAgBvB;AAMD,wBAAsB,aAAa,CAAE,GAAG,EAAC,GAAG,GAAC,MAAM,mBAMlD"}
|
package/dist/util.js
CHANGED
|
@@ -7,15 +7,12 @@ import {
|
|
|
7
7
|
KeyUse,
|
|
8
8
|
CharSize
|
|
9
9
|
} from "./types.js";
|
|
10
|
+
import { did, InvalidMaxValue } from "./crypto.js";
|
|
10
11
|
import {
|
|
11
12
|
BASE58_DID_PREFIX,
|
|
12
13
|
KEY_USE,
|
|
13
14
|
RSA_SIGN_ALGORITHM,
|
|
14
15
|
RSA_ALGORITHM,
|
|
15
|
-
DEFAULT_HASH_ALGORITHM,
|
|
16
|
-
DEFAULT_CHAR_SIZE,
|
|
17
|
-
SALT_LENGTH,
|
|
18
|
-
RSA_HASHING_ALGORITHM,
|
|
19
16
|
RSA_DID_PREFIX,
|
|
20
17
|
KEY_TYPE,
|
|
21
18
|
EDWARDS_DID_PREFIX,
|
|
@@ -27,24 +24,6 @@ function toArrayBuffer(data) {
|
|
|
27
24
|
return new Uint8Array(data).buffer;
|
|
28
25
|
}
|
|
29
26
|
__name(toArrayBuffer, "toArrayBuffer");
|
|
30
|
-
const did = {
|
|
31
|
-
keyTypes: {
|
|
32
|
-
"bls12-381": {
|
|
33
|
-
magicBytes: new Uint8Array([234, 1]),
|
|
34
|
-
verify: /* @__PURE__ */ __name(() => {
|
|
35
|
-
throw new Error("Not implemented");
|
|
36
|
-
}, "verify")
|
|
37
|
-
},
|
|
38
|
-
rsa: {
|
|
39
|
-
magicBytes: new Uint8Array([0, 245, 2]),
|
|
40
|
-
verify: rsaVerify2
|
|
41
|
-
},
|
|
42
|
-
ed25519: {
|
|
43
|
-
magicBytes: new Uint8Array([237, 1]),
|
|
44
|
-
verify: ed25519Verify
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
27
|
async function ed25519Verify({
|
|
49
28
|
message,
|
|
50
29
|
publicKey,
|
|
@@ -77,108 +56,6 @@ async function publicKeyToDid(_publicKey, keyType = "rsa") {
|
|
|
77
56
|
return BASE58_DID_PREFIX + uToString(prefixedBuf, "base58btc");
|
|
78
57
|
}
|
|
79
58
|
__name(publicKeyToDid, "publicKeyToDid");
|
|
80
|
-
const rsaOperations = {
|
|
81
|
-
verify: /* @__PURE__ */ __name(async function rsaVerify(msg, sig, publicKey, charSize = DEFAULT_CHAR_SIZE, hashAlg = DEFAULT_HASH_ALGORITHM) {
|
|
82
|
-
return webcrypto.subtle.verify(
|
|
83
|
-
{
|
|
84
|
-
name: RSA_SIGN_ALGORITHM,
|
|
85
|
-
saltLength: SALT_LENGTH
|
|
86
|
-
},
|
|
87
|
-
typeof publicKey === "string" ? await importPublicKey(publicKey, hashAlg, KeyUse.Sign) : publicKey,
|
|
88
|
-
normalizeBase64ToBuf(sig),
|
|
89
|
-
normalizeUnicodeToBuf(msg, charSize)
|
|
90
|
-
);
|
|
91
|
-
}, "rsaVerify"),
|
|
92
|
-
sign: /* @__PURE__ */ __name(async function sign(msg, privateKey, charSize = DEFAULT_CHAR_SIZE) {
|
|
93
|
-
return webcrypto.subtle.sign(
|
|
94
|
-
{ name: RSA_SIGN_ALGORITHM, saltLength: SALT_LENGTH },
|
|
95
|
-
privateKey,
|
|
96
|
-
normalizeUnicodeToBuf(msg, charSize)
|
|
97
|
-
);
|
|
98
|
-
}, "sign"),
|
|
99
|
-
/**
|
|
100
|
-
* Encrypt the given AES key *to* a given public key.
|
|
101
|
-
*/
|
|
102
|
-
encrypt: /* @__PURE__ */ __name(async function rsaEncrypt(msg, publicKey, charSize = DEFAULT_CHAR_SIZE, hashAlg = DEFAULT_HASH_ALGORITHM) {
|
|
103
|
-
let pubKey;
|
|
104
|
-
if (typeof publicKey === "string") {
|
|
105
|
-
pubKey = await importPublicKey(publicKey, hashAlg, KeyUse.Exchange);
|
|
106
|
-
} else {
|
|
107
|
-
pubKey = publicKey instanceof Uint8Array ? await importPublicKey(toArrayBuffer(publicKey), hashAlg, KeyUse.Exchange) : publicKey;
|
|
108
|
-
}
|
|
109
|
-
return webcrypto.subtle.encrypt(
|
|
110
|
-
{ name: RSA_ALGORITHM },
|
|
111
|
-
pubKey,
|
|
112
|
-
normalizeUnicodeToBuf(msg, charSize)
|
|
113
|
-
);
|
|
114
|
-
}, "rsaEncrypt"),
|
|
115
|
-
/**
|
|
116
|
-
* Use RSA to decrypt the given data.
|
|
117
|
-
*/
|
|
118
|
-
decrypt: /* @__PURE__ */ __name(async function rsaDecrypt(_data, privateKey) {
|
|
119
|
-
const key = isCryptoKey(privateKey) ? privateKey : await importRsaKey(privateKey, ["decrypt"]);
|
|
120
|
-
let data;
|
|
121
|
-
if (typeof _data === "string") {
|
|
122
|
-
data = fromString(_data, "base64pad");
|
|
123
|
-
} else {
|
|
124
|
-
data = _data;
|
|
125
|
-
}
|
|
126
|
-
const arrayBuffer = await webcrypto.subtle.decrypt(
|
|
127
|
-
{ name: RSA_ALGORITHM },
|
|
128
|
-
key,
|
|
129
|
-
data instanceof Uint8Array ? toArrayBuffer(data) : data
|
|
130
|
-
);
|
|
131
|
-
const arr = new Uint8Array(arrayBuffer);
|
|
132
|
-
return arr;
|
|
133
|
-
}, "rsaDecrypt")
|
|
134
|
-
};
|
|
135
|
-
async function rsaVerify2({
|
|
136
|
-
message,
|
|
137
|
-
publicKey,
|
|
138
|
-
signature
|
|
139
|
-
}) {
|
|
140
|
-
return rsaOperations.verify(
|
|
141
|
-
message,
|
|
142
|
-
signature,
|
|
143
|
-
await webcrypto.subtle.importKey(
|
|
144
|
-
"spki",
|
|
145
|
-
toArrayBuffer(publicKey),
|
|
146
|
-
{ name: RSA_SIGN_ALGORITHM, hash: RSA_HASHING_ALGORITHM },
|
|
147
|
-
false,
|
|
148
|
-
["verify"]
|
|
149
|
-
),
|
|
150
|
-
8
|
|
151
|
-
);
|
|
152
|
-
}
|
|
153
|
-
__name(rsaVerify2, "rsaVerify");
|
|
154
|
-
async function importPublicKey(base64Key, hashAlg, use) {
|
|
155
|
-
checkValidKeyUse(use);
|
|
156
|
-
const alg = use === KeyUse.Exchange ? RSA_ALGORITHM : RSA_SIGN_ALGORITHM;
|
|
157
|
-
const uses = use === KeyUse.Exchange ? ["encrypt"] : ["verify"];
|
|
158
|
-
const buf = typeof base64Key === "string" ? base64ToArrBuf(stripKeyHeader(base64Key)) : base64Key;
|
|
159
|
-
return webcrypto.subtle.importKey("spki", buf, {
|
|
160
|
-
name: alg,
|
|
161
|
-
hash: { name: hashAlg }
|
|
162
|
-
}, true, uses);
|
|
163
|
-
}
|
|
164
|
-
__name(importPublicKey, "importPublicKey");
|
|
165
|
-
const InvalidKeyUse = new Error("Invalid key use. Please use 'encryption' or 'signing");
|
|
166
|
-
const InvalidMaxValue = new Error("Max must be less than 256 and greater than 0");
|
|
167
|
-
function checkValidKeyUse(use) {
|
|
168
|
-
checkValid(use, [KeyUse.Sign, KeyUse.Exchange], InvalidKeyUse);
|
|
169
|
-
}
|
|
170
|
-
__name(checkValidKeyUse, "checkValidKeyUse");
|
|
171
|
-
function checkValid(toCheck, opts, error) {
|
|
172
|
-
const match = opts.some((opt) => opt === toCheck);
|
|
173
|
-
if (!match) {
|
|
174
|
-
throw error;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
__name(checkValid, "checkValid");
|
|
178
|
-
function stripKeyHeader(base64Key) {
|
|
179
|
-
return base64Key.replace("-----BEGIN PUBLIC KEY-----\n", "").replace("\n-----END PUBLIC KEY-----", "");
|
|
180
|
-
}
|
|
181
|
-
__name(stripKeyHeader, "stripKeyHeader");
|
|
182
59
|
function base64ToArrBuf(string) {
|
|
183
60
|
const uint8 = fromString(string, "base64pad");
|
|
184
61
|
return toArrayBuffer(uint8);
|
|
@@ -221,24 +98,6 @@ function normalizeUnicodeToBuf(msg, charSize) {
|
|
|
221
98
|
}
|
|
222
99
|
}
|
|
223
100
|
__name(normalizeUnicodeToBuf, "normalizeUnicodeToBuf");
|
|
224
|
-
function importRsaKey(key, keyUsages) {
|
|
225
|
-
return webcrypto.subtle.importKey(
|
|
226
|
-
"spki",
|
|
227
|
-
key instanceof Uint8Array ? toArrayBuffer(key) : key,
|
|
228
|
-
{ name: RSA_ALGORITHM, hash: RSA_HASHING_ALGORITHM },
|
|
229
|
-
false,
|
|
230
|
-
keyUsages
|
|
231
|
-
);
|
|
232
|
-
}
|
|
233
|
-
__name(importRsaKey, "importRsaKey");
|
|
234
|
-
function isCryptoKey(val) {
|
|
235
|
-
return hasProp(val, "algorithm") && hasProp(val, "extractable") && hasProp(val, "type");
|
|
236
|
-
}
|
|
237
|
-
__name(isCryptoKey, "isCryptoKey");
|
|
238
|
-
function hasProp(data, prop) {
|
|
239
|
-
return typeof data === "object" && data != null && prop in data;
|
|
240
|
-
}
|
|
241
|
-
__name(hasProp, "hasProp");
|
|
242
101
|
async function getPublicKeyAsArrayBuffer(keypair) {
|
|
243
102
|
const spki = keypair instanceof CryptoKey ? await webcrypto.subtle.exportKey(
|
|
244
103
|
"spki",
|
|
@@ -373,7 +232,8 @@ async function makeEccKeypair(curve, uses) {
|
|
|
373
232
|
return keys;
|
|
374
233
|
}
|
|
375
234
|
__name(makeEccKeypair, "makeEccKeypair");
|
|
376
|
-
async function makeRSAKeypair(size, hashAlg, use) {
|
|
235
|
+
async function makeRSAKeypair(size, hashAlg, use, extractable) {
|
|
236
|
+
const isExtractable = extractable || false;
|
|
377
237
|
if (!Object.values(KeyUse).includes(use)) {
|
|
378
238
|
throw new Error("invalid key use");
|
|
379
239
|
}
|
|
@@ -384,7 +244,7 @@ async function makeRSAKeypair(size, hashAlg, use) {
|
|
|
384
244
|
modulusLength: size,
|
|
385
245
|
publicExponent: publicExponent(),
|
|
386
246
|
hash: { name: hashAlg }
|
|
387
|
-
},
|
|
247
|
+
}, isExtractable, uses);
|
|
388
248
|
}
|
|
389
249
|
__name(makeRSAKeypair, "makeRSAKeypair");
|
|
390
250
|
function publicExponent() {
|
|
@@ -399,12 +259,8 @@ async function getDeviceName(did2) {
|
|
|
399
259
|
}
|
|
400
260
|
__name(getDeviceName, "getDeviceName");
|
|
401
261
|
export {
|
|
402
|
-
InvalidKeyUse,
|
|
403
|
-
InvalidMaxValue,
|
|
404
262
|
base64ToArrBuf,
|
|
405
|
-
checkValidKeyUse,
|
|
406
263
|
createDeviceName,
|
|
407
|
-
did,
|
|
408
264
|
didToPublicKey,
|
|
409
265
|
ed25519Verify,
|
|
410
266
|
fromBase64,
|
|
@@ -412,9 +268,6 @@ export {
|
|
|
412
268
|
getPublicKeyAsArrayBuffer,
|
|
413
269
|
getPublicKeyAsUint8Array,
|
|
414
270
|
importKey,
|
|
415
|
-
importPublicKey,
|
|
416
|
-
importRsaKey,
|
|
417
|
-
isCryptoKey,
|
|
418
271
|
joinBufs,
|
|
419
272
|
makeEccKeypair,
|
|
420
273
|
makeRSAKeypair,
|
|
@@ -425,10 +278,9 @@ export {
|
|
|
425
278
|
normalizeUtf8ToBuf,
|
|
426
279
|
publicKeyToDid,
|
|
427
280
|
randomBuf,
|
|
428
|
-
rsaOperations,
|
|
429
|
-
rsaVerify2 as rsaVerify,
|
|
430
281
|
sha256,
|
|
431
282
|
strToArrBuf,
|
|
283
|
+
toArrayBuffer,
|
|
432
284
|
toBase64
|
|
433
285
|
};
|
|
434
286
|
//# sourceMappingURL=util.js.map
|