Skip to content

Commit

Permalink
Merge pull request #361 from getsafle/dev
Browse files Browse the repository at this point in the history
Vault evm controller integration dev->test
  • Loading branch information
sshubhamagg authored Oct 23, 2024
2 parents d48278d + f94cc42 commit 9cee2d9
Show file tree
Hide file tree
Showing 8 changed files with 988 additions and 497 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Thumbs.db
*.app
*.exe
*.war

.env
# Large media files
*.mp4
*.tiff
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,9 @@
- Update BEVM controller version
- Fixed validatePin function for tests

### 2.9.1 (2024-09-23)
### 2.9.2 (2024-09-23)

-Integrated vault-evm-controller
-Resolved issue for unarchival of a wallet
-Adding test for bitcoin
-Adding sensitive info in env github pipeline
28 changes: 22 additions & 6 deletions package-lock.json

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

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@getsafle/safle-vault",
"version": "2.9.1",
"version": "2.9.2",
"description": "Safle Vault is a non-custodial, flexible and highly available crypto wallet which can be used to access dapps, send/receive crypto and store identity. Vault SDK is used to manage the vault and provide methods to generate vault, add new accounts, update the state and also enable the user to perform several vault related operations.",
"main": "src/index.js",
"scripts": {
Expand Down Expand Up @@ -57,7 +57,7 @@
"@getsafle/vault-bitcoin-controller": "^2.0.7",
"@getsafle/vault-bsc-controller": "^1.2.4",
"@getsafle/vault-eth-controller": "^1.4.6",
"@getsafle/vault-evm-controller": "^1.0.0",
"@getsafle/vault-evm-controller": "^1.0.1",
"@getsafle/vault-mantle-controller": "^1.0.1",
"@getsafle/vault-optimism-controller": "^1.0.8",
"@getsafle/vault-polygon-controller": "^1.2.8",
Expand All @@ -67,7 +67,10 @@
"@getsafle/vault-stacks-controller": "^1.0.5",
"@getsafle/vault-velas-controller": "^1.3.1",
"bip39": "^3.0.4",
"crypto": "^1.0.1",
"crypto-js": "^4.1.1",
"dotenv": "^16.4.5",
"eth-sig-util": "^3.0.1",
"ethers": "^5.5.3",
"jest": "^29.4.3",
"limiter": "^2.1.0",
Expand Down
164 changes: 161 additions & 3 deletions src/lib/keyring.js
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,130 @@ class Keyring {
return { response: signedMessage };
}
}
async signTypedMessage(address, data, pin, encryptionKey, rpcUrl = "") {
if (
typeof pin != "string" ||
pin.match(/^[0-9]+$/) === null ||
pin < 0 ||
pin.length != 6
) {
return { error: ERROR_MESSAGE.INCORRECT_PIN_TYPE };
}

const res = await this.validatePin(pin);

if (res.response == false || res.error) {
return { error: ERROR_MESSAGE.INCORRECT_PIN };
}

const err = helper.validateEncryptionKey(
this.vault,
JSON.stringify(encryptionKey)
);

if (err.error) {
return { error: err.error };
}

const { error, response } = await this.exportPrivateKey(address, pin);

if (error) {
return { error };
}

const { privateKey, isImported } = response;
if (isImported) {
if (
Chains.evmChains.hasOwnProperty(this.chain) ||
this.chain === "ethereum"
) {
const signedMsg = await this.keyringInstance.customSignTypedMessage(
"0x" + privateKey,
data
);

return { response: signedMsg };
}
return { error: ERROR_MESSAGE.UNSUPPORTED_NON_EVM_FUNCTIONALITY };
}

const accounts = await this.getAccounts();

if (accounts.response.filter((e) => e.address === address).length < 1) {
return { error: ERROR_MESSAGE.NONEXISTENT_KEYRING_ACCOUNT };
}

if (
Chains.evmChains.hasOwnProperty(this.chain) ||
this.chain === "ethereum"
) {
const msgParams = { from: address, data: data };

const signedMsg = await this.keyringInstance.signTypedMessage(msgParams);

return { response: signedMsg };
}

const { signedMessage } = await this[this.chain].signTypedMessage(
data,
address
);

return { response: signedMessage };
}
async personalSign(address, data, pin, encryptionKey, rpcUrl = "") {
if (
typeof pin != "string" ||
pin.match(/^[0-9]+$/) === null ||
pin < 0 ||
pin.length != 6
) {
return { error: ERROR_MESSAGE.INCORRECT_PIN_TYPE };
}

const res = await this.validatePin(pin);

if (res.response == false || res.error) {
return { error: ERROR_MESSAGE.INCORRECT_PIN };
}

const err = helper.validateEncryptionKey(
this.vault,
JSON.stringify(encryptionKey)
);

if (err.error) {
return { error: err.error };
}

const { error, response } = await this.exportPrivateKey(address, pin);

if (error) {
return { error };
}

const { privateKey, isImported } = response;

const accounts = await this.getAccounts();

if (accounts.response.filter((e) => e.address === address).length < 1) {
return { error: ERROR_MESSAGE.NONEXISTENT_KEYRING_ACCOUNT };
}

if (
Chains.evmChains.hasOwnProperty(this.chain) ||
this.chain === "ethereum"
) {
const signedMsg = await this.keyringInstance.customPersonalSign(
"0x" + privateKey,
data
);

return { response: signedMsg };
} else {
return { error: ERROR_MESSAGE.UNSUPPORTED_NON_EVM_FUNCTIONALITY };
}
}

async signTransaction(rawTx, pin, rpcUrl) {
if (
Expand Down Expand Up @@ -621,14 +745,13 @@ class Keyring {
if (isImported) {
const signedTransaction = await this.keyringInstance.signTransaction(
rawTx,
web3,
privateKey
);
return { response: signedTransaction };
} else {
const signedTx = await this.keyringInstance.signTransaction(
rawTx,
web3
privateKey
);
return { response: signedTx };
}
Expand Down Expand Up @@ -1083,7 +1206,42 @@ class Keyring {
});

if (isDuplicateAddress) {
return { error: ERROR_MESSAGE.ADDRESS_ALREADY_PRESENT };
try {
Object.keys(this.decryptedVault)
.slice(0, -1)
.forEach((chain) => {
this.decryptedVault[chain]?.public?.forEach(
(account, index) => {
if (account.address === address && account.isDeleted) {
this.decryptedVault[chain].public[
index
].isDeleted = false;
}
}
);
});

const vault = await helper.cryptography(
JSON.stringify(this.decryptedVault),
JSON.stringify(encryptionKey),
"encryption"
);

this.vault = vault;

this.logs.getState().logs.push({
timestamp: Date.now(),
action: "import-wallet",
vault: this.vault,
chain: this.chain,
address,
});

return { response: { vault, address } };
} catch (error) {
console.error("Error processing duplicate address:", error);
throw error; // or handle it as appropriate for your application
}
}
}

Expand Down
Loading

1 comment on commit 9cee2d9

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report (79%)
File% Stmts% Branch% Funcs% LinesUncovered Line #s
All files78.8969.5890.179.2 
chains94.735010094.73 
   index.js94.735010094.7355
config100100100100 
   index.js100100100100 
constants100100100100 
   index.js100100100100 
constants/responses100100100100 
   index.js100100100100 
lib79.7571.5791.4279.69 
   keyring.js77.3770.291.2277.2753, 70, 80, 84–85, 168–188, 241, 284–294, 371–449, 478, 496–497, 507, 513, 522, 528, 558–567, 572, 586–591, 601, 607, 616, 622, 638, 644, 658–663, 672, 678, 687, 693, 701, 715, 738, 768–781, 871–874, 1085, 1115, 1141–1142, 1161–1278, 1380, 1409–1412, 1436–1438, 1485, 1544–1547, 1575, 1620, 1626
   vault.js92.3883.6792.392.3824, 34, 56–58, 87, 100, 116
utils71.7555.3882.3573.8 
   helper.js71.7555.3882.3573.89–16, 89–99, 132, 155, 167–169, 178–182, 203–205, 232, 250, 268, 335–367

Please sign in to comment.