@0xkey-io/api-key-stamper 0.1.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.
- package/CHANGELOG.md +5 -0
- package/LICENSE +201 -0
- package/README.md +26 -0
- package/dist/_kernel/stamper.d.ts +29 -0
- package/dist/_kernel/stamper.d.ts.map +1 -0
- package/dist/_kernel/stamping.d.ts +6 -0
- package/dist/_kernel/stamping.d.ts.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +371 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +366 -0
- package/dist/index.mjs.map +1 -0
- package/dist/nodecrypto-BEvYdLDk.js +46 -0
- package/dist/nodecrypto-BEvYdLDk.js.map +1 -0
- package/dist/nodecrypto-CWFu007J.js +25 -0
- package/dist/nodecrypto-CWFu007J.js.map +1 -0
- package/dist/nodecrypto.d.ts +6 -0
- package/dist/nodecrypto.d.ts.map +1 -0
- package/dist/purejs-BG8ixYIu.js +20 -0
- package/dist/purejs-BG8ixYIu.js.map +1 -0
- package/dist/purejs-XGKef2yb.js +18 -0
- package/dist/purejs-XGKef2yb.js.map +1 -0
- package/dist/purejs.d.ts +6 -0
- package/dist/purejs.d.ts.map +1 -0
- package/dist/tink/bytes.d.ts +45 -0
- package/dist/tink/bytes.d.ts.map +1 -0
- package/dist/tink/elliptic_curves.d.ts +18 -0
- package/dist/tink/elliptic_curves.d.ts.map +1 -0
- package/dist/utils-65MgUjOA.js +30 -0
- package/dist/utils-65MgUjOA.js.map +1 -0
- package/dist/utils-U6G_myeu.js +28 -0
- package/dist/utils-U6G_myeu.js.map +1 -0
- package/dist/utils.d.ts +14 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/webcrypto-BM8R-Kt_.js +115 -0
- package/dist/webcrypto-BM8R-Kt_.js.map +1 -0
- package/dist/webcrypto-BdGOYdbN.js +113 -0
- package/dist/webcrypto-BdGOYdbN.js.map +1 -0
- package/dist/webcrypto.d.ts +7 -0
- package/dist/webcrypto.d.ts.map +1 -0
- package/package.json +62 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { pointDecode } from './index.mjs';
|
|
2
|
+
import { uint8ArrayFromHexString, hexStringToBase64url, DEFAULT_JWK_MEMBER_BYTE_LENGTH } from '@0xkey-io/encoding';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Converts a ZeroXKey API key pair into a JSON Web Key (JWK) format.
|
|
6
|
+
* This function accepts P-256 API keys only.
|
|
7
|
+
*
|
|
8
|
+
* @param {Object} input - The ZeroXKey API key components.
|
|
9
|
+
* @param {string} input.uncompressedPrivateKeyHex - Hexadecimal-encoded uncompressed private key (32-byte scalar).
|
|
10
|
+
* @param {string} input.compressedPublicKeyHex - Hexadecimal-encoded compressed public key (33 bytes).
|
|
11
|
+
* @returns {JsonWebKey} A JSON Web Key object representing the EC P-256 key.
|
|
12
|
+
*/
|
|
13
|
+
function convertZeroXKeyApiKeyToJwk(input) {
|
|
14
|
+
const { uncompressedPrivateKeyHex, compressedPublicKeyHex } = input;
|
|
15
|
+
let jwk;
|
|
16
|
+
try {
|
|
17
|
+
jwk = pointDecode(uint8ArrayFromHexString(compressedPublicKeyHex));
|
|
18
|
+
}
|
|
19
|
+
catch (e) {
|
|
20
|
+
throw new Error(`unable to load API key: invalid public key. Did you switch your public and private key by accident? Is your public key a valid, compressed P-256 public key?`);
|
|
21
|
+
}
|
|
22
|
+
// Ensure that d is sufficiently padded
|
|
23
|
+
jwk.d = hexStringToBase64url(uncompressedPrivateKeyHex, DEFAULT_JWK_MEMBER_BYTE_LENGTH);
|
|
24
|
+
return jwk;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { convertZeroXKeyApiKeyToJwk as c };
|
|
28
|
+
//# sourceMappingURL=utils-U6G_myeu.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils-U6G_myeu.js","sources":["../src/utils.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAOA;;;;;;;;AAQG;AACG,SAAU,0BAA0B,CAAC,KAG1C,EAAA;AACC,IAAA,MAAM,EAAE,yBAAyB,EAAE,sBAAsB,EAAE,GAAG,KAAK;AAEnE,IAAA,IAAI,GAAG;AACP,IAAA,IAAI;QACF,GAAG,GAAG,WAAW,CAAC,uBAAuB,CAAC,sBAAsB,CAAC,CAAC;IACpE;IAAE,OAAO,CAAC,EAAE;AACV,QAAA,MAAM,IAAI,KAAK,CACb,CAAA,4JAAA,CAA8J,CAC/J;IACH;;IAGA,GAAG,CAAC,CAAC,GAAG,oBAAoB,CAC1B,yBAAyB,EACzB,8BAA8B,CAC/B;AAED,IAAA,OAAO,GAAG;AACZ;;;;"}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a ZeroXKey API key pair into a JSON Web Key (JWK) format.
|
|
3
|
+
* This function accepts P-256 API keys only.
|
|
4
|
+
*
|
|
5
|
+
* @param {Object} input - The ZeroXKey API key components.
|
|
6
|
+
* @param {string} input.uncompressedPrivateKeyHex - Hexadecimal-encoded uncompressed private key (32-byte scalar).
|
|
7
|
+
* @param {string} input.compressedPublicKeyHex - Hexadecimal-encoded compressed public key (33 bytes).
|
|
8
|
+
* @returns {JsonWebKey} A JSON Web Key object representing the EC P-256 key.
|
|
9
|
+
*/
|
|
10
|
+
export declare function convertZeroXKeyApiKeyToJwk(input: {
|
|
11
|
+
uncompressedPrivateKeyHex: string;
|
|
12
|
+
compressedPublicKeyHex: string;
|
|
13
|
+
}): JsonWebKey;
|
|
14
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAOA;;;;;;;;GAQG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE;IAChD,yBAAyB,EAAE,MAAM,CAAC;IAClC,sBAAsB,EAAE,MAAM,CAAC;CAChC,GAAG,UAAU,CAmBb"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utils = require('./utils-65MgUjOA.js');
|
|
4
|
+
var encoding = require('@0xkey-io/encoding');
|
|
5
|
+
require('./index.js');
|
|
6
|
+
require('@0xkey-io/crypto');
|
|
7
|
+
|
|
8
|
+
/// <reference lib="dom" />
|
|
9
|
+
const signWithApiKey = async (input) => {
|
|
10
|
+
const { content, publicKey, privateKey } = input;
|
|
11
|
+
const key = await importZeroXKeyApiKey({
|
|
12
|
+
uncompressedPrivateKeyHex: privateKey,
|
|
13
|
+
compressedPublicKeyHex: publicKey,
|
|
14
|
+
});
|
|
15
|
+
return await signMessage({ key, content });
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Imports a P-256 ZeroXKey API key into a WebCrypto `CryptoKey`.
|
|
19
|
+
*
|
|
20
|
+
* @param {Object} input - The ZeroXKey API key components.
|
|
21
|
+
* @param {string} input.uncompressedPrivateKeyHex - Hexadecimal-encoded uncompressed private key (32-byte scalar).
|
|
22
|
+
* @param {string} input.compressedPublicKeyHex - Hexadecimal-encoded compressed public key (33 bytes).
|
|
23
|
+
* @returns {Promise<CryptoKey>} A `CryptoKey` object representing a P-256 key.
|
|
24
|
+
*/
|
|
25
|
+
async function importZeroXKeyApiKey(input) {
|
|
26
|
+
const { uncompressedPrivateKeyHex, compressedPublicKeyHex } = input;
|
|
27
|
+
const jwk = utils.convertZeroXKeyApiKeyToJwk({
|
|
28
|
+
uncompressedPrivateKeyHex,
|
|
29
|
+
compressedPublicKeyHex,
|
|
30
|
+
});
|
|
31
|
+
return await crypto.subtle.importKey("jwk", jwk, {
|
|
32
|
+
name: "ECDSA",
|
|
33
|
+
namedCurve: "P-256",
|
|
34
|
+
}, false, // not extractable
|
|
35
|
+
["sign"]);
|
|
36
|
+
}
|
|
37
|
+
async function signMessage(input) {
|
|
38
|
+
const { key, content } = input;
|
|
39
|
+
const signatureIeee1363 = await crypto.subtle.sign({
|
|
40
|
+
name: "ECDSA",
|
|
41
|
+
hash: "SHA-256",
|
|
42
|
+
}, key, new TextEncoder().encode(content));
|
|
43
|
+
const signatureDer = convertEcdsaIeee1363ToDer(new Uint8Array(signatureIeee1363));
|
|
44
|
+
return encoding.uint8ArrayToHexString(signatureDer);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* `SubtleCrypto.sign(...)` outputs signature in IEEE P1363 format:
|
|
48
|
+
* - https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/sign#ecdsa
|
|
49
|
+
*
|
|
50
|
+
* ZeroXKey expects the signature encoding to be DER-encoded ASN.1.
|
|
51
|
+
*
|
|
52
|
+
* Code modified from https://github.com/google/tink/blob/6f74b99a2bfe6677e3670799116a57268fd067fa/javascript/subtle/elliptic_curves.ts#L114
|
|
53
|
+
*
|
|
54
|
+
* Transform an ECDSA signature in IEEE 1363 encoding to DER encoding.
|
|
55
|
+
*
|
|
56
|
+
* @param ieee the ECDSA signature in IEEE encoding
|
|
57
|
+
* @return ECDSA signature in DER encoding
|
|
58
|
+
*/
|
|
59
|
+
function convertEcdsaIeee1363ToDer(ieee) {
|
|
60
|
+
if (ieee.length % 2 != 0 || ieee.length == 0 || ieee.length > 132) {
|
|
61
|
+
throw new Error("Invalid IEEE P1363 signature encoding. Length: " + ieee.length);
|
|
62
|
+
}
|
|
63
|
+
const r = toUnsignedBigNum(ieee.subarray(0, ieee.length / 2));
|
|
64
|
+
const s = toUnsignedBigNum(ieee.subarray(ieee.length / 2, ieee.length));
|
|
65
|
+
let offset = 0;
|
|
66
|
+
const length = 1 + 1 + r.length + 1 + 1 + s.length;
|
|
67
|
+
let der;
|
|
68
|
+
if (length >= 128) {
|
|
69
|
+
der = new Uint8Array(length + 3);
|
|
70
|
+
der[offset++] = 48;
|
|
71
|
+
der[offset++] = 128 + 1;
|
|
72
|
+
der[offset++] = length;
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
der = new Uint8Array(length + 2);
|
|
76
|
+
der[offset++] = 48;
|
|
77
|
+
der[offset++] = length;
|
|
78
|
+
}
|
|
79
|
+
der[offset++] = 2;
|
|
80
|
+
der[offset++] = r.length;
|
|
81
|
+
der.set(r, offset);
|
|
82
|
+
offset += r.length;
|
|
83
|
+
der[offset++] = 2;
|
|
84
|
+
der[offset++] = s.length;
|
|
85
|
+
der.set(s, offset);
|
|
86
|
+
return der;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Code modified from https://github.com/google/tink/blob/6f74b99a2bfe6677e3670799116a57268fd067fa/javascript/subtle/elliptic_curves.ts#L311
|
|
90
|
+
*
|
|
91
|
+
* Transform a big integer in big endian to minimal unsigned form which has
|
|
92
|
+
* no extra zero at the beginning except when the highest bit is set.
|
|
93
|
+
*/
|
|
94
|
+
function toUnsignedBigNum(bytes) {
|
|
95
|
+
// Remove zero prefixes.
|
|
96
|
+
let start = 0;
|
|
97
|
+
while (start < bytes.length && bytes[start] == 0) {
|
|
98
|
+
start++;
|
|
99
|
+
}
|
|
100
|
+
if (start == bytes.length) {
|
|
101
|
+
start = bytes.length - 1;
|
|
102
|
+
}
|
|
103
|
+
let extraZero = 0;
|
|
104
|
+
// If the 1st bit is not zero, add 1 zero byte.
|
|
105
|
+
if ((bytes[start] & 128) == 128) {
|
|
106
|
+
// Add extra zero.
|
|
107
|
+
extraZero = 1;
|
|
108
|
+
}
|
|
109
|
+
const res = new Uint8Array(bytes.length - start + extraZero);
|
|
110
|
+
res.set(bytes.subarray(start), extraZero);
|
|
111
|
+
return res;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
exports.signWithApiKey = signWithApiKey;
|
|
115
|
+
//# sourceMappingURL=webcrypto-BM8R-Kt_.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webcrypto-BM8R-Kt_.js","sources":["../src/webcrypto.ts"],"sourcesContent":[null],"names":["conv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,IAAA,GAAG,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC;AAClB,IAAA,MAAM,IAAI,CAAC,CAAC,MAAM;AAClB,IAAA,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC;IACjB,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM;AACxB,IAAA,GAAG,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC;AAClB,IAAA,OAAO,GAAG;AACZ;AAEA;;;;;AAKG;AACH,SAAS,gBAAgB,CAAC,KAAiB,EAAA;;IAEzC,IAAI,KAAK,GAAG,CAAC;AACb,IAAA,OAAO,KAAK,GAAG,KAAK,CAAC,MAAM,IAAI,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE;AAChD,QAAA,KAAK,EAAE;IACT;AACA,IAAA,IAAI,KAAK,IAAI,KAAK,CAAC,MAAM,EAAE;AACzB,QAAA,KAAK,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC;IAC1B;IACA,IAAI,SAAS,GAAG,CAAC;;IAGjB,IAAI,CAAC,KAAK,CAAC,KAAK,CAAE,GAAG,GAAG,KAAK,GAAG,EAAE;;QAEhC,SAAS,GAAG,CAAC;IACf;AACA,IAAA,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,KAAK,CAAC,MAAM,GAAG,KAAK,GAAG,SAAS,CAAC;AAC5D,IAAA,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,SAAS,CAAC;AACzC,IAAA,OAAO,GAAG;AACZ;;;;"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { c as convertZeroXKeyApiKeyToJwk } from './utils-U6G_myeu.js';
|
|
2
|
+
import { uint8ArrayToHexString } from '@0xkey-io/encoding';
|
|
3
|
+
import './index.mjs';
|
|
4
|
+
import '@0xkey-io/crypto';
|
|
5
|
+
|
|
6
|
+
/// <reference lib="dom" />
|
|
7
|
+
const signWithApiKey = async (input) => {
|
|
8
|
+
const { content, publicKey, privateKey } = input;
|
|
9
|
+
const key = await importZeroXKeyApiKey({
|
|
10
|
+
uncompressedPrivateKeyHex: privateKey,
|
|
11
|
+
compressedPublicKeyHex: publicKey,
|
|
12
|
+
});
|
|
13
|
+
return await signMessage({ key, content });
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Imports a P-256 ZeroXKey API key into a WebCrypto `CryptoKey`.
|
|
17
|
+
*
|
|
18
|
+
* @param {Object} input - The ZeroXKey API key components.
|
|
19
|
+
* @param {string} input.uncompressedPrivateKeyHex - Hexadecimal-encoded uncompressed private key (32-byte scalar).
|
|
20
|
+
* @param {string} input.compressedPublicKeyHex - Hexadecimal-encoded compressed public key (33 bytes).
|
|
21
|
+
* @returns {Promise<CryptoKey>} A `CryptoKey` object representing a P-256 key.
|
|
22
|
+
*/
|
|
23
|
+
async function importZeroXKeyApiKey(input) {
|
|
24
|
+
const { uncompressedPrivateKeyHex, compressedPublicKeyHex } = input;
|
|
25
|
+
const jwk = convertZeroXKeyApiKeyToJwk({
|
|
26
|
+
uncompressedPrivateKeyHex,
|
|
27
|
+
compressedPublicKeyHex,
|
|
28
|
+
});
|
|
29
|
+
return await crypto.subtle.importKey("jwk", jwk, {
|
|
30
|
+
name: "ECDSA",
|
|
31
|
+
namedCurve: "P-256",
|
|
32
|
+
}, false, // not extractable
|
|
33
|
+
["sign"]);
|
|
34
|
+
}
|
|
35
|
+
async function signMessage(input) {
|
|
36
|
+
const { key, content } = input;
|
|
37
|
+
const signatureIeee1363 = await crypto.subtle.sign({
|
|
38
|
+
name: "ECDSA",
|
|
39
|
+
hash: "SHA-256",
|
|
40
|
+
}, key, new TextEncoder().encode(content));
|
|
41
|
+
const signatureDer = convertEcdsaIeee1363ToDer(new Uint8Array(signatureIeee1363));
|
|
42
|
+
return uint8ArrayToHexString(signatureDer);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* `SubtleCrypto.sign(...)` outputs signature in IEEE P1363 format:
|
|
46
|
+
* - https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/sign#ecdsa
|
|
47
|
+
*
|
|
48
|
+
* ZeroXKey expects the signature encoding to be DER-encoded ASN.1.
|
|
49
|
+
*
|
|
50
|
+
* Code modified from https://github.com/google/tink/blob/6f74b99a2bfe6677e3670799116a57268fd067fa/javascript/subtle/elliptic_curves.ts#L114
|
|
51
|
+
*
|
|
52
|
+
* Transform an ECDSA signature in IEEE 1363 encoding to DER encoding.
|
|
53
|
+
*
|
|
54
|
+
* @param ieee the ECDSA signature in IEEE encoding
|
|
55
|
+
* @return ECDSA signature in DER encoding
|
|
56
|
+
*/
|
|
57
|
+
function convertEcdsaIeee1363ToDer(ieee) {
|
|
58
|
+
if (ieee.length % 2 != 0 || ieee.length == 0 || ieee.length > 132) {
|
|
59
|
+
throw new Error("Invalid IEEE P1363 signature encoding. Length: " + ieee.length);
|
|
60
|
+
}
|
|
61
|
+
const r = toUnsignedBigNum(ieee.subarray(0, ieee.length / 2));
|
|
62
|
+
const s = toUnsignedBigNum(ieee.subarray(ieee.length / 2, ieee.length));
|
|
63
|
+
let offset = 0;
|
|
64
|
+
const length = 1 + 1 + r.length + 1 + 1 + s.length;
|
|
65
|
+
let der;
|
|
66
|
+
if (length >= 128) {
|
|
67
|
+
der = new Uint8Array(length + 3);
|
|
68
|
+
der[offset++] = 48;
|
|
69
|
+
der[offset++] = 128 + 1;
|
|
70
|
+
der[offset++] = length;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
der = new Uint8Array(length + 2);
|
|
74
|
+
der[offset++] = 48;
|
|
75
|
+
der[offset++] = length;
|
|
76
|
+
}
|
|
77
|
+
der[offset++] = 2;
|
|
78
|
+
der[offset++] = r.length;
|
|
79
|
+
der.set(r, offset);
|
|
80
|
+
offset += r.length;
|
|
81
|
+
der[offset++] = 2;
|
|
82
|
+
der[offset++] = s.length;
|
|
83
|
+
der.set(s, offset);
|
|
84
|
+
return der;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Code modified from https://github.com/google/tink/blob/6f74b99a2bfe6677e3670799116a57268fd067fa/javascript/subtle/elliptic_curves.ts#L311
|
|
88
|
+
*
|
|
89
|
+
* Transform a big integer in big endian to minimal unsigned form which has
|
|
90
|
+
* no extra zero at the beginning except when the highest bit is set.
|
|
91
|
+
*/
|
|
92
|
+
function toUnsignedBigNum(bytes) {
|
|
93
|
+
// Remove zero prefixes.
|
|
94
|
+
let start = 0;
|
|
95
|
+
while (start < bytes.length && bytes[start] == 0) {
|
|
96
|
+
start++;
|
|
97
|
+
}
|
|
98
|
+
if (start == bytes.length) {
|
|
99
|
+
start = bytes.length - 1;
|
|
100
|
+
}
|
|
101
|
+
let extraZero = 0;
|
|
102
|
+
// If the 1st bit is not zero, add 1 zero byte.
|
|
103
|
+
if ((bytes[start] & 128) == 128) {
|
|
104
|
+
// Add extra zero.
|
|
105
|
+
extraZero = 1;
|
|
106
|
+
}
|
|
107
|
+
const res = new Uint8Array(bytes.length - start + extraZero);
|
|
108
|
+
res.set(bytes.subarray(start), extraZero);
|
|
109
|
+
return res;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export { signWithApiKey };
|
|
113
|
+
//# sourceMappingURL=webcrypto-BdGOYdbN.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webcrypto-BdGOYdbN.js","sources":["../src/webcrypto.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;;AAAA;MAIa,cAAc,GAAG,OAAO,KAIpC,KAAqB;IACpB,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,KAAK;AAEhD,IAAA,MAAM,GAAG,GAAG,MAAM,oBAAoB,CAAC;AACrC,QAAA,yBAAyB,EAAE,UAAU;AACrC,QAAA,sBAAsB,EAAE,SAAS;AAClC,KAAA,CAAC;IACF,OAAO,MAAM,WAAW,CAAC,EAAE,GAAG,EAAE,OAAO,EAAE,C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AEzC,IAAI,KAAK,GAAG,CAAC;AACb,IAAA,OAAO,KAAK,GAAG,KAAK,CAAC,MAAM,IAAI,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE;AAChD,QAAA,KAAK,EAAE;IACT;AACA,IAAA,IAAI,KAAK,IAAI,KAAK,CAAC,MAAM,EAAE;AACzB,QAAA,KAAK,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC;IAC1B;IACA,IAAI,SAAS,GAAG,CAAC;;IAGjB,IAAI,CAAC,KAAK,CAAC,KAAK,CAAE,GAAG,GAAG,KAAK,GAAG,EAAE;;QAEhC,SAAS,GAAG,CAAC;IACf;AACA,IAAA,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,KAAK,CAAC,MAAM,GAAG,KAAK,GAAG,SAAS,CAAC;AAC5D,IAAA,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,SAAS,CAAC;AACzC,IAAA,OAAO,GAAG;AACZ;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webcrypto.d.ts","sourceRoot":"","sources":["../src/webcrypto.ts"],"names":[],"mappings":";AAIA,eAAO,MAAM,cAAc,UAAiB;IAC1C,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;CACpB,KAAG,QAAQ,MAAM,CAQjB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@0xkey-io/api-key-stamper",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"module": "./dist/index.mjs",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"import": "./dist/index.mjs",
|
|
10
|
+
"require": "./dist/index.js",
|
|
11
|
+
"default": "./dist/index.mjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"license": "Apache-2.0",
|
|
16
|
+
"description": "API key stamper for @0xkey-io/http",
|
|
17
|
+
"keywords": [
|
|
18
|
+
"0xkey",
|
|
19
|
+
"http",
|
|
20
|
+
"stamper",
|
|
21
|
+
"api key"
|
|
22
|
+
],
|
|
23
|
+
"author": {
|
|
24
|
+
"name": "0xkey",
|
|
25
|
+
"url": "https://0xkey.com/"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/0xkey-io/sdk-js/tree/main/packages/api-key-stamper",
|
|
28
|
+
"bugs": {
|
|
29
|
+
"url": "https://github.com/0xkey-io/sdk-js/issues"
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/0xkey-io/sdk-js.git",
|
|
34
|
+
"directory": "packages/api-key-stamper"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist/",
|
|
38
|
+
"CHANGELOG.md",
|
|
39
|
+
"README.md"
|
|
40
|
+
],
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=18.0.0"
|
|
46
|
+
},
|
|
47
|
+
"browser": {
|
|
48
|
+
"crypto": false
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"@noble/curves": "^1.3.0",
|
|
52
|
+
"sha256-uint8array": "^0.10.7",
|
|
53
|
+
"@0xkey-io/encoding": "0.1.0",
|
|
54
|
+
"@0xkey-io/crypto": "0.1.0"
|
|
55
|
+
},
|
|
56
|
+
"scripts": {
|
|
57
|
+
"build": "rollup -c",
|
|
58
|
+
"clean": "rimraf ./dist ./.cache",
|
|
59
|
+
"test": "jest",
|
|
60
|
+
"typecheck": "tsc -p tsconfig.typecheck.json"
|
|
61
|
+
}
|
|
62
|
+
}
|