-
Notifications
You must be signed in to change notification settings - Fork 0
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
5 changed files
with
77 additions
and
189 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,46 @@ | ||
/** | ||
* This Key Manager is based on the cryptography package provided by | ||
* @nearfoundation/near-js-encryption-box | ||
* It relies on base58 encoding and requires a nonce to decrypt the key! | ||
*/ | ||
|
||
import { HDNodeWallet } from "ethers"; | ||
import { KeyContract } from "../keyContract"; | ||
import { EthKeyManager } from "./interface"; | ||
import { NearAccount } from "../types"; | ||
import CryptoJS from "crypto-js"; | ||
|
||
export class CryptoJSKeyManager implements EthKeyManager { | ||
// EthKeyContract connected to account for `nearPrivateKey`. | ||
contract: KeyContract; | ||
|
||
constructor(contract: KeyContract) { | ||
this.contract = contract; | ||
} | ||
|
||
async encryptAndSetKey( | ||
ethWallet: HDNodeWallet, | ||
encryptionKey: string, | ||
): Promise<string | undefined> { | ||
let encryptedKey = CryptoJS.AES.encrypt( | ||
ethWallet.privateKey, | ||
encryptionKey, | ||
); | ||
console.log("Posting Encrypted Key", encryptedKey.toString()); | ||
await this.contract.methods.set_key({ | ||
encrypted_key: encryptedKey.toString(), | ||
}); | ||
return undefined; | ||
} | ||
|
||
async retrieveAndDecryptKey( | ||
nearAccount: NearAccount, | ||
// nonce?: string | undefined, | ||
): Promise<string> { | ||
const retrievedKey = await this.contract.methods.get_key({ | ||
account_id: nearAccount.accountId, | ||
}); | ||
let bytes = CryptoJS.AES.decrypt(retrievedKey!, nearAccount.privateKey); | ||
return bytes.toString(CryptoJS.enc.Utf8); | ||
} | ||
} |
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,3 +1,4 @@ | ||
export * from "./encryption/base58"; | ||
export * from "./encryption/cryptojs"; | ||
export * from "./keyContract"; | ||
export * from "./types"; |
Oops, something went wrong.