-
Notifications
You must be signed in to change notification settings - Fork 78
add methods and struct for e2ee #788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mikelodder7
wants to merge
7
commits into
feat/rc-naga-2025-04-07
Choose a base branch
from
ml/e2ee
base: feat/rc-naga-2025-04-07
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+248
−40
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4a43aa3
add methods and struct for e2ee
mikelodder7 547c1be
feat(e2ee): make use of internal nacl package and add tests, and remo…
Ansonhkg 0f40949
Merge pull request #815 from LIT-Protocol/anson/propose-change-for-788
Ansonhkg c0d91e7
Merge branch 'feat/rc-naga-2025-04-07' into ml/e2ee
Ansonhkg 6228dc7
Merge branch 'ml/e2ee' of https://github.com/LIT-Protocol/js-sdk into…
Ansonhkg 2afa74c
fmt
Ansonhkg 122dd16
e2ee
mikelodder7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -17,6 +17,7 @@ import { | |
SessionKeyPair, | ||
SigningAccessControlConditionJWTPayload, | ||
SigShare, | ||
WalletEncryptedPayload, | ||
} from '@lit-protocol/types'; | ||
import { | ||
uint8arrayFromString, | ||
|
@@ -372,6 +373,103 @@ async function getAmdCert(url: string): Promise<Uint8Array> { | |
} | ||
} | ||
|
||
export const walletEncrypt = async( | ||
myWalletSecretKey: Uint8Array, | ||
theirWalletPublicKey: Uint8Array, | ||
sessionSig: Uint8Array, | ||
message: Uint8Array | ||
): Promise<WalletEncryptedPayload> => { | ||
const random = new Uint8Array(16); | ||
window.crypto.getRandomValues(random); | ||
const dateNow = Date.now(); | ||
const createdAt = Math.floor(dateNow / 1000); | ||
const timestamp = Buffer.alloc(8); | ||
timestamp.writeBigUInt64BE(BigInt(createdAt), 0); | ||
|
||
const myWalletPublicKey = new Uint8Array(32); | ||
nacl.crypto_scalarmult_base(myWalletPublicKey, myWalletSecretKey); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: if we can use @noble curves and hashes way better so we move towards lib unification (and drop tweetnacl) |
||
|
||
// Construct AAD | ||
const sessionSignature = Buffer.from(sessionSig); // Replace with actual session signature | ||
Ansonhkg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const theirPublicKey = Buffer.from(theirWalletPublicKey); // Replace with their public key | ||
const myPublicKey = Buffer.from(myWalletPublicKey); // Replace with your wallet public key | ||
|
||
const aad = Buffer.concat([ | ||
sessionSignature, | ||
random, | ||
timestamp, | ||
theirPublicKey, | ||
myPublicKey, | ||
]); | ||
|
||
const hash = new Uint8Array(64); | ||
nacl.crypto_hash(hash, aad); | ||
|
||
const nonce = hash.slice(0, 24); | ||
const ciphertext = nacl.box(message, nonce, theirPublicKey, myWalletSecretKey); | ||
return { | ||
V1: { | ||
verification_key: uint8ArrayToHex(myWalletPublicKey), | ||
ciphertext_and_tag: uint8ArrayToHex(ciphertext), | ||
session_signature: uint8ArrayToHex(sessionSignature), | ||
random: uint8ArrayToHex(random), | ||
created_at: dateNow.toISOString(), | ||
} | ||
}; | ||
} | ||
|
||
export const walletDecrypt = async( | ||
myWalletSecretKey: Uint8Array, | ||
payload: WalletEncryptedPayload | ||
): Promise<Uint8Array> => { | ||
const dateSent = new Date(payload.V1.created_at) | ||
const createdAt = Math.floor(dateSent / 1000); | ||
const timestamp = Buffer.alloc(8); | ||
timestamp.writeBigUInt64BE(BigInt(createdAt), 0); | ||
|
||
const myWalletPublicKey = new Uint8Array(32); | ||
nacl.crypto_scalarmult_base(myWalletPublicKey, myWalletSecretKey); | ||
|
||
// Construct AAD | ||
const random = Buffer.from(hexToUint8Array(payload.V1.random)); | ||
const sessionSignature = Buffer.from(hexToUint8Array(payload.V1.session_signature)); // Replace with actual session signature | ||
const theirPublicKey = hexToUint8Array(payload.V1.verification_key); | ||
const theirPublicKeyBuffer = Buffer.from(theirPublicKey); // Replace with their public key | ||
const myPublicKey = Buffer.from(myWalletPublicKey); // Replace with your wallet public key | ||
|
||
const aad = Buffer.concat([ | ||
sessionSignature, | ||
random, | ||
timestamp, | ||
theirPublicKeyBuffer, | ||
myPublicKey, | ||
]); | ||
|
||
const hash = new Uint8Array(64); | ||
nacl.crypto_hash(hash, aad); | ||
|
||
const nonce = hash.slice(0, 24); | ||
const message = nacl.box.open(payload.V1.ciphertext_and_tag, nonce, theirPublicKey, myWalletSecretKey); | ||
return message; | ||
} | ||
|
||
function uint8ArrayToHex(array: Uint8Array) { | ||
return Array.from(array) | ||
.map(byte => byte.toString(16).padStart(2, '0')) | ||
.join(''); | ||
} | ||
|
||
function hexToUint8Array(hexString: string): Uint8Array { | ||
if (hexString.length % 2 !== 0) { | ||
throw new Error("Hex string must have an even length"); | ||
} | ||
const bytes = new Uint8Array(hexString.length / 2); | ||
for (let i = 0; i < bytes.length; i++) { | ||
bytes[i] = parseInt(hexString.slice(i * 2, i * 2 + 2), 16); | ||
} | ||
return bytes; | ||
} | ||
|
||
/** | ||
* | ||
* Check the attestation against AMD certs | ||
|
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
window
won't work on node side, neither deno if we want to put the SDK in LAs. However the method exists on nodes and denos crypto module