discord-interactions
Advanced tools
Comparing version 4.0.0-alpha.1 to 4.0.0-alpha.2
@@ -115,5 +115,7 @@ "use strict"; | ||
try { | ||
const encoder = new TextEncoder(); | ||
const timestampData = (0, util_1.valueToUint8Array)(timestamp); | ||
const bodyData = (0, util_1.valueToUint8Array)(rawBody); | ||
const message = (0, util_1.concatUint8Arrays)(timestampData, bodyData); | ||
const publicKey = typeof clientPublicKey === 'string' | ||
? yield util_1.subtleCrypto.importKey('raw', (0, util_1.base64ToArrayBuffer)(clientPublicKey), { | ||
? yield util_1.subtleCrypto.importKey('raw', (0, util_1.valueToUint8Array)(clientPublicKey, 'hex'), { | ||
name: 'ed25519', | ||
@@ -123,11 +125,10 @@ namedCurve: 'ed25519', | ||
: clientPublicKey; | ||
const body = typeof rawBody === 'string' | ||
? rawBody | ||
: Buffer.from(rawBody).toString('utf-8'); | ||
const isValid = yield util_1.subtleCrypto.verify({ | ||
name: 'ed25519', | ||
}, publicKey, (0, util_1.base64ToArrayBuffer)(signature), encoder.encode(timestamp + body)); | ||
}, publicKey, (0, util_1.valueToUint8Array)(signature, 'hex'), message); | ||
console.log(`isValid: ${isValid}`); | ||
return isValid; | ||
} | ||
catch (ex) { | ||
console.error(ex); | ||
return false; | ||
@@ -134,0 +135,0 @@ } |
@@ -0,13 +1,18 @@ | ||
/// <reference types="node" /> | ||
export declare const subtleCrypto: SubtleCrypto; | ||
/** | ||
* Convert a base64 encoded string to an ArrayBuffer. | ||
* @param base64 base64 encoded string | ||
* @returns | ||
* Converts different types to Uint8Array. | ||
* | ||
* @param value - Value to convert. Strings are parsed as hex. | ||
* @param format - Format of value. Valid options: 'hex'. Defaults to utf-8. | ||
* @returns Value in Uint8Array form. | ||
*/ | ||
export declare function base64ToArrayBuffer(base64: string): ArrayBufferLike; | ||
export declare function valueToUint8Array(value: Uint8Array | ArrayBuffer | Buffer | string, format?: string): Uint8Array; | ||
/** | ||
* Converts an array buffer to a base64 encoded string | ||
* @param buffer The data in arrayBuffer format | ||
* @returns a base 64 encoded string | ||
* Merge two arrays. | ||
* | ||
* @param arr1 - First array | ||
* @param arr2 - Second array | ||
* @returns Concatenated arrays | ||
*/ | ||
export declare function arrayBufferToBase64(buffer: ArrayBufferLike): string; | ||
export declare function concatUint8Arrays(arr1: Uint8Array, arr2: Uint8Array): Uint8Array; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.arrayBufferToBase64 = exports.base64ToArrayBuffer = exports.subtleCrypto = void 0; | ||
exports.concatUint8Arrays = exports.valueToUint8Array = exports.subtleCrypto = void 0; | ||
/** | ||
@@ -34,31 +34,54 @@ * Based on environment, get a reference to the Web Crypto API's SubtleCrypto interface. | ||
/** | ||
* Convert a base64 encoded string to an ArrayBuffer. | ||
* @param base64 base64 encoded string | ||
* @returns | ||
* Converts different types to Uint8Array. | ||
* | ||
* @param value - Value to convert. Strings are parsed as hex. | ||
* @param format - Format of value. Valid options: 'hex'. Defaults to utf-8. | ||
* @returns Value in Uint8Array form. | ||
*/ | ||
function base64ToArrayBuffer(base64) { | ||
const binaryString = atob(base64); | ||
const len = binaryString.length; | ||
const bytes = new Uint8Array(len); | ||
for (let i = 0; i < len; i++) { | ||
bytes[i] = binaryString.charCodeAt(i); | ||
function valueToUint8Array(value, format) { | ||
if (value == null) { | ||
return new Uint8Array(); | ||
} | ||
return bytes.buffer; | ||
if (typeof value === 'string') { | ||
if (format === 'hex') { | ||
const matches = value.match(/.{1,2}/g); | ||
if (matches == null) { | ||
throw new Error('Value is not a valid hex string'); | ||
} | ||
const hexVal = matches.map((byte) => Number.parseInt(byte, 16)); | ||
return new Uint8Array(hexVal); | ||
} | ||
return new TextEncoder().encode(value); | ||
} | ||
try { | ||
if (Buffer.isBuffer(value)) { | ||
return new Uint8Array(value); | ||
} | ||
} | ||
catch (ex) { | ||
// Runtime doesn't have Buffer | ||
} | ||
if (value instanceof ArrayBuffer) { | ||
return new Uint8Array(value); | ||
} | ||
if (value instanceof Uint8Array) { | ||
return value; | ||
} | ||
throw new Error('Unrecognized value type, must be one of: string, Buffer, ArrayBuffer, Uint8Array'); | ||
} | ||
exports.base64ToArrayBuffer = base64ToArrayBuffer; | ||
exports.valueToUint8Array = valueToUint8Array; | ||
/** | ||
* Converts an array buffer to a base64 encoded string | ||
* @param buffer The data in arrayBuffer format | ||
* @returns a base 64 encoded string | ||
* Merge two arrays. | ||
* | ||
* @param arr1 - First array | ||
* @param arr2 - Second array | ||
* @returns Concatenated arrays | ||
*/ | ||
function arrayBufferToBase64(buffer) { | ||
let binary = ''; | ||
const bytes = new Uint8Array(buffer); | ||
const len = bytes.byteLength; | ||
for (let i = 0; i < len; i++) { | ||
binary += String.fromCharCode(bytes[i]); | ||
} | ||
return btoa(binary); | ||
function concatUint8Arrays(arr1, arr2) { | ||
const merged = new Uint8Array(arr1.length + arr2.length); | ||
merged.set(arr1); | ||
merged.set(arr2, arr1.length); | ||
return merged; | ||
} | ||
exports.arrayBufferToBase64 = arrayBufferToBase64; | ||
exports.concatUint8Arrays = concatUint8Arrays; | ||
//# sourceMappingURL=util.js.map |
{ | ||
"name": "discord-interactions", | ||
"version": "4.0.0-alpha.1", | ||
"version": "4.0.0-alpha.2", | ||
"description": "Helpers for discord interactions", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36376
556