-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: export crypto primitives (#1728)
* export crypto primitives * export crypto * update imports * fix size limit * rename crypto.js * move Signature type * fix path * fix: size-limit (#1734) * fix paths, revert change to config --------- Co-authored-by: Danish Arora <[email protected]>
- Loading branch information
1 parent
7df21b7
commit 7eb3375
Showing
16 changed files
with
158 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,3 @@ | ||
import nodeCrypto from "crypto"; | ||
|
||
import * as secp from "@noble/secp256k1"; | ||
import { concat } from "@waku/utils/bytes"; | ||
import sha3 from "js-sha3"; | ||
|
||
import { Asymmetric, Symmetric } from "../constants.js"; | ||
|
||
declare const self: Record<string, any> | undefined; | ||
const crypto: { node?: any; web?: any } = { | ||
node: nodeCrypto, | ||
web: typeof self === "object" && "crypto" in self ? self.crypto : undefined | ||
}; | ||
|
||
export function getSubtle(): SubtleCrypto { | ||
if (crypto.web) { | ||
return crypto.web.subtle; | ||
} else if (crypto.node) { | ||
return crypto.node.webcrypto.subtle; | ||
} else { | ||
throw new Error( | ||
"The environment doesn't have Crypto Subtle API (if in the browser, be sure to use to be in a secure context, ie, https)" | ||
); | ||
} | ||
} | ||
|
||
export const randomBytes = secp.utils.randomBytes; | ||
export const sha256 = secp.utils.sha256; | ||
|
||
/** | ||
* Generate a new private key to be used for asymmetric encryption. | ||
* | ||
* Use {@link getPublicKey} to get the corresponding Public Key. | ||
*/ | ||
export function generatePrivateKey(): Uint8Array { | ||
return randomBytes(Asymmetric.keySize); | ||
} | ||
|
||
/** | ||
* Generate a new symmetric key to be used for symmetric encryption. | ||
*/ | ||
export function generateSymmetricKey(): Uint8Array { | ||
return randomBytes(Symmetric.keySize); | ||
} | ||
|
||
/** | ||
* Return the public key for the given private key, to be used for asymmetric | ||
* encryption. | ||
*/ | ||
export const getPublicKey = secp.getPublicKey; | ||
|
||
/** | ||
* ECDSA Sign a message with the given private key. | ||
* | ||
* @param message The message to sign, usually a hash. | ||
* @param privateKey The ECDSA private key to use to sign the message. | ||
* | ||
* @returns The signature and the recovery id concatenated. | ||
*/ | ||
export async function sign( | ||
message: Uint8Array, | ||
privateKey: Uint8Array | ||
): Promise<Uint8Array> { | ||
const [signature, recoveryId] = await secp.sign(message, privateKey, { | ||
recovered: true, | ||
der: false | ||
}); | ||
return concat( | ||
[signature, new Uint8Array([recoveryId])], | ||
signature.length + 1 | ||
); | ||
} | ||
|
||
export function keccak256(input: Uint8Array): Uint8Array { | ||
return new Uint8Array(sha3.keccak256.arrayBuffer(input)); | ||
} | ||
export * from "./utils.js"; | ||
export * as ecies from "./ecies.js"; | ||
export * as symmetric from "./symmetric.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import nodeCrypto from "crypto"; | ||
|
||
import * as secp from "@noble/secp256k1"; | ||
import { concat } from "@waku/utils/bytes"; | ||
import sha3 from "js-sha3"; | ||
|
||
import { Asymmetric, Symmetric } from "../misc.js"; | ||
|
||
declare const self: Record<string, any> | undefined; | ||
Check warning on line 9 in packages/message-encryption/src/crypto/utils.ts GitHub Actions / check
|
||
const crypto: { node?: any; web?: any } = { | ||
Check warning on line 10 in packages/message-encryption/src/crypto/utils.ts GitHub Actions / check
Check warning on line 10 in packages/message-encryption/src/crypto/utils.ts GitHub Actions / check
Check warning on line 10 in packages/message-encryption/src/crypto/utils.ts GitHub Actions / proto
|
||
node: nodeCrypto, | ||
web: typeof self === "object" && "crypto" in self ? self.crypto : undefined | ||
}; | ||
|
||
export function getSubtle(): SubtleCrypto { | ||
if (crypto.web) { | ||
return crypto.web.subtle; | ||
} else if (crypto.node) { | ||
return crypto.node.webcrypto.subtle; | ||
} else { | ||
throw new Error( | ||
"The environment doesn't have Crypto Subtle API (if in the browser, be sure to use to be in a secure context, ie, https)" | ||
); | ||
} | ||
} | ||
|
||
export const randomBytes = secp.utils.randomBytes; | ||
export const sha256 = secp.utils.sha256; | ||
|
||
/** | ||
* Generate a new private key to be used for asymmetric encryption. | ||
* | ||
* Use {@link getPublicKey} to get the corresponding Public Key. | ||
*/ | ||
export function generatePrivateKey(): Uint8Array { | ||
return randomBytes(Asymmetric.keySize); | ||
} | ||
|
||
/** | ||
* Generate a new symmetric key to be used for symmetric encryption. | ||
*/ | ||
export function generateSymmetricKey(): Uint8Array { | ||
return randomBytes(Symmetric.keySize); | ||
} | ||
|
||
/** | ||
* Return the public key for the given private key, to be used for asymmetric | ||
* encryption. | ||
*/ | ||
export const getPublicKey = secp.getPublicKey; | ||
|
||
/** | ||
* ECDSA Sign a message with the given private key. | ||
* | ||
* @param message The message to sign, usually a hash. | ||
* @param privateKey The ECDSA private key to use to sign the message. | ||
* | ||
* @returns The signature and the recovery id concatenated. | ||
*/ | ||
export async function sign( | ||
message: Uint8Array, | ||
privateKey: Uint8Array | ||
): Promise<Uint8Array> { | ||
const [signature, recoveryId] = await secp.sign(message, privateKey, { | ||
recovered: true, | ||
der: false | ||
}); | ||
return concat( | ||
[signature, new Uint8Array([recoveryId])], | ||
signature.length + 1 | ||
); | ||
} | ||
|
||
export function keccak256(input: Uint8Array): Uint8Array { | ||
return new Uint8Array(sha3.keccak256.arrayBuffer(input)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.