-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,32 @@ | ||
import { | ||
// eslint-disable-next-line camelcase | ||
ecies_decrypt_k256_sha3_256, | ||
// eslint-disable-next-line camelcase | ||
ecies_encrypt_k256_sha3_256, | ||
} from '@xmtp/ecies-bindings-wasm' | ||
import { PrivateKey } from '.' | ||
|
||
// Uses ECIES to encrypt messages where the sender and recipient are the same | ||
export default class SelfEncryption { | ||
privateKey: PrivateKey | ||
|
||
constructor(identityKey: PrivateKey) { | ||
this.privateKey = identityKey | ||
} | ||
|
||
encrypt(data: Uint8Array): Uint8Array { | ||
return ecies_encrypt_k256_sha3_256( | ||
this.privateKey.publicKey.secp256k1Uncompressed.bytes, | ||
this.privateKey.secp256k1.bytes, | ||
data | ||
) | ||
} | ||
|
||
decrypt(message: Uint8Array): Uint8Array { | ||
return ecies_decrypt_k256_sha3_256( | ||
this.privateKey.publicKey.secp256k1Uncompressed.bytes, | ||
this.privateKey.secp256k1.bytes, | ||
message | ||
) | ||
} | ||
} |
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,36 @@ | ||
import { PrivateKeyBundleV1 } from '../../src/crypto/PrivateKeyBundle' | ||
import SelfEncryption from '../../src/crypto/SelfEncryption' | ||
import { newWallet } from '../helpers' | ||
import { equalBytes } from '../../src/crypto/utils' | ||
|
||
describe('SelfEncryption', () => { | ||
let bundle: PrivateKeyBundleV1 | ||
|
||
beforeEach(async () => { | ||
bundle = await PrivateKeyBundleV1.generate(newWallet()) | ||
}) | ||
|
||
it('round trips data', async () => { | ||
const message = new TextEncoder().encode('hello world') | ||
const encryptor = new SelfEncryption(bundle.identityKey) | ||
|
||
const ciphertext = encryptor.encrypt(message) | ||
expect(ciphertext).toBeDefined() | ||
|
||
const decrypted = encryptor.decrypt(ciphertext) | ||
expect(equalBytes(decrypted, message)).toBeTruthy() | ||
}) | ||
|
||
it('throws on decryption failure', async () => { | ||
const message = new TextEncoder().encode('hello world') | ||
const encryptor = new SelfEncryption(bundle.identityKey) | ||
|
||
const ciphertext = encryptor.encrypt(message) | ||
expect(ciphertext).toBeDefined() | ||
|
||
const differentEncryptor = new SelfEncryption( | ||
(await PrivateKeyBundleV1.generate(newWallet())).identityKey | ||
) | ||
expect(() => differentEncryptor.decrypt(ciphertext)).toThrow() | ||
}) | ||
}) |