Skip to content

Commit

Permalink
Merge pull request #6 from bytedeveloperr/encryptions
Browse files Browse the repository at this point in the history
add wallet encryptions for ethereum wallets
  • Loading branch information
iamnotstatic authored May 5, 2022
2 parents d665b10 + aa18625 commit 091b4f7
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.DS_Store
node_modules
dist
.vscode
62 changes: 57 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ The following methods are available with this SDK:
- [Get Address from Private Key](#get-address-from-private-key)
- [Get Transaction Hash](#get-transaction-with-hash)
- [Transfer](#transfer)
- [Encrypt Private Key](#encrypt-private-key)
- [Decrypt Encrypted JSON](#decrypt-encrypted-json)

### Create Wallet

Expand Down Expand Up @@ -121,7 +123,7 @@ const data = await multichainWallet.getBalance({

```javascript
{
balance: 2
balance: 2;
}
```

Expand Down Expand Up @@ -181,7 +183,7 @@ const address = await multichainWallet.getAddressFromPrivateKey({

```javascript
{
address: '0x1C082D1052fb44134a408651c01148aDBFcCe7Fe'
address: '0x1C082D1052fb44134a408651c01148aDBFcCe7Fe';
}
```

Expand Down Expand Up @@ -211,7 +213,7 @@ const receipt = await multichainWallet.getTransaction({
```javascript
{
receipt: {
object
object;
}
}
```
Expand Down Expand Up @@ -285,8 +287,58 @@ const transfer = await multichainWallet.transfer({

```javascript
{
hash: '0xf2978fe918f3e28b7f57101bbc99aa9d7d2d71507ca4a08bac6dd09575527502'
hash: '0xf2978fe918f3e28b7f57101bbc99aa9d7d2d71507ca4a08bac6dd09575527502';
}
```

### Encryptions

#### Encrypt Private Key

It supports encryption of ethereum and other EVM compatible chains private keys.

```javascript
// encrypt private key.

const encrypted = await multichainWallet.getEncryptedJsonFromPrivateKey({
network: 'ethereum',
privateKey:
'0f9e5c0bee6c7d06b95204ca22dea8d7f89bb04e8527a2c59e134d185d9af8ad',
password: 'walletpassword',
});
```

#### Response

```javascript
{
json: '{"address":"1c082d1052fb44134a408651c01148adbfcce7fe","id":"abfb9f10-165a-4b7a-935d-51729f10c310","version":3,"Crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"f3fac53ee2d76c293977d1af3a7d73bb"},"ciphertext":"c5034579cdf32d7a612c9a83801aad899abfebb7436712f363ecf89546bbcbce","kdf":"scrypt","kdfparams":{"salt":"78ff80ece5d681b1aecd829526388472d1889da233229fa5c1416e8f2035b7a8","n":131072,"dklen":32,"p":1,"r":8},"mac":"0f70eca6138ffe60b174308b6ab7a8a81a0d2b662e2cf5d8727443cf12af766c"}}';
}
```

#### Decrypt Encrypted JSON

It supports decryption of encrypted JSONs (A.K.A keystore).

```javascript
// decrypting encrypted JSON.

const decrypted = await multichainWallet.getWalletFromEncryptedJson({
network: 'ethereum',
json:
'{"address":"1c082d1052fb44134a408651c01148adbfcce7fe","id":"abfb9f10-165a-4b7a-935d-51729f10c310","version":3,"Crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"f3fac53ee2d76c293977d1af3a7d73bb"},"ciphertext":"c5034579cdf32d7a612c9a83801aad899abfebb7436712f363ecf89546bbcbce","kdf":"scrypt","kdfparams":{"salt":"78ff80ece5d681b1aecd829526388472d1889da233229fa5c1416e8f2035b7a8","n":131072,"dklen":32,"p":1,"r":8},"mac":"0f70eca6138ffe60b174308b6ab7a8a81a0d2b662e2cf5d8727443cf12af766c"}}',
password: 'walletpassword',
});
```

#### Response

```javascript
{
privateKey: '0x0f9e5c0bee6c7d06b95204ca22dea8d7f89bb04e8527a2c59e134d185d9af8ad',
address: '0x1C082D1052fb44134a408651c01148aDBFcCe7Fe'
}

```

#### Solana Network
Expand Down Expand Up @@ -320,7 +372,7 @@ const transfer = await MultichainCryptoWallet.transfer({

```javascript
{
hash: '3nGq2yczqCpm8bF2dyvdPtXpnFLJ1oGWkDfD6neLbRay8SjNqYNhWQBKE1ZFunxvFhJ47FyT6igNpYPP293jXCZk'
hash: '3nGq2yczqCpm8bF2dyvdPtXpnFLJ1oGWkDfD6neLbRay8SjNqYNhWQBKE1ZFunxvFhJ47FyT6igNpYPP293jXCZk';
}
```

Expand Down
27 changes: 27 additions & 0 deletions src/common/helpers/ethersHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { ethers } from 'ethers';
import {
BalancePayload,
GetAddressFromPrivateKeyPayload,
GetEncryptedJsonFromPrivateKey,
GetTransactionPayload,
GetWalletFromEncryptedjsonPayload,
TransferPayload,
} from '../utils/types';
import { successResponse } from '../utils';
Expand Down Expand Up @@ -183,11 +185,36 @@ const getTransaction = async ({ hash, rpcUrl }: GetTransactionPayload) => {
}
};

const getEncryptedJsonFromPrivateKey = async (
args: GetEncryptedJsonFromPrivateKey
) => {
const wallet = new ethers.Wallet(args.privateKey);
const json = await wallet.encrypt(args.password);

return successResponse({ json });
};

const getWalletFromEncryptedJson = async (
args: GetWalletFromEncryptedjsonPayload
) => {
const wallet = await ethers.Wallet.fromEncryptedJson(
args.json,
args.password
);

return successResponse({
privateKey: wallet.privateKey,
address: wallet.address,
});
};

export default {
getBalance,
createWallet,
getAddressFromPrivateKey,
generateWalletFromMnemonic,
transfer,
getTransaction,
getEncryptedJsonFromPrivateKey,
getWalletFromEncryptedJson,
};
12 changes: 12 additions & 0 deletions src/common/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,15 @@ export interface GenerateWalletFromMnemonicPayload {
export interface IResponse {
[key: string]: any;
}

export interface GetEncryptedJsonFromPrivateKey {
password: string;
privateKey: string;
network: string;
}

export interface GetWalletFromEncryptedjsonPayload {
json: string;
password: string;
network: string;
}
30 changes: 30 additions & 0 deletions src/services/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
GetAddressFromPrivateKeyPayload,
GenerateWalletFromMnemonicPayload,
GetTransactionPayload,
GetWalletFromEncryptedjsonPayload,
GetEncryptedJsonFromPrivateKey,
} from '../../common/utils/types';

export async function getBalance(args: BalancePayload) {
Expand Down Expand Up @@ -103,3 +105,31 @@ export async function getTransaction(args: GetTransactionPayload) {
throw error;
}
}

export async function getEncryptedJsonFromPrivateKey(
args: GetEncryptedJsonFromPrivateKey
) {
try {
if (args.network === 'ethereum') {
return await ethereumHelper.getEncryptedJsonFromPrivateKey({ ...args });
}

return;
} catch (error) {
throw error;
}
}

export async function getWalletFromEncryptedJson(
args: GetWalletFromEncryptedjsonPayload
) {
try {
if (args.network === 'ethereum') {
return await ethereumHelper.getWalletFromEncryptedJson({ ...args });
}

return;
} catch (error) {
throw error;
}
}
27 changes: 27 additions & 0 deletions test/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
getAddressFromPrivateKey,
getTransaction,
transfer,
getWalletFromEncryptedJson,
getEncryptedJsonFromPrivateKey,
} from '../src';

describe('MultichainCryptoWallet', () => {
Expand Down Expand Up @@ -190,4 +192,29 @@ describe('MultichainCryptoWallet', () => {

expect(typeof receipt).toBe('object');
});

it('encrypts ethereum address privatekey and returns the encrypted json', async () => {
const data = await getEncryptedJsonFromPrivateKey({
privateKey:
'0f9e5c0bee6c7d06b95204ca22dea8d7f89bb04e8527a2c59e134d185d9af8ad',
network: 'ethereum',
password: 'walletpassword',
});

expect(typeof data).toBe('object');
expect(typeof (data && data.json)).toBe('string');
});

it('decrypts ethereum address and returns the wallet details', async () => {
const data = await getWalletFromEncryptedJson({
json:
'{"address":"1c082d1052fb44134a408651c01148adbfcce7fe","id":"ca8b45b0-e69d-4e5e-8003-8f3dbb1082cf","version":3,"Crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"fe66bd308ad315126ae6f09f0d6599f4"},"ciphertext":"5f2abe02e49868c36df36f884680b132333e541f89cd7eb375247ff7c8a6ccdd","kdf":"scrypt","kdfparams":{"salt":"2570bf687cb7d9cd694e1c79f6e817c9c66467e81b04013104620670f0664bf5","n":131072,"dklen":32,"p":1,"r":8},"mac":"35f69fb7283c65d75c000a0c93042c063d2903efe9b9e6f03b05d842f47ed1e9"}}',
network: 'ethereum',
password: 'walletpassword',
});

expect(typeof data).toBe('object');
expect(typeof (data && data.privateKey)).toBe('string');
expect(typeof (data && data.address)).toBe('string');
});
});

0 comments on commit 091b4f7

Please sign in to comment.