Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@turnkey/http

Package Overview
Dependencies
Maintainers
2
Versions
65
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@turnkey/http - npm Package Compare versions

Comparing version 0.7.1-beta.0 to 0.8.0-beta.0

dist/encoding.d.ts

7

CHANGELOG.md
# @turnkey/http
## 0.8.0
### Minor Changes
- Added browser runtime support — `@turnkey/http` is now a universal (isomorphic) package
- Dropped support for Node.js v14; we recommend using Node v18+
## 0.7.0

@@ -4,0 +11,0 @@

15

dist/base.js
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.stableStringify = exports.request = void 0;
const node_fetch_1 = __importDefault(require("node-fetch"));
const universal_1 = require("./universal");
const stamp_1 = require("./stamp");
const config_1 = require("./config");
const encoding_1 = require("./encoding");
const sharedHeaders = {};

@@ -23,8 +21,9 @@ const sharedRequestOptions = {

const sealedBody = stableStringify(inputBody);
const jsonStamp = Buffer.from(stableStringify((0, stamp_1.stamp)({
const sealedStamp = stableStringify(await (0, stamp_1.stamp)({
content: sealedBody,
privateKey: apiPrivateKey,
publicKey: apiPublicKey,
})));
const response = await (0, node_fetch_1.default)(url.toString(), {
}));
const xStamp = (0, encoding_1.stringToBase64urlString)(sealedStamp);
const response = await (0, universal_1.fetch)(url.toString(), {
...sharedRequestOptions,

@@ -35,3 +34,3 @@ method,

...inputHeaders,
"X-Stamp": jsonStamp.toString("base64url"),
"X-Stamp": xStamp,
},

@@ -38,0 +37,0 @@ body: sealedBody,

@@ -5,7 +5,7 @@ export declare function stamp(input: {

privateKey: string;
}): {
}): Promise<{
publicKey: string;
scheme: string;
signature: string;
};
}>;
//# sourceMappingURL=stamp.d.ts.map
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.stamp = void 0;
const crypto = __importStar(require("crypto"));
// Specific byte-sequence for curve prime256v1 (DER encoding)
const PRIVATE_KEY_PREFIX = Buffer.from("308141020100301306072a8648ce3d020106082a8648ce3d030107042730250201010420", "hex");
function stamp(input) {
const universal_1 = require("./universal");
const encoding_1 = require("./encoding");
const elliptic_curves_1 = require("./tink/elliptic_curves");
async function stamp(input) {
const { content, publicKey, privateKey } = input;
const privateKeyBuffer = Buffer.from(privateKey, "hex");
const privateKeyPkcs8Der = Buffer.concat([
PRIVATE_KEY_PREFIX,
privateKeyBuffer,
]);
const privateKeyObject = crypto.createPrivateKey({
type: "pkcs8",
format: "der",
key: privateKeyPkcs8Der,
const key = await importTurnkeyApiKey({
uncompressedPrivateKeyHex: privateKey,
compressedPublicKeyHex: publicKey,
});
const sign = crypto.createSign("SHA256");
sign.write(Buffer.from(content));
sign.end();
const signature = sign.sign(privateKeyObject, "hex");
const signature = await signMessage({ key, content });
return {

@@ -53,2 +21,98 @@ publicKey: publicKey,

exports.stamp = stamp;
async function importTurnkeyApiKey(input) {
const { uncompressedPrivateKeyHex, compressedPublicKeyHex } = input;
const jwk = convertTurnkeyApiKeyToJwk({
uncompressedPrivateKeyHex,
compressedPublicKeyHex,
});
return await universal_1.subtle.importKey("jwk", jwk, {
name: "ECDSA",
namedCurve: "P-256",
}, false, // not extractable
["sign"] // allow signing
);
}
async function signMessage(input) {
const { key, content } = input;
const signatureIeee1363 = await universal_1.subtle.sign({
name: "ECDSA",
hash: "SHA-256",
}, key, new universal_1.TextEncoder().encode(content));
const signatureDer = convertEcdsaIeee1363ToDer(new Uint8Array(signatureIeee1363));
return (0, encoding_1.uint8ArrayToHexString)(signatureDer);
}
function convertTurnkeyApiKeyToJwk(input) {
const { uncompressedPrivateKeyHex, compressedPublicKeyHex } = input;
const jwk = (0, elliptic_curves_1.pointDecode)((0, encoding_1.hexStringToUint8Array)(compressedPublicKeyHex));
jwk.d = (0, encoding_1.hexStringToBase64urlString)(uncompressedPrivateKeyHex);
return jwk;
}
/**
* `SubtleCrypto.sign(...)` outputs signature in IEEE P1363 format:
* - https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/sign#ecdsa
*
* Turnkey expects the signature encoding to be DER-encoded ASN.1:
* - https://github.com/tkhq/tkcli/blob/7f0159af5a73387ff050647180d1db4d3a3aa033/src/internal/apikey/apikey.go#L149
*
* Code modified from https://github.com/google/tink/blob/6f74b99a2bfe6677e3670799116a57268fd067fa/javascript/subtle/elliptic_curves.ts#L114
*
* Transform an ECDSA signature in IEEE 1363 encoding to DER encoding.
*
* @param ieee the ECDSA signature in IEEE encoding
* @return ECDSA signature in DER encoding
*/
function convertEcdsaIeee1363ToDer(ieee) {
if (ieee.length % 2 != 0 || ieee.length == 0 || ieee.length > 132) {
throw new Error("Invalid IEEE P1363 signature encoding. Length: " + ieee.length);
}
const r = toUnsignedBigNum(ieee.subarray(0, ieee.length / 2));
const s = toUnsignedBigNum(ieee.subarray(ieee.length / 2, ieee.length));
let offset = 0;
const length = 1 + 1 + r.length + 1 + 1 + s.length;
let der;
if (length >= 128) {
der = new Uint8Array(length + 3);
der[offset++] = 48;
der[offset++] = 128 + 1;
der[offset++] = length;
}
else {
der = new Uint8Array(length + 2);
der[offset++] = 48;
der[offset++] = length;
}
der[offset++] = 2;
der[offset++] = r.length;
der.set(r, offset);
offset += r.length;
der[offset++] = 2;
der[offset++] = s.length;
der.set(s, offset);
return der;
}
/**
* Code modified from https://github.com/google/tink/blob/6f74b99a2bfe6677e3670799116a57268fd067fa/javascript/subtle/elliptic_curves.ts#L311
*
* Transform a big integer in big endian to minimal unsigned form which has
* no extra zero at the beginning except when the highest bit is set.
*/
function toUnsignedBigNum(bytes) {
// Remove zero prefixes.
let start = 0;
while (start < bytes.length && bytes[start] == 0) {
start++;
}
if (start == bytes.length) {
start = bytes.length - 1;
}
let extraZero = 0;
// If the 1st bit is not zero, add 1 zero byte.
if ((bytes[start] & 128) == 128) {
// Add extra zero.
extraZero = 1;
}
const res = new Uint8Array(bytes.length - start + extraZero);
res.set(bytes.subarray(start), extraZero);
return res;
}
//# sourceMappingURL=stamp.js.map
{
"name": "@turnkey/http",
"version": "0.7.1-beta.0",
"version": "0.8.0-beta.0",
"main": "./dist/index.js",

@@ -32,5 +32,7 @@ "types": "./dist/index.d.ts",

"dependencies": {
"@types/node-fetch": "^2.6.2",
"node-fetch": "^2.6.8"
"undici": "^5.21.2"
},
"engines": {
"node": ">=16.0.0"
},
"scripts": {

@@ -37,0 +39,0 @@ "build": "tsc",

@@ -5,4 +5,6 @@ # @turnkey/http

Typed HTTP client for interacting with [Turnkey](https://turnkey.io) API.
A lower-level, fully typed HTTP client for interacting with [Turnkey](https://turnkey.io) API.
For signing transactions and messages, check out the higher-level [`@turnkey/ethers`](/packages/ethers/) Signer.
API Docs: https://turnkey.readme.io/

@@ -9,0 +11,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc