Skip to content

Commit

Permalink
Merge pull request #26 from CityOfZion/CU-86a1jh86w
Browse files Browse the repository at this point in the history
CU-86a1jh86w - Implement method to encrypt a private key
  • Loading branch information
melanke authored Dec 5, 2023
2 parents 98ec5e6 + aa76979 commit 3cdf1eb
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 9 deletions.
8 changes: 2 additions & 6 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/blockchain-service/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface BlockchainService<BSCustomName extends string = string> {
generateAccountFromMnemonic(mnemonic: string | string, index: number): AccountWithDerivationPath
generateAccountFromKey(key: string): Account
decrypt(keyOrJson: string, password: string): Promise<Account>
encrypt(key: string, password: string): Promise<string>
validateAddress(address: string): boolean
validateEncrypted(keyOrJson: string): boolean
validateKey(key: string): boolean
Expand Down
5 changes: 5 additions & 0 deletions packages/bs-ethereum/src/BSEthereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ export class BSEthereum<BSCustomName extends string = string>
}
}

async encrypt(key: string, password: string): Promise<string> {
const wallet = new ethers.Wallet(key)
return wallet.encrypt(password)
}

async transfer({ senderAccount, intent }: TransferParam): Promise<string> {
const provider = new ethers.providers.JsonRpcProvider(this.network.url)
const wallet = new ethers.Wallet(senderAccount.key, provider)
Expand Down
8 changes: 7 additions & 1 deletion packages/bs-ethereum/src/__tests__/BSEthereum.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,17 @@ describe('BSEthereum', () => {

it('Should be able to decrypt a encrypted key', async () => {
const password = 'TestPassword'
const validEncryptedJson = await wallet.encrypt(password)
const validEncryptedJson = await bsEthereum.encrypt(wallet.privateKey, password)
const decryptedAccount = await bsEthereum.decrypt(validEncryptedJson, password)
expect(decryptedAccount).toEqual(account)
})

it('Should be able to encrypt key', async () => {
const password = 'TestPassword'
const validEncryptedJson = await bsEthereum.encrypt(wallet.privateKey, password)
expect(validEncryptedJson).toEqual(expect.objectContaining({ address: wallet.address }))
})

it.skip('Should be able to calculate transfer fee', async () => {
const account = bsEthereum.generateAccountFromKey(process.env.TESTNET_PRIVATE_KEY as string)

Expand Down
4 changes: 4 additions & 0 deletions packages/bs-neo-legacy/src/BSNeoLegacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ export class BSNeoLegacy<BSCustomName extends string = string>
return this.generateAccountFromKey(privateKey)
}

encrypt(key: string, password: string): Promise<string> {
return wallet.encrypt(key, password)
}

async transfer({ intent: transferIntent, senderAccount, tipIntent, ...params }: TransferParam): Promise<string> {
const apiProvider = new api.neoCli.instance(this.network.url)
const account = new wallet.Account(senderAccount.key)
Expand Down
10 changes: 9 additions & 1 deletion packages/bs-neo-legacy/src/__tests__/BSNeoLegacy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,19 @@ describe('BSNeoLegacy', () => {
const mnemonic = generateMnemonic()
const account = bsNeoLegacy.generateAccountFromMnemonic(mnemonic, 0)
const password = 'TestPassword'
const encryptedKey = await wallet.encrypt(account.key, password)
const encryptedKey = await bsNeoLegacy.encrypt(account.key, password)
const decryptedAccount = await bsNeoLegacy.decrypt(encryptedKey, password)
expect(account).toEqual(expect.objectContaining(decryptedAccount))
}, 10000)

it('Should be able to encrypt a key', async () => {
const mnemonic = generateMnemonic()
const account = bsNeoLegacy.generateAccountFromMnemonic(mnemonic, 0)
const password = 'TestPassword'
const encryptedKey = await bsNeoLegacy.encrypt(account.key, password)
expect(encryptedKey).toEqual(expect.any(String))
})

it.skip('Should be able to transfer a native asset', async () => {
const account = bsNeoLegacy.generateAccountFromKey(process.env.TESTNET_PRIVATE_KEY as string)
const balance = await bsNeoLegacy.blockchainDataService.getBalance(account.address)
Expand Down
4 changes: 4 additions & 0 deletions packages/bs-neo3/src/BSNeo3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ export class BSNeo3<BSCustomName extends string = string>
return this.generateAccountFromKey(privateKey)
}

encrypt(key: string, password: string): Promise<string> {
return wallet.encrypt(key, password)
}

async calculateTransferFee(param: TransferParam): Promise<string> {
const account = new wallet.Account(param.senderAccount.key)
const invoker = await NeonInvoker.init({
Expand Down
10 changes: 9 additions & 1 deletion packages/bs-neo3/src/__tests__/BSNeo3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,19 @@ describe('BSNeo3', () => {
const mnemonic = generateMnemonic()
const account = bsNeo3.generateAccountFromMnemonic(mnemonic, 0)
const password = 'TestPassword'
const encryptedKey = await wallet.encrypt(account.key, password)
const encryptedKey = await bsNeo3.encrypt(account.key, password)
const decryptedAccount = await bsNeo3.decrypt(encryptedKey, password)
expect(account).toEqual(expect.objectContaining(decryptedAccount))
}, 20000)

it('Should be able to encrypt a key', async () => {
const mnemonic = generateMnemonic()
const account = bsNeo3.generateAccountFromMnemonic(mnemonic, 0)
const password = 'TestPassword'
const encryptedKey = await bsNeo3.encrypt(account.key, password)
expect(encryptedKey).toEqual(expect.any(String))
})

it.skip('Should be able to calculate transfer fee', async () => {
const account = bsNeo3.generateAccountFromKey(process.env.TESTNET_PRIVATE_KEY as string)

Expand Down

0 comments on commit 3cdf1eb

Please sign in to comment.