-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: encrypt sms factor key before sending it to web3auth's service
- Loading branch information
Showing
2 changed files
with
45 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { getPubKeyPoint, encrypt, type EncryptedMessage, decrypt, toPrivKeyECC } from '@tkey-mpc/common-types' | ||
import { Point } from '@web3auth/mpc-core-kit' | ||
import type BN from 'bn.js' | ||
|
||
/** | ||
* Util class to encrypt and decrypt data using i.e. the postbox key of the torus storage | ||
*/ | ||
export default class EncryptionUtil { | ||
private privateKey: BN | ||
|
||
constructor(privateKey: BN) { | ||
this.privateKey = privateKey | ||
} | ||
|
||
public async encrypt<T>(data: T): Promise<string> { | ||
const oAuthKeyBN = this.privateKey | ||
const oAuthPubKey = getPubKeyPoint(oAuthKeyBN) | ||
const payload = JSON.stringify(data) | ||
const encryptedMessage = await encrypt( | ||
Point.fromTkeyPoint(oAuthPubKey).toBufferSEC1(false), | ||
Buffer.from(payload, 'utf-8'), | ||
) | ||
|
||
return JSON.stringify(encryptedMessage) | ||
} | ||
|
||
public async decrypt<T>(data: string): Promise<T> { | ||
const encryptedData = JSON.parse(data) as EncryptedMessage | ||
const decryptedDataBuffer = await decrypt(toPrivKeyECC(this.privateKey), encryptedData) | ||
return JSON.parse(decryptedDataBuffer.toString('utf-8')) as T | ||
} | ||
} |
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