@turnkey/http 0.7.1-beta.0 → 0.8.0-beta.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 +7 -0
- package/README.md +3 -1
- package/dist/base.js +7 -8
- package/dist/base.js.map +1 -1
- package/dist/encoding.d.ts +8 -0
- package/dist/encoding.d.ts.map +1 -0
- package/dist/encoding.js +40 -0
- package/dist/encoding.js.map +1 -0
- package/dist/stamp.d.ts +2 -2
- package/dist/stamp.d.ts.map +1 -1
- package/dist/stamp.js +104 -40
- package/dist/stamp.js.map +1 -1
- package/dist/tink/bytes.d.ts +45 -0
- package/dist/tink/bytes.d.ts.map +1 -0
- package/dist/tink/bytes.js +82 -0
- package/dist/tink/bytes.js.map +1 -0
- package/dist/tink/elliptic_curves.d.ts +14 -0
- package/dist/tink/elliptic_curves.d.ts.map +1 -0
- package/dist/tink/elliptic_curves.js +178 -0
- package/dist/tink/elliptic_curves.js.map +1 -0
- package/dist/universal.d.ts +8 -0
- package/dist/universal.d.ts.map +1 -0
- package/dist/universal.js +8 -0
- package/dist/universal.js.map +1 -0
- package/package.json +5 -3
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@turnkey/http)
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
A lower-level, fully typed HTTP client for interacting with [Turnkey](https://turnkey.io) API.
|
|
6
|
+
|
|
7
|
+
For signing transactions and messages, check out the higher-level [`@turnkey/ethers`](/packages/ethers/) Signer.
|
|
6
8
|
|
|
7
9
|
API Docs: https://turnkey.readme.io/
|
|
8
10
|
|
package/dist/base.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.stableStringify = exports.request = void 0;
|
|
7
|
-
const
|
|
4
|
+
const universal_1 = require("./universal");
|
|
8
5
|
const stamp_1 = require("./stamp");
|
|
9
6
|
const config_1 = require("./config");
|
|
7
|
+
const encoding_1 = require("./encoding");
|
|
10
8
|
const sharedHeaders = {};
|
|
11
9
|
const sharedRequestOptions = {
|
|
12
10
|
redirect: "follow",
|
|
@@ -20,18 +18,19 @@ async function request(input) {
|
|
|
20
18
|
substitution: inputSubstitution,
|
|
21
19
|
});
|
|
22
20
|
const sealedBody = stableStringify(inputBody);
|
|
23
|
-
const
|
|
21
|
+
const sealedStamp = stableStringify(await (0, stamp_1.stamp)({
|
|
24
22
|
content: sealedBody,
|
|
25
23
|
privateKey: apiPrivateKey,
|
|
26
24
|
publicKey: apiPublicKey,
|
|
27
|
-
}))
|
|
28
|
-
const
|
|
25
|
+
}));
|
|
26
|
+
const xStamp = (0, encoding_1.stringToBase64urlString)(sealedStamp);
|
|
27
|
+
const response = await (0, universal_1.fetch)(url.toString(), {
|
|
29
28
|
...sharedRequestOptions,
|
|
30
29
|
method,
|
|
31
30
|
headers: {
|
|
32
31
|
...sharedHeaders,
|
|
33
32
|
...inputHeaders,
|
|
34
|
-
"X-Stamp":
|
|
33
|
+
"X-Stamp": xStamp,
|
|
35
34
|
},
|
|
36
35
|
body: sealedBody,
|
|
37
36
|
});
|
package/dist/base.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.js","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":";;;AAAA,2CAAoC;AACpC,mCAAgC;AAChC,qCAAqC;AACrC,yCAAqD;AASrD,MAAM,aAAa,GAAkB,EAAE,CAAC;AAExC,MAAM,oBAAoB,GAAyB;IACjD,QAAQ,EAAE,QAAQ;CACnB,CAAC;AAEK,K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AAG,MAAM,CAAC,UAAU,CAAC,IAAI,GAAG,GAAG,EAAE,eAAe,CAAC,GAAG,CAAC,CAAC,CAAC;QACnE,SAAS,CACP,MAAM,KAAK,MAAM,EACjB,oCAAoC,GAAG,aAAa,GAAG,2BAA2B,IAAI,CAAC,SAAS,CAC9F,eAAe,CAChB,EAAE,CACJ,CAAC;QAEF,MAAM,GAAG,MAAM,CAAC;KACjB;IAED,SAAS,CACP,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,EACtB,0DAA0D,MAAM,GAAG,CACpE,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,SAAS,SAAS,CAAC,SAAc,EAAE,OAAe;IAChD,IAAI,CAAC,SAAS,EAAE;QACd,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;KAC1B;AACH,CAAC;AAED,SAAgB,eAAe,CAAC,KAA0B;IACxD,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;AAC/B,CAAC;AAFD,0CAEC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Code modified from https://github.com/github/webauthn-json/blob/e932b3585fa70b0bd5b5a4012ba7dbad7b0a0d0f/src/webauthn-json/base64url.ts#L23
|
|
3
|
+
*/
|
|
4
|
+
export declare function stringToBase64urlString(input: string): string;
|
|
5
|
+
export declare function uint8ArrayToHexString(input: Uint8Array): string;
|
|
6
|
+
export declare function hexStringToUint8Array(input: string): Uint8Array;
|
|
7
|
+
export declare function hexStringToBase64urlString(input: string): string;
|
|
8
|
+
//# sourceMappingURL=encoding.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encoding.d.ts","sourceRoot":"","sources":["../src/encoding.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAY7D;AAED,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAK/D;AAED,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAgB/D;AAED,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAMhE"}
|
package/dist/encoding.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.hexStringToBase64urlString = exports.hexStringToUint8Array = exports.uint8ArrayToHexString = exports.stringToBase64urlString = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Code modified from https://github.com/github/webauthn-json/blob/e932b3585fa70b0bd5b5a4012ba7dbad7b0a0d0f/src/webauthn-json/base64url.ts#L23
|
|
6
|
+
*/
|
|
7
|
+
function stringToBase64urlString(input) {
|
|
8
|
+
// string to base64
|
|
9
|
+
const base64String = btoa(input);
|
|
10
|
+
// base64 to base64url
|
|
11
|
+
// We assume that the base64url string is well-formed.
|
|
12
|
+
const base64urlString = base64String
|
|
13
|
+
.replace(/\+/g, "-")
|
|
14
|
+
.replace(/\//g, "_")
|
|
15
|
+
.replace(/=/g, "");
|
|
16
|
+
return base64urlString;
|
|
17
|
+
}
|
|
18
|
+
exports.stringToBase64urlString = stringToBase64urlString;
|
|
19
|
+
function uint8ArrayToHexString(input) {
|
|
20
|
+
return input.reduce((result, x) => result + x.toString(16).padStart(2, "0"), "");
|
|
21
|
+
}
|
|
22
|
+
exports.uint8ArrayToHexString = uint8ArrayToHexString;
|
|
23
|
+
function hexStringToUint8Array(input) {
|
|
24
|
+
if (input.length === 0 ||
|
|
25
|
+
input.length % 2 !== 0 ||
|
|
26
|
+
/[^a-fA-F0-9]/u.test(input)) {
|
|
27
|
+
throw new Error(`Invalid hex string: ${JSON.stringify(input)}`);
|
|
28
|
+
}
|
|
29
|
+
return Uint8Array.from(input
|
|
30
|
+
.match(/.{2}/g // Split string by every two characters
|
|
31
|
+
)
|
|
32
|
+
.map((byte) => parseInt(byte, 16)));
|
|
33
|
+
}
|
|
34
|
+
exports.hexStringToUint8Array = hexStringToUint8Array;
|
|
35
|
+
function hexStringToBase64urlString(input) {
|
|
36
|
+
const buffer = hexStringToUint8Array(input);
|
|
37
|
+
return stringToBase64urlString(buffer.reduce((result, x) => result + String.fromCharCode(x), ""));
|
|
38
|
+
}
|
|
39
|
+
exports.hexStringToBase64urlString = hexStringToBase64urlString;
|
|
40
|
+
//# sourceMappingURL=encoding.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encoding.js","sourceRoot":"","sources":["../src/encoding.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,SAAgB,uBAAuB,CAAC,KAAa;IACnD,mBAAmB;IACnB,MAAM,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC;IAEjC,sBAAsB;IACtB,sDAAsD;IACtD,MAAM,eAAe,GAAG,YAAY;SACjC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC;SACnB,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC;SACnB,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IAErB,OAAO,eAAe,CAAC;AACzB,CAAC;AAZD,0DAYC;AAED,SAAgB,qBAAqB,CAAC,KAAiB;IACrD,OAAO,KAAK,CAAC,MAAM,CACjB,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,EACvD,EAAE,CACH,CAAC;AACJ,CAAC;AALD,sDAKC;AAED,SAAgB,qBAAqB,CAAC,KAAa;IACjD,IACE,KAAK,CAAC,MAAM,KAAK,CAAC;QAClB,KAAK,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC;QACtB,eAAe,CAAC,IAAI,CAAC,KAAK,CAAC,EAC3B;QACA,MAAM,IAAI,KAAK,CAAC,uBAAuB,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;KACjE;IAED,OAAO,UAAU,CAAC,IAAI,CACpB,KAAK;SACF,KAAK,CACJ,OAAO,CAAC,uCAAuC;KAC/C;SACD,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CACrC,CAAC;AACJ,CAAC;AAhBD,sDAgBC;AAED,SAAgB,0BAA0B,CAAC,KAAa;IACtD,MAAM,MAAM,GAAG,qBAAqB,CAAC,KAAK,CAAC,CAAC;IAE5C,OAAO,uBAAuB,CAC5B,MAAM,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAClE,CAAC;AACJ,CAAC;AAND,gEAMC"}
|
package/dist/stamp.d.ts
CHANGED
package/dist/stamp.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stamp.d.ts","sourceRoot":"","sources":["../src/stamp.ts"],"names":[],"mappings":"AAQA,
|
|
1
|
+
{"version":3,"file":"stamp.d.ts","sourceRoot":"","sources":["../src/stamp.ts"],"names":[],"mappings":"AAQA,wBAAsB,KAAK,CAAC,KAAK,EAAE;IACjC,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;CACpB;;;;GAcA"}
|
package/dist/stamp.js
CHANGED
|
@@ -1,48 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
3
|
exports.stamp = void 0;
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
function stamp(input) {
|
|
4
|
+
const universal_1 = require("./universal");
|
|
5
|
+
const encoding_1 = require("./encoding");
|
|
6
|
+
const elliptic_curves_1 = require("./tink/elliptic_curves");
|
|
7
|
+
async function stamp(input) {
|
|
31
8
|
const { content, publicKey, privateKey } = input;
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
privateKeyBuffer,
|
|
36
|
-
]);
|
|
37
|
-
const privateKeyObject = crypto.createPrivateKey({
|
|
38
|
-
type: "pkcs8",
|
|
39
|
-
format: "der",
|
|
40
|
-
key: privateKeyPkcs8Der,
|
|
9
|
+
const key = await importTurnkeyApiKey({
|
|
10
|
+
uncompressedPrivateKeyHex: privateKey,
|
|
11
|
+
compressedPublicKeyHex: publicKey,
|
|
41
12
|
});
|
|
42
|
-
const
|
|
43
|
-
sign.write(Buffer.from(content));
|
|
44
|
-
sign.end();
|
|
45
|
-
const signature = sign.sign(privateKeyObject, "hex");
|
|
13
|
+
const signature = await signMessage({ key, content });
|
|
46
14
|
return {
|
|
47
15
|
publicKey: publicKey,
|
|
48
16
|
scheme: "SIGNATURE_SCHEME_TK_API_P256",
|
|
@@ -50,4 +18,100 @@ function stamp(input) {
|
|
|
50
18
|
};
|
|
51
19
|
}
|
|
52
20
|
exports.stamp = stamp;
|
|
21
|
+
async function importTurnkeyApiKey(input) {
|
|
22
|
+
const { uncompressedPrivateKeyHex, compressedPublicKeyHex } = input;
|
|
23
|
+
const jwk = convertTurnkeyApiKeyToJwk({
|
|
24
|
+
uncompressedPrivateKeyHex,
|
|
25
|
+
compressedPublicKeyHex,
|
|
26
|
+
});
|
|
27
|
+
return await universal_1.subtle.importKey("jwk", jwk, {
|
|
28
|
+
name: "ECDSA",
|
|
29
|
+
namedCurve: "P-256",
|
|
30
|
+
}, false, // not extractable
|
|
31
|
+
["sign"] // allow signing
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
async function signMessage(input) {
|
|
35
|
+
const { key, content } = input;
|
|
36
|
+
const signatureIeee1363 = await universal_1.subtle.sign({
|
|
37
|
+
name: "ECDSA",
|
|
38
|
+
hash: "SHA-256",
|
|
39
|
+
}, key, new universal_1.TextEncoder().encode(content));
|
|
40
|
+
const signatureDer = convertEcdsaIeee1363ToDer(new Uint8Array(signatureIeee1363));
|
|
41
|
+
return (0, encoding_1.uint8ArrayToHexString)(signatureDer);
|
|
42
|
+
}
|
|
43
|
+
function convertTurnkeyApiKeyToJwk(input) {
|
|
44
|
+
const { uncompressedPrivateKeyHex, compressedPublicKeyHex } = input;
|
|
45
|
+
const jwk = (0, elliptic_curves_1.pointDecode)((0, encoding_1.hexStringToUint8Array)(compressedPublicKeyHex));
|
|
46
|
+
jwk.d = (0, encoding_1.hexStringToBase64urlString)(uncompressedPrivateKeyHex);
|
|
47
|
+
return jwk;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* `SubtleCrypto.sign(...)` outputs signature in IEEE P1363 format:
|
|
51
|
+
* - https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/sign#ecdsa
|
|
52
|
+
*
|
|
53
|
+
* Turnkey expects the signature encoding to be DER-encoded ASN.1:
|
|
54
|
+
* - https://github.com/tkhq/tkcli/blob/7f0159af5a73387ff050647180d1db4d3a3aa033/src/internal/apikey/apikey.go#L149
|
|
55
|
+
*
|
|
56
|
+
* Code modified from https://github.com/google/tink/blob/6f74b99a2bfe6677e3670799116a57268fd067fa/javascript/subtle/elliptic_curves.ts#L114
|
|
57
|
+
*
|
|
58
|
+
* Transform an ECDSA signature in IEEE 1363 encoding to DER encoding.
|
|
59
|
+
*
|
|
60
|
+
* @param ieee the ECDSA signature in IEEE encoding
|
|
61
|
+
* @return ECDSA signature in DER encoding
|
|
62
|
+
*/
|
|
63
|
+
function convertEcdsaIeee1363ToDer(ieee) {
|
|
64
|
+
if (ieee.length % 2 != 0 || ieee.length == 0 || ieee.length > 132) {
|
|
65
|
+
throw new Error("Invalid IEEE P1363 signature encoding. Length: " + ieee.length);
|
|
66
|
+
}
|
|
67
|
+
const r = toUnsignedBigNum(ieee.subarray(0, ieee.length / 2));
|
|
68
|
+
const s = toUnsignedBigNum(ieee.subarray(ieee.length / 2, ieee.length));
|
|
69
|
+
let offset = 0;
|
|
70
|
+
const length = 1 + 1 + r.length + 1 + 1 + s.length;
|
|
71
|
+
let der;
|
|
72
|
+
if (length >= 128) {
|
|
73
|
+
der = new Uint8Array(length + 3);
|
|
74
|
+
der[offset++] = 48;
|
|
75
|
+
der[offset++] = 128 + 1;
|
|
76
|
+
der[offset++] = length;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
der = new Uint8Array(length + 2);
|
|
80
|
+
der[offset++] = 48;
|
|
81
|
+
der[offset++] = length;
|
|
82
|
+
}
|
|
83
|
+
der[offset++] = 2;
|
|
84
|
+
der[offset++] = r.length;
|
|
85
|
+
der.set(r, offset);
|
|
86
|
+
offset += r.length;
|
|
87
|
+
der[offset++] = 2;
|
|
88
|
+
der[offset++] = s.length;
|
|
89
|
+
der.set(s, offset);
|
|
90
|
+
return der;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Code modified from https://github.com/google/tink/blob/6f74b99a2bfe6677e3670799116a57268fd067fa/javascript/subtle/elliptic_curves.ts#L311
|
|
94
|
+
*
|
|
95
|
+
* Transform a big integer in big endian to minimal unsigned form which has
|
|
96
|
+
* no extra zero at the beginning except when the highest bit is set.
|
|
97
|
+
*/
|
|
98
|
+
function toUnsignedBigNum(bytes) {
|
|
99
|
+
// Remove zero prefixes.
|
|
100
|
+
let start = 0;
|
|
101
|
+
while (start < bytes.length && bytes[start] == 0) {
|
|
102
|
+
start++;
|
|
103
|
+
}
|
|
104
|
+
if (start == bytes.length) {
|
|
105
|
+
start = bytes.length - 1;
|
|
106
|
+
}
|
|
107
|
+
let extraZero = 0;
|
|
108
|
+
// If the 1st bit is not zero, add 1 zero byte.
|
|
109
|
+
if ((bytes[start] & 128) == 128) {
|
|
110
|
+
// Add extra zero.
|
|
111
|
+
extraZero = 1;
|
|
112
|
+
}
|
|
113
|
+
const res = new Uint8Array(bytes.length - start + extraZero);
|
|
114
|
+
res.set(bytes.subarray(start), extraZero);
|
|
115
|
+
return res;
|
|
116
|
+
}
|
|
53
117
|
//# sourceMappingURL=stamp.js.map
|
package/dist/stamp.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stamp.js","sourceRoot":"","sources":["../src/stamp.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"stamp.js","sourceRoot":"","sources":["../src/stamp.ts"],"names":[],"mappings":";;;AAAA,2CAAkD;AAClD,yCAIoB;AACpB,4DAAqD;AAE9C,KAAK,UAAU,KAAK,CAAC,KAI3B;IACC,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,KAAK,CAAC;IAEjD,MAAM,GAAG,GAAG,MA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C,EAAE,SAAS,CAAC,CAAC;IAC1C,OAAO,GAAG,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Code modified from https://github.com/google/tink/blob/6f74b99a2bfe6677e3670799116a57268fd067fa/javascript/subtle/bytes.ts
|
|
3
|
+
*
|
|
4
|
+
* @license
|
|
5
|
+
* Copyright 2020 Google LLC
|
|
6
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Converts the hex string to a byte array.
|
|
10
|
+
*
|
|
11
|
+
* @param hex the input
|
|
12
|
+
* @return the byte array output
|
|
13
|
+
* @throws {!Error}
|
|
14
|
+
* @static
|
|
15
|
+
*/
|
|
16
|
+
export declare function fromHex(hex: string): Uint8Array;
|
|
17
|
+
/**
|
|
18
|
+
* Converts a byte array to hex.
|
|
19
|
+
*
|
|
20
|
+
* @param bytes the byte array input
|
|
21
|
+
* @return hex the output
|
|
22
|
+
* @static
|
|
23
|
+
*/
|
|
24
|
+
export declare function toHex(bytes: Uint8Array): string;
|
|
25
|
+
/**
|
|
26
|
+
* Base64 encode a byte array.
|
|
27
|
+
*
|
|
28
|
+
* @param bytes the byte array input
|
|
29
|
+
* @param opt_webSafe True indicates we should use the alternative
|
|
30
|
+
* alphabet, which does not require escaping for use in URLs.
|
|
31
|
+
* @return base64 output
|
|
32
|
+
* @static
|
|
33
|
+
*/
|
|
34
|
+
export declare function toBase64(bytes: Uint8Array, opt_webSafe?: boolean): string;
|
|
35
|
+
/**
|
|
36
|
+
* Turns a byte array into the string given by the concatenation of the
|
|
37
|
+
* characters to which the numbers correspond. Each byte is corresponding to a
|
|
38
|
+
* character. Does not support multi-byte characters.
|
|
39
|
+
*
|
|
40
|
+
* @param bytes Array of numbers representing
|
|
41
|
+
* characters.
|
|
42
|
+
* @return Stringification of the array.
|
|
43
|
+
*/
|
|
44
|
+
export declare function toByteString(bytes: Uint8Array): string;
|
|
45
|
+
//# sourceMappingURL=bytes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bytes.d.ts","sourceRoot":"","sources":["../../src/tink/bytes.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH;;;;;;;GAOG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAS/C;AAED;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAO/C;AAED;;;;;;;;GAQG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,UAAU,EAAE,WAAW,CAAC,EAAE,OAAO,GAAG,MAAM,CASzE;AAED;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAMtD"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Code modified from https://github.com/google/tink/blob/6f74b99a2bfe6677e3670799116a57268fd067fa/javascript/subtle/bytes.ts
|
|
4
|
+
*
|
|
5
|
+
* @license
|
|
6
|
+
* Copyright 2020 Google LLC
|
|
7
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.toByteString = exports.toBase64 = exports.toHex = exports.fromHex = void 0;
|
|
11
|
+
/**
|
|
12
|
+
* Converts the hex string to a byte array.
|
|
13
|
+
*
|
|
14
|
+
* @param hex the input
|
|
15
|
+
* @return the byte array output
|
|
16
|
+
* @throws {!Error}
|
|
17
|
+
* @static
|
|
18
|
+
*/
|
|
19
|
+
function fromHex(hex) {
|
|
20
|
+
if (hex.length % 2 != 0) {
|
|
21
|
+
throw new Error("Hex string length must be multiple of 2");
|
|
22
|
+
}
|
|
23
|
+
const arr = new Uint8Array(hex.length / 2);
|
|
24
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
25
|
+
arr[i / 2] = parseInt(hex.substring(i, i + 2), 16);
|
|
26
|
+
}
|
|
27
|
+
return arr;
|
|
28
|
+
}
|
|
29
|
+
exports.fromHex = fromHex;
|
|
30
|
+
/**
|
|
31
|
+
* Converts a byte array to hex.
|
|
32
|
+
*
|
|
33
|
+
* @param bytes the byte array input
|
|
34
|
+
* @return hex the output
|
|
35
|
+
* @static
|
|
36
|
+
*/
|
|
37
|
+
function toHex(bytes) {
|
|
38
|
+
let result = "";
|
|
39
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
40
|
+
const hexByte = bytes[i].toString(16);
|
|
41
|
+
result += hexByte.length > 1 ? hexByte : "0" + hexByte;
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
exports.toHex = toHex;
|
|
46
|
+
/**
|
|
47
|
+
* Base64 encode a byte array.
|
|
48
|
+
*
|
|
49
|
+
* @param bytes the byte array input
|
|
50
|
+
* @param opt_webSafe True indicates we should use the alternative
|
|
51
|
+
* alphabet, which does not require escaping for use in URLs.
|
|
52
|
+
* @return base64 output
|
|
53
|
+
* @static
|
|
54
|
+
*/
|
|
55
|
+
function toBase64(bytes, opt_webSafe) {
|
|
56
|
+
const encoded = btoa(
|
|
57
|
+
/* padding */
|
|
58
|
+
toByteString(bytes)).replace(/=/g, "");
|
|
59
|
+
if (opt_webSafe) {
|
|
60
|
+
return encoded.replace(/\+/g, "-").replace(/\//g, "_");
|
|
61
|
+
}
|
|
62
|
+
return encoded;
|
|
63
|
+
}
|
|
64
|
+
exports.toBase64 = toBase64;
|
|
65
|
+
/**
|
|
66
|
+
* Turns a byte array into the string given by the concatenation of the
|
|
67
|
+
* characters to which the numbers correspond. Each byte is corresponding to a
|
|
68
|
+
* character. Does not support multi-byte characters.
|
|
69
|
+
*
|
|
70
|
+
* @param bytes Array of numbers representing
|
|
71
|
+
* characters.
|
|
72
|
+
* @return Stringification of the array.
|
|
73
|
+
*/
|
|
74
|
+
function toByteString(bytes) {
|
|
75
|
+
let str = "";
|
|
76
|
+
for (let i = 0; i < bytes.length; i += 1) {
|
|
77
|
+
str += String.fromCharCode(bytes[i]);
|
|
78
|
+
}
|
|
79
|
+
return str;
|
|
80
|
+
}
|
|
81
|
+
exports.toByteString = toByteString;
|
|
82
|
+
//# sourceMappingURL=bytes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bytes.js","sourceRoot":"","sources":["../../src/tink/bytes.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAEH;;;;;;;GAOG;AACH,SAAgB,OAAO,CAAC,GAAW;IACjC,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,EAAE;QACvB,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;KAC5D;IACD,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IAC3C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE;QACtC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;KACpD;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AATD,0BASC;AAED;;;;;;GAMG;AACH,SAAgB,KAAK,CAAC,KAAiB;IACrC,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QACrC,MAAM,OAAO,GAAG,KAAK,CAAC,CAAC,CAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;QACvC,MAAM,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,GAAG,OAAO,CAAC;KACxD;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAPD,sBAOC;AAED;;;;;;;;GAQG;AACH,SAAgB,QAAQ,CAAC,KAAiB,EAAE,WAAqB;IAC/D,MAAM,OAAO,GAAG,IAAI;IAClB,aAAa;IACb,YAAY,CAAC,KAAK,CAAC,CACpB,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IACpB,IAAI,WAAW,EAAE;QACf,OAAO,OAAO,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;KACxD;IACD,OAAO,OAAO,CAAC;AACjB,CAAC;AATD,4BASC;AAED;;;;;;;;GAQG;AACH,SAAgB,YAAY,CAAC,KAAiB;IAC5C,IAAI,GAAG,GAAG,EAAE,CAAC;IACb,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE;QACxC,GAAG,IAAI,MAAM,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,CAAE,CAAC,CAAC;KACvC;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAND,oCAMC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Code modified from https://github.com/google/tink/blob/6f74b99a2bfe6677e3670799116a57268fd067fa/javascript/subtle/elliptic_curves.ts
|
|
3
|
+
*
|
|
4
|
+
* @license
|
|
5
|
+
* Copyright 2020 Google LLC
|
|
6
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Decodes a public key in _compressed_ format.
|
|
10
|
+
*
|
|
11
|
+
* P-256 only
|
|
12
|
+
*/
|
|
13
|
+
export declare function pointDecode(point: Uint8Array): JsonWebKey;
|
|
14
|
+
//# sourceMappingURL=elliptic_curves.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"elliptic_curves.d.ts","sourceRoot":"","sources":["../../src/tink/elliptic_curves.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AA2HH;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,UAAU,GAAG,UAAU,CAwBzD"}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Code modified from https://github.com/google/tink/blob/6f74b99a2bfe6677e3670799116a57268fd067fa/javascript/subtle/elliptic_curves.ts
|
|
4
|
+
*
|
|
5
|
+
* @license
|
|
6
|
+
* Copyright 2020 Google LLC
|
|
7
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
8
|
+
*/
|
|
9
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
12
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
13
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
14
|
+
}
|
|
15
|
+
Object.defineProperty(o, k2, desc);
|
|
16
|
+
}) : (function(o, m, k, k2) {
|
|
17
|
+
if (k2 === undefined) k2 = k;
|
|
18
|
+
o[k2] = m[k];
|
|
19
|
+
}));
|
|
20
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
21
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
22
|
+
}) : function(o, v) {
|
|
23
|
+
o["default"] = v;
|
|
24
|
+
});
|
|
25
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
26
|
+
if (mod && mod.__esModule) return mod;
|
|
27
|
+
var result = {};
|
|
28
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
29
|
+
__setModuleDefault(result, mod);
|
|
30
|
+
return result;
|
|
31
|
+
};
|
|
32
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
33
|
+
exports.pointDecode = void 0;
|
|
34
|
+
const Bytes = __importStar(require("./bytes"));
|
|
35
|
+
/**
|
|
36
|
+
* P-256 only
|
|
37
|
+
*/
|
|
38
|
+
function getModulus() {
|
|
39
|
+
// https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf (Appendix D).
|
|
40
|
+
return BigInt("115792089210356248762697446949407573530086143415290314195533631308" +
|
|
41
|
+
"867097853951");
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* P-256 only
|
|
45
|
+
*/
|
|
46
|
+
function getB() {
|
|
47
|
+
// https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf (Appendix D).
|
|
48
|
+
return BigInt("0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b");
|
|
49
|
+
}
|
|
50
|
+
/** Converts byte array to bigint. */
|
|
51
|
+
function byteArrayToInteger(bytes) {
|
|
52
|
+
return BigInt("0x" + Bytes.toHex(bytes));
|
|
53
|
+
}
|
|
54
|
+
/** Converts bigint to byte array. */
|
|
55
|
+
function integerToByteArray(i) {
|
|
56
|
+
let input = i.toString(16);
|
|
57
|
+
// If necessary, prepend leading zero to ensure that input length is even.
|
|
58
|
+
input = input.length % 2 === 0 ? input : "0" + input;
|
|
59
|
+
return Bytes.fromHex(input);
|
|
60
|
+
}
|
|
61
|
+
/** Returns true iff the ith bit (in lsb order) of n is set. */
|
|
62
|
+
function testBit(n, i) {
|
|
63
|
+
const m = BigInt(1) << BigInt(i);
|
|
64
|
+
return (n & m) !== BigInt(0);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Computes a modular exponent. Since JavaScript BigInt operations are not
|
|
68
|
+
* constant-time, information about the inputs could leak. Therefore, THIS
|
|
69
|
+
* METHOD SHOULD ONLY BE USED FOR POINT DECOMPRESSION.
|
|
70
|
+
*
|
|
71
|
+
* @param b base
|
|
72
|
+
* @param exp exponent
|
|
73
|
+
* @param p modulus
|
|
74
|
+
* @return b^exp modulo p
|
|
75
|
+
*/
|
|
76
|
+
function modPow(b, exp, p) {
|
|
77
|
+
if (exp === BigInt(0)) {
|
|
78
|
+
return BigInt(1);
|
|
79
|
+
}
|
|
80
|
+
let result = b;
|
|
81
|
+
const exponentBitString = exp.toString(2);
|
|
82
|
+
for (let i = 1; i < exponentBitString.length; ++i) {
|
|
83
|
+
result = (result * result) % p;
|
|
84
|
+
if (exponentBitString[i] === "1") {
|
|
85
|
+
result = (result * b) % p;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Computes a square root modulo an odd prime. Since timing and exceptions can
|
|
92
|
+
* leak information about the inputs, THIS METHOD SHOULD ONLY BE USED FOR
|
|
93
|
+
* POINT DECOMPRESSION.
|
|
94
|
+
*
|
|
95
|
+
* @param x square
|
|
96
|
+
* @param p prime modulus
|
|
97
|
+
* @return square root of x modulo p
|
|
98
|
+
*/
|
|
99
|
+
function modSqrt(x, p) {
|
|
100
|
+
if (p <= BigInt(0)) {
|
|
101
|
+
throw new Error("p must be positive");
|
|
102
|
+
}
|
|
103
|
+
const base = x % p;
|
|
104
|
+
// The currently supported NIST curves P-256, P-384, and P-521 all satisfy
|
|
105
|
+
// p % 4 == 3. However, although currently a no-op, the following check
|
|
106
|
+
// should be left in place in case other curves are supported in the future.
|
|
107
|
+
if (testBit(p, 0) && /* istanbul ignore next */ testBit(p, 1)) {
|
|
108
|
+
// Case p % 4 == 3 (applies to NIST curves P-256, P-384, and P-521)
|
|
109
|
+
// q = (p + 1) / 4
|
|
110
|
+
const q = (p + BigInt(1)) >> BigInt(2);
|
|
111
|
+
const squareRoot = modPow(base, q, p);
|
|
112
|
+
if ((squareRoot * squareRoot) % p !== base) {
|
|
113
|
+
throw new Error("could not find a modular square root");
|
|
114
|
+
}
|
|
115
|
+
return squareRoot;
|
|
116
|
+
}
|
|
117
|
+
// Skipping other elliptic curve types that require Cipolla's algorithm.
|
|
118
|
+
throw new Error("unsupported modulus value");
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Computes the y-coordinate of a point on an elliptic curve given its
|
|
122
|
+
* x-coordinate. Since timing and exceptions can leak information about the
|
|
123
|
+
* inputs, THIS METHOD SHOULD ONLY BE USED FOR POINT DECOMPRESSION.
|
|
124
|
+
*
|
|
125
|
+
* P-256 only
|
|
126
|
+
*
|
|
127
|
+
* @param x x-coordinate
|
|
128
|
+
* @param lsb least significant bit of the y-coordinate
|
|
129
|
+
* @return y-coordinate
|
|
130
|
+
*/
|
|
131
|
+
function getY(x, lsb) {
|
|
132
|
+
const p = getModulus();
|
|
133
|
+
const a = p - BigInt(3);
|
|
134
|
+
const b = getB();
|
|
135
|
+
const rhs = ((x * x + a) * x + b) % p;
|
|
136
|
+
let y = modSqrt(rhs, p);
|
|
137
|
+
if (lsb !== testBit(y, 0)) {
|
|
138
|
+
y = (p - y) % p;
|
|
139
|
+
}
|
|
140
|
+
return y;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Decodes a public key in _compressed_ format.
|
|
144
|
+
*
|
|
145
|
+
* P-256 only
|
|
146
|
+
*/
|
|
147
|
+
function pointDecode(point) {
|
|
148
|
+
const fieldSize = fieldSizeInBytes();
|
|
149
|
+
if (point.length !== 1 + fieldSize) {
|
|
150
|
+
throw new Error("compressed point has wrong length");
|
|
151
|
+
}
|
|
152
|
+
if (point[0] !== 2 && point[0] !== 3) {
|
|
153
|
+
throw new Error("invalid format");
|
|
154
|
+
}
|
|
155
|
+
const lsb = point[0] === 3; // point[0] must be 2 (false) or 3 (true).
|
|
156
|
+
const x = byteArrayToInteger(point.subarray(1, point.length));
|
|
157
|
+
const p = getModulus();
|
|
158
|
+
if (x < BigInt(0) || x >= p) {
|
|
159
|
+
throw new Error("x is out of range");
|
|
160
|
+
}
|
|
161
|
+
const y = getY(x, lsb);
|
|
162
|
+
const result = {
|
|
163
|
+
kty: "EC",
|
|
164
|
+
crv: "P-256",
|
|
165
|
+
x: Bytes.toBase64(integerToByteArray(x), /* websafe */ true),
|
|
166
|
+
y: Bytes.toBase64(integerToByteArray(y), /* websafe */ true),
|
|
167
|
+
ext: true,
|
|
168
|
+
};
|
|
169
|
+
return result;
|
|
170
|
+
}
|
|
171
|
+
exports.pointDecode = pointDecode;
|
|
172
|
+
/**
|
|
173
|
+
* P-256 only
|
|
174
|
+
*/
|
|
175
|
+
function fieldSizeInBytes() {
|
|
176
|
+
return 32;
|
|
177
|
+
}
|
|
178
|
+
//# sourceMappingURL=elliptic_curves.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"elliptic_curves.js","sourceRoot":"","sources":["../../src/tink/elliptic_curves.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,+CAAiC;AAEjC;;GAEG;AACH,SAAS,UAAU;IACjB,2EAA2E;IAC3E,OAAO,MAAM,CACX,oEAAoE;QAClE,cAAc,CACjB,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,SAAS,IAAI;IACX,2EAA2E;IAC3E,OAAO,MAAM,CACX,oEAAoE,CACrE,CAAC;AACJ,CAAC;AAED,qCAAqC;AACrC,SAAS,kBAAkB,CAAC,KAAiB;IAC3C,OAAO,MAAM,CAAC,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC;AAC3C,CAAC;AAED,qCAAqC;AACrC,SAAS,kBAAkB,CAAC,CAAS;IACnC,IAAI,KAAK,GAAG,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IAC3B,0EAA0E;IAC1E,KAAK,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,GAAG,KAAK,CAAC;IAC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;;CACgC,CAAC;AAEzD,eAAO,MAAM,KAAK,yBAA+C,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/// <reference lib="dom" />
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.fetch = exports.TextEncoder = exports.subtle = void 0;
|
|
5
|
+
exports.subtle = globalThis?.crypto?.subtle ?? require("crypto").webcrypto.subtle;
|
|
6
|
+
exports.TextEncoder = globalThis?.TextEncoder ?? require("util").TextEncoder;
|
|
7
|
+
exports.fetch = globalThis?.fetch ?? require("undici").fetch;
|
|
8
|
+
//# sourceMappingURL=universal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"universal.js","sourceRoot":"","sources":["../src/universal.ts"],"names":[],"mappings":";AAAA,2BAA2B;;;AAEd,QAAA,MAAM,GACjB,UAAU,EAAE,MAAM,EAAE,MAAM,IAAI,OAAO,CAAC,QAAQ,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC;AAEtD,QAAA,WAAW,GACtB,UAAU,EAAE,WAAW,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC,WAAW,CAAC;AAE5C,QAAA,KAAK,GAAG,UAAU,EAAE,KAAK,IAAI,OAAO,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turnkey/http",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0-beta.0",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -29,8 +29,10 @@
|
|
|
29
29
|
"access": "public"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"
|
|
33
|
-
|
|
32
|
+
"undici": "^5.21.2"
|
|
33
|
+
},
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=16.0.0"
|
|
34
36
|
},
|
|
35
37
|
"scripts": {
|
|
36
38
|
"build": "tsc",
|