Skip to content

Commit

Permalink
Access ephemeral key in cipher instance
Browse files Browse the repository at this point in the history
  • Loading branch information
buberdds committed Jan 17, 2025
1 parent 58341fb commit 744fc57
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
3 changes: 3 additions & 0 deletions clients/js/src/cipher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ function formatFailure(fail: CallFailure): string {
export abstract class Cipher {
public abstract kind: CipherKind;
public abstract publicKey: Uint8Array;
public abstract ephemeralKey: Uint8Array;
public abstract epoch?: number;

public abstract encrypt(
Expand Down Expand Up @@ -213,6 +214,7 @@ export abstract class Cipher {
export class X25519DeoxysII extends Cipher {
public override readonly kind = CipherKind.X25519DeoxysII;
public override readonly publicKey: Uint8Array;
public override readonly ephemeralKey: Uint8Array;
public override readonly epoch: number | undefined;

private cipher: deoxysii.AEAD;
Expand Down Expand Up @@ -243,6 +245,7 @@ export class X25519DeoxysII extends Cipher {
super();

this.publicKey = keypair.publicKey;
this.ephemeralKey = keypair.secretKey;
this.epoch = epoch;

// Derive a shared secret using X25519 (followed by hashing to remove ECDH bias).
Expand Down
5 changes: 2 additions & 3 deletions clients/js/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ export async function notifySapphireSnap(
options: SapphireWrapOptions,
provider: EIP2696_EthereumProvider,
) {
const secretKey = (cipher as any).secretKey as Uint8Array | undefined;
if (secretKey) {
if (cipher.ephemeralKey) {
const peerPublicKey = await options.fetcher.fetch(provider);
await provider.request({
method: 'wallet_invokeSnap',
Expand All @@ -175,7 +174,7 @@ export async function notifySapphireSnap(
method: 'setTransactionDecryptKeys',
params: {
id: transactionData,
ephemeralSecretKey: hexlify(secretKey),
ephemeralSecretKey: hexlify(cipher.ephemeralKey),
peerPublicKey: hexlify(peerPublicKey.key),
peerPublicKeyEpoch: peerPublicKey.epoch,
},
Expand Down

0 comments on commit 744fc57

Please sign in to comment.