Skip to content

Commit

Permalink
Merge pull request #19 from getsafle/tests
Browse files Browse the repository at this point in the history
Tests
  • Loading branch information
sshubhamagg authored Aug 19, 2024
2 parents 17268e9 + 89bdbbb commit 11d9f3e
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
- Implemented functionality to get gass fees
- Implemented functionality to broadcast a transaction
- Implemented Readme
- Implemented tests
2 changes: 1 addition & 1 deletion package-lock.json

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

42 changes: 42 additions & 0 deletions test/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module.exports = {
HD_WALLET_12_MNEMONIC_TEST_OTHER:
"orange lecture tiger surround narrow much novel arrange sample balance weapon bacon",
HD_WALLET_12_MNEMONIC:
"affair entry detect broom axis crawl found valve bamboo taste broken hundred",
HD_WALLET_24_MNEMONIC:
"begin pyramid grit rigid mountain stamp legal item result peace wealth supply satoshi elegant roof identify furnace march west chicken pen gorilla spot excuse",

TESTING_MESSAGE_1: "ThisMessageOneIsForTesting",
TESTING_MESSAGE_2: "This_message_two_is_for_testing",
TESTING_MESSAGE_3: "This message three is for testing",

EXTERNAL_ACCOUNT_PRIVATE_KEY:
"0xbcb7a8680126610ca94440b020280f9ef82194a4dc2760653073b5f5b150c9c3",
EXTERNAL_ACCOUNT_ADDRESS: "0x9E1447ea3F6abA7a5D344B360B95Fd9BAE049448",
EXTERNAL_ACCOUNT_WRONG_PRIVATE_KEY_1: "random_private_key",
EXTERNAL_ACCOUNT_WRONG_PRIVATE_KEY_2:
"0xbcb7a8680126610ca94440b020280f9ef829ad26637bfb5cc",
EXTERNAL_ACCOUNT_WRONG_PRIVATE_KEY_3:
"QUWL7cmUp9Cj9DF3gLFqqSipopXyzuF4QXmDNV3ZTZ28GB6Ug98Z",

TRANSACTION_TYPE: {
NATIVE_TRANSFER: "NATIVE_TRANSFER",
CONTRACT_TRANSACTION: "CONTRACT_TRANSACTION",
},
TRANSFER_BTC: {
BTC_RECEIVER: "0xd27189917dd3E4B0e9eB731eCEe358254520FA01", // generated from HD_WALLET_12_MNEMONIC_TEST_OTHER
BTC_AMOUNT: 13,
},
NETWORK: {
MAINNET: {
NETWORK: "MAINNET",
CHAIN_ID: 30,
URL: "https://rpc.mainnet.rootstock.io/Ufjv34jGv6FZXRv0IsbpNjoIygQ0X4-T",
},
TESTNET: {
NETWORK: "TESTNET",
CHAIN_ID: 31,
URL: "https://rpc.testnet.rootstock.io/Ufjv34jGv6FZXRv0IsbpNjoIygQ0X4-T",
},
},
};
89 changes: 89 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
var assert = require("assert");
const Web3 = require("web3");
const CryptoJS = require("crypto-js");
const {
KeyringController: RootStockKeyring,
getBalance,
} = require("../src/index");
const {
HD_WALLET_12_MNEMONIC,
EXTERNAL_ACCOUNT_PRIVATE_KEY,
EXTERNAL_ACCOUNT_ADDRESS,
NETWORK: { TESTNET, MAINNET },
} = require("./constants");

const opts = {
encryptor: {
encrypt(pass, object) {
const ciphertext = CryptoJS.AES.encrypt(
JSON.stringify(object),
pass
).toString();

return ciphertext;
},
decrypt(pass, encryptedString) {
const bytes = CryptoJS.AES.decrypt(encryptedString, pass);
const decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));

return decryptedData;
},
},
};

const opts_empty = {};

const PASSWORD = "random_password";

describe("Initialize wallet ", () => {
const rootstockKeyring = new RootStockKeyring(opts);

it("Create new vault and keychain", async () => {
const res = await rootstockKeyring.createNewVaultAndKeychain(PASSWORD);
console.log("res ", res);
});

it("Create new vault and restore", async () => {
const res = await rootstockKeyring.createNewVaultAndRestore(
PASSWORD,
HD_WALLET_12_MNEMONIC
);
assert(
rootstockKeyring.keyrings[0].mnemonic === HD_WALLET_12_MNEMONIC,
"Wrong mnemonic"
);
});

it("Export account (privateKey)", async () => {
const res = await rootstockKeyring.getAccounts();
let account = res[0];
const accRes = await rootstockKeyring.exportAccount(account);
console.log("accRes ", accRes, Buffer.from(accRes, "hex"));
});

it("Get accounts", async () => {
const acc = await rootstockKeyring.getAccounts();
console.log("acc ", acc);
});

it("Should import correct account ", async () => {
const address = await rootstockKeyring.importWallet(
EXTERNAL_ACCOUNT_PRIVATE_KEY
);
assert(
address.toLowerCase() === EXTERNAL_ACCOUNT_ADDRESS.toLowerCase(),
"Wrong address"
);
assert(
rootstockKeyring.importedWallets.length === 1,
"Should have 1 imported wallet"
);
});

it("Get address balance", async () => {
const accounts = await rootstockKeyring.getAccounts();
const web3 = new Web3(TESTNET.URL);
const balance = await getBalance(accounts[0], web3);
console.log(" get balance ", balance, accounts);
});
});

0 comments on commit 11d9f3e

Please sign in to comment.