-
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
7 changed files
with
142 additions
and
43 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
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,69 @@ | ||
/** | ||
* 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 bs58 from "bs58"; | ||
import { KeyPair } from "near-api-js"; | ||
import { create, open } from "@nearfoundation/near-js-encryption-box"; | ||
|
||
export class Base58KeyManager 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 keyPair = KeyPair.fromString(encryptionKey); | ||
let encodedEthKey = this.encodeEthKey(ethWallet.privateKey); | ||
const { secret: encryptedKey, nonce } = create( | ||
encodedEthKey, | ||
keyPair.getPublicKey().toString(), | ||
encryptionKey, | ||
); | ||
console.log("Posting Encrypted Key", encryptedKey, nonce); | ||
await this.contract.methods.set_key({ encrypted_key: encryptedKey }); | ||
return nonce || undefined; | ||
} | ||
|
||
async retrieveAndDecryptKey( | ||
nearAccount: NearAccount, | ||
nonce?: string, | ||
): Promise<string> { | ||
const retrievedKey = await this.contract.methods.get_key({ | ||
account_id: nearAccount.accountId, | ||
}); | ||
let keyPair = KeyPair.fromString(nearAccount.privateKey); | ||
const decryptedKey = open( | ||
retrievedKey!, | ||
keyPair.getPublicKey().toString(), | ||
nearAccount.privateKey, | ||
nonce!, | ||
); | ||
if (decryptedKey === null) { | ||
throw new Error("Unable to decrypt key!"); | ||
} | ||
return this.decodeEthKey(decryptedKey); | ||
} | ||
|
||
private encodeEthKey(key: string): string { | ||
const bytes = Buffer.from(key.slice(2), "hex"); | ||
const encodedKey = bs58.encode(bytes); | ||
return encodedKey; | ||
} | ||
|
||
private decodeEthKey(key: string): string { | ||
const bytes = Buffer.from(bs58.decode(key)); | ||
return "0x" + bytes.toString("hex"); | ||
} | ||
} |
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,23 @@ | ||
import { ethers } from "ethers"; | ||
import { NearAccount } from "../types"; | ||
|
||
export interface EthKeyManager { | ||
/** | ||
* | ||
* @param ethWallet - Ethereum Wallet to be stored on key contract. | ||
* @param encryptionKey - Secret key of for encryption. | ||
* @returns Nonce if needed decrypt encoded key, otherwise nothing. | ||
*/ | ||
encryptAndSetKey( | ||
ethWallet: ethers.HDNodeWallet, | ||
encryptionKey: string, | ||
): Promise<string | undefined>; | ||
|
||
retrieveAndDecryptKey( | ||
nearAccount: NearAccount, | ||
nonce?: string, | ||
): Promise<string>; | ||
|
||
// encodeEthKey(key: string): string; | ||
// decodeEthKey(key: string): string; | ||
} |
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,3 @@ | ||
export * from "./encryption/base58"; | ||
export * from "./keyContract"; | ||
export * from "./types"; |
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,27 @@ | ||
import { Account, Contract } from "near-api-js"; | ||
|
||
export interface IKeyContract { | ||
set_key: (args: { encrypted_key: string }) => Promise<void>; | ||
get_key: (args: { account_id: string }) => Promise<string | null>; | ||
} | ||
|
||
export class KeyContract { | ||
// Contract method interface | ||
methods: IKeyContract; | ||
// Connected Account | ||
account: Account; | ||
|
||
/** | ||
* Constructs an instance of a connected KeyContract | ||
* @param contractId - Account ID of deployed contract. | ||
* @param account - Near Account to sign change method transactions. | ||
*/ | ||
constructor(contractId: string, account: Account) { | ||
this.account = account; | ||
this.methods = new Contract(account, contractId, { | ||
viewMethods: ["get_key"], | ||
changeMethods: ["set_key"], | ||
useLocalViewExecution: false, | ||
}) as unknown as IKeyContract; | ||
} | ||
} |
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,4 @@ | ||
export interface NearAccount { | ||
accountId: string; | ||
privateKey: string; | ||
} |
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