Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sign typed message and updating public rpcs #352

Merged
merged 13 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading