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

Fraschetti/18343- Add contract test for wallet_getDerivedAddresses #19115

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
31 changes: 31 additions & 0 deletions modules/react-native-status/nodejs/status.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,36 @@ void _InitLogging(const FunctionCallbackInfo<Value>& args) {
delete c;
}

void _RestoreAccountAndLogin(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();

if (args.Length() != 1) {
// Throw an Error that is passed back to JavaScript
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8Literal(isolate, "Wrong number of arguments for RestoreAccountAndLogin")));
return;
}

// Check the argument types

if (!args[0]->IsString()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8Literal(isolate, "Wrong argument type for 'RestoreAccountAndLogin'")));
return;
}

String::Utf8Value arg0Obj(isolate, args[0]->ToString(context).ToLocalChecked());
char *arg0 = *arg0Obj;

// Call exported Go function, which returns a C string
char *c = RestoreAccountAndLogin(arg0);

Local<String> ret = String::NewFromUtf8(isolate, c).ToLocalChecked();
args.GetReturnValue().Set(ret);
delete c;
}


void init(Local<Object> exports) {
NODE_SET_METHOD(exports, "multiAccountGenerateAndDeriveAddresses", _MultiAccountGenerateAndDeriveAddresses);
Expand Down Expand Up @@ -1945,6 +1975,7 @@ void init(Local<Object> exports) {
NODE_SET_METHOD(exports, "connectionChange", _ConnectionChange);
NODE_SET_METHOD(exports, "pollSignal", _PollSignal);
NODE_SET_METHOD(exports, "initLogging", _InitLogging);
NODE_SET_METHOD(exports, "restoreAccountAndLogin", _RestoreAccountAndLogin);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, init)
Expand Down
2 changes: 1 addition & 1 deletion src/mocks/js_dependencies.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(:require-macros [legacy.status-im.utils.slurp :refer [slurp]])
(:require
[legacy.status-im.fleet.default-fleet :refer (default-fleets)])
(:require [legacy.status-im.utils.test :as utils.test]))
(:require [tests.test-utils :as utils.test]))

;; to generate a js Proxy at js/__STATUS_MOBILE_JS_IDENTITY_PROXY__ that accept any (.xxx) call and
;; return itself
Expand Down
14 changes: 13 additions & 1 deletion src/test_helpers/integration.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
(defmacro with-app-initialized
[& body]
`(do
(legacy.status-im.utils.test/init!)
(tests.test-utils/init!)
(if (test-helpers.integration/app-initialized)
(do ~@body)
(do
Expand All @@ -22,3 +22,15 @@
(rf-test/wait-for [:messenger-started]
(test-helpers.integration/assert-messenger-started)
~@body))))

(defmacro with-recovered-account
[& body]
`(if (test-helpers.integration/messenger-started)
(do ~@body)
(do
(test-helpers.integration/recover-multiaccount!)
(rf-test/wait-for
[:messenger-started]
(test-helpers.integration/enable-testnet!)
(test-helpers.integration/assert-messenger-started)
~@body))))
45 changes: 42 additions & 3 deletions src/test_helpers/integration.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
legacy.status-im.events
[legacy.status-im.multiaccounts.logout.core :as logout]
legacy.status-im.subs.root
[legacy.status-im.utils.test :as legacy-test]
[native-module.core :as native-module]
[promesa.core :as p]
[re-frame.core :as rf]
Expand All @@ -15,7 +14,20 @@
status-im.subs.root
[taoensso.timbre :as log]
[tests.integration-test.constants :as constants]
[utils.collection :as collection]))
[tests.test-utils :as test-utils]
[utils.collection :as collection]
[utils.security.core :as security]
[utils.transforms :as transforms]))

(defn validate-mnemonic
[mnemonic on-success on-error]
(native-module/validate-mnemonic
(security/safe-unmask-data mnemonic)
(fn [result]
(let [{:keys [error keyUID]} (transforms/json->clj result)]
(if (seq error)
(when on-error (on-error error))
(on-success mnemonic keyUID))))))

(def default-re-frame-wait-for-timeout-ms
"Controls the maximum time allowed to wait for all events to be processed by
Expand Down Expand Up @@ -125,7 +137,7 @@

(defn setup-app
[]
(legacy-test/init!)
(test-utils/init!)
(if (app-initialized)
(p/resolved ::app-initialized)
(do
Expand Down Expand Up @@ -223,3 +235,30 @@
(set! rf.interop/debug-enabled? false))
:after (fn []
(set! rf.interop/debug-enabled? true))})

(defn recover-and-login
[seed-phrase]
(rf/dispatch [:profile.recover/recover-and-login
{:display-name (:name constants/recovery-account)
:seed-phrase seed-phrase
:password constants/password
:color "blue"}]))

(defn enable-testnet!
[]
(rf/dispatch [:profile.settings/profile-update :test-networks-enabled?
true {}])
(rf/dispatch [:wallet/initialize]))

(defn recover-multiaccount!
[]
(let [masked-seed-phrase (security/mask-data (:seed-phrase constants/recovery-account))]
(validate-mnemonic
masked-seed-phrase
(fn [mnemonic key-uid]
(rf/dispatch [:onboarding/seed-phrase-validated
(security/mask-data mnemonic) key-uid])
(rf/dispatch [:pop-to-root :profiles])
(rf/dispatch [:profile/profile-selected key-uid])
(recover-and-login mnemonic))
#())))
4 changes: 2 additions & 2 deletions src/tests/contract_test/core_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
legacy.status-im.events
[legacy.status-im.multiaccounts.logout.core :as logout]
legacy.status-im.subs.root
[legacy.status-im.utils.test :as utils.test]
[re-frame.core :as rf]
status-im.events
status-im.navigation.core
status-im.subs.root
[test-helpers.integration :as h]))
[test-helpers.integration :as h]
[tests.test-utils :as utils.test]))

(deftest initialize-app-test
(h/log-headline :initialize-app-test)
Expand Down
32 changes: 31 additions & 1 deletion src/tests/contract_test/wallet_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
legacy.status-im.events
[legacy.status-im.multiaccounts.logout.core :as logout]
legacy.status-im.subs.root
[native-module.core :as native-module]
[status-im.common.emoji-picker.utils :as emoji-picker.utils]
[status-im.constants :as constants]
[status-im.contexts.wallet.data-store :as data-store]
status-im.events
status-im.navigation.core
status-im.subs.root
[test-helpers.integration :as h]
[tests.contract-test.utils :as contract-utils]))
[tests.contract-test.utils :as contract-utils]
[tests.integration-test.constants :as integration-constants]))

(defn assert-accounts-get-accounts
[result]
Expand Down Expand Up @@ -84,3 +86,31 @@
(h/logout)
(rf-test/wait-for
[::logout/logout-method])))))

(defn get-main-account
[accounts]
(:address (first accounts)))

(def test-password integration-constants/password)

(defn assert-derived-account
[response]
(is (= (:address response) (:address response)))
(is (= (:public-key response) (:public-key response)))
(is (= "m/43'/60'/1581'/0'/0" (:path (first response)))))

(deftest wallet-get-derived-addressess-contract
(h/log-headline :wallet/create-derived-addresses)
(rf-test/run-test-async
(h/with-app-initialized
(h/with-recovered-account
(let [sha3-pwd (native-module/sha3 test-password)
derivation-path ["m/43'/60'/1581'/0'/0"]
main-account (contract-utils/call-rpc-endpoint
{:rpc-endpoint "accounts_getAccounts"
:action get-main-account})]

(contract-utils/call-rpc-endpoint
{:rpc-endpoint "wallet_getDerivedAddresses"
:params [sha3-pwd main-account derivation-path]
:action assert-derived-account}))))))
7 changes: 7 additions & 0 deletions src/tests/integration_test/constants.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@
(def community {:membership 1 :name "foo" :description "bar"})

(def account-name "account-abc")

(def recovery-account
{:name "wallet-abc"
:seed-phrase "destroy end torch puzzle develop note wise island disease chaos kind bus"
:key-uid "0x6827f3d1e21ae723a24e0dcbac1853b5ed4d651c821a2326492ce8f2367ec905"
:public-key "0x48b8b9e2a8f71e1beae729d83bcd385ffc6af0d5"
:main-account "0x48b8b9e2a8f71e1beae729d83bcd385ffc6af0d5"})
6 changes: 3 additions & 3 deletions src/tests/integration_test/core_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
legacy.status-im.events
[legacy.status-im.multiaccounts.logout.core :as logout]
legacy.status-im.subs.root
[legacy.status-im.utils.test :as utils.test]
[promesa.core :as p]
[re-frame.core :as rf]
status-im.events
status-im.navigation.core
status-im.subs.root
[test-helpers.integration :as h]))
[test-helpers.integration :as h]
[tests.test-utils :as test-utils]))

(deftest initialize-app-test
(h/integration-test ::initialize-app
(fn []
(p/do
(utils.test/init!)
(test-utils/init!)
(rf/dispatch [:app-started])
;; Use initialize-view because it has the longest avg. time and is
;; dispatched by initialize-multiaccounts (last non-view event).
Expand Down
4 changes: 2 additions & 2 deletions src/tests/integration_test/profile_test.cljs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
(ns tests.integration-test.profile-test
(:require
[cljs.test :refer [deftest is use-fixtures]]
[legacy.status-im.utils.test :as utils.test]
[promesa.core :as p]
[status-im.contexts.profile.utils :as profile.utils]
[test-helpers.integration :as h]
[tests.test-utils :as test-utils]
[utils.re-frame :as rf]))

(use-fixtures :each (h/fixture-session))
Expand All @@ -24,7 +24,7 @@
(h/integration-test ::edit-profile-picture
(fn []
(let [mock-image "resources/images/mock2/monkey.png"
absolute-path (.resolve utils.test/path mock-image)]
absolute-path (.resolve test-utils/path mock-image)]
(p/do
(rf/dispatch [:profile/edit-picture absolute-path 80 80])
(h/wait-for [:profile/update-local-picture :toasts/upsert])
Expand Down
Loading