Skip to content
Open
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
17 changes: 17 additions & 0 deletions manta-js/examples/asset-webpack-ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,29 @@ import { web3Accounts, web3Enable, web3FromSource } from '@polkadot/extension-da
async function main() {
// await toPrivateOnlySignTest();
await toPrivateTest();
// await testStorage();
// await privateTransferTest();
// await toPublicTest();
// await publicTransferTest();
console.log("END");
}

/// Test to storage forward and back
const testStorage = async () => {
const privateWalletConfig = {
environment: Environment.Development,
network: Network.Dolphin
}

const privateWallet = await MantaPrivateWallet.init(privateWalletConfig);
await privateWallet.updateStorage();
const res = await privateWallet.updateFromStorage();
const storage = await privateWallet.getStorage();

console.log(res);
console.log(storage);
}

// Get Polkadot JS Signer and Polkadot JS account address.
const getPolkadotSignerAndAddress = async () => {
const extensions = await web3Enable('Polkadot App');
Expand Down
13 changes: 13 additions & 0 deletions manta-js/examples/asset-webpack-ts/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3697,6 +3697,13 @@ iconv-lite@^0.6.2:
dependencies:
safer-buffer ">= 2.1.2 < 3.0.0"

idb-keyval@^6.2.0:
version "6.2.0"
resolved "https://registry.yarnpkg.com/idb-keyval/-/idb-keyval-6.2.0.tgz#3af94a3cc0689d6ee0bc9e045d2a3340ea897173"
integrity sha512-uw+MIyQn2jl3+hroD7hF8J7PUviBU7BPKWw4f/ISf32D4LoGu98yHjrzWWJDASu9QNrX10tCJqk9YY0ClWm8Ng==
dependencies:
safari-14-idb-fix "^3.0.0"

ieee754@^1.1.13, ieee754@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.2.1.tgz#8eb7a10a63fff25d15a57b001586d177d1b0d352"
Expand Down Expand Up @@ -4167,6 +4174,7 @@ manta.js@../../package:
"@polkadot/util-crypto" "8.1.2"
axios "^0.27.2"
bn.js "^5.2.0"
idb-keyval "^6.2.0"
scale-codec "^0.10.0"
typescript "^4.9.4"

Expand Down Expand Up @@ -5396,6 +5404,11 @@ rxjs@^7.3.0, rxjs@^7.4.0, rxjs@^7.5.5, rxjs@^7.8.0:
dependencies:
tslib "^2.1.0"

safari-14-idb-fix@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/safari-14-idb-fix/-/safari-14-idb-fix-3.0.0.tgz#450fc049b996ec7f3fd9ca2f89d32e0761583440"
integrity sha512-eBNFLob4PMq8JA1dGyFn6G97q3/WzNtFK4RnzT1fnLq+9RyrGknzYiM/9B12MnKAxuj1IXr7UKYtTNtjyKMBog==

safe-buffer@5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1:
version "5.1.2"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"
Expand Down
3 changes: 2 additions & 1 deletion manta-js/package/package.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"axios": "^0.27.2",
"bn.js": "^5.2.0",
"scale-codec": "^0.10.0",
"typescript": "^4.9.4"
"typescript": "^4.9.4",
"idb-keyval": "^6.2.0"
},
"browser": {
"crypto": false
Expand Down
63 changes: 61 additions & 2 deletions manta-js/package/src/privateWallet.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { ApiPromise, WsProvider } from '@polkadot/api';
import { base58Decode, base58Encode } from '@polkadot/util-crypto';
import Api, { ApiConfig } from './api/index';
import BN from 'bn.js';
import { get as getStorage, set as setStorage } from 'idb-keyval';
import config from './manta-config.json';
import { Transaction as WasmTransaction, Wallet as WasmWallet } from './wallet/crate/pkg/manta_wasm_wallet';
import { Transaction as WasmTransaction, Wallet as WasmWallet, StorageStateOption } from './wallet/crate/pkg/manta_wasm_wallet';
import { Signer, SubmittableExtrinsic } from '@polkadot/api/types';
import {
Address,
Expand Down Expand Up @@ -72,6 +73,7 @@ export class MantaPrivateWallet implements IMantaPrivateWallet {
wasmApi: any;
walletIsBusy: boolean;
initialSyncIsFinished: boolean;
storageUpdated: boolean;
loggingEnabled: boolean;
isBindAuthorizationContext: boolean;
parameters: any;
Expand All @@ -93,6 +95,7 @@ export class MantaPrivateWallet implements IMantaPrivateWallet {
this.wasmApi = wasmApi;
this.walletIsBusy = false;
this.initialSyncIsFinished = false;
this.storageUpdated = false;
this.loggingEnabled = loggingEnabled;
this.isBindAuthorizationContext = false;
this.parameters = parameters;
Expand Down Expand Up @@ -421,6 +424,60 @@ export class MantaPrivateWallet implements IMantaPrivateWallet {
}
}

/// Gets the storage for the specific Network Signer and updates the IndexDB storage
/// Returns true if successful, false if not
/// TODO: do not rewrite whole storage on updates (after prototype)
async updateStorage(): Promise<boolean> {
try {
await this.waitForWallet();
this.walletIsBusy = true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think storing utxos to storage should be asynchronous, it should not block user interaction. even if storage fails

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah should be fine since we are just reading the signer storage. Will update

this.log('Beginning storage write');

const networkType = this.wasm.Network.from_string(`"${this.network}"`);
const storageOption = await this.wasmWallet.get_storage_string(networkType);

this.walletIsBusy = false;
if (!storageOption) {
return false;
} else {
// TODO: encrypt before save
setStorage(this.network, storageOption);
}

this.storageUpdated = true;
return true;
} catch (e) {
this.walletIsBusy = false;
console.error('Storage failed with error: ', e);
return false;
}
}

// Gets the storage from browser IndexDB and send to update the current Network Signer
async updateFromStorage(): Promise<boolean> {
try {
await this.waitForWallet();
this.walletIsBusy = true;
this.log('Beginning storage reading');

const networkType = this.wasm.Network.from_string(`"${this.network}"`);
// TODO: dont forget decryption after encryption is implemented
const storage = this.wasm.StorageStateOption.from_string(await getStorage(this.network));
const res = this.wasmWallet.get_storage(storage, networkType);

this.walletIsBusy = false;
return res;
} catch (e) {
this.walletIsBusy = false;
console.error('Storage reading with error: ', e);
return false;
}
}

// ONLY FOR TESTING
async getStorage(): Promise<String> {
return await getStorage(this.network)
}
///
/// Private Methods
///
Expand Down Expand Up @@ -473,7 +530,6 @@ export class MantaPrivateWallet implements IMantaPrivateWallet {
): Promise<InitWasmResult> {
// will be replaced by Browser.runtime.getURL(url)
const wasm = await import('./wallet/crate/pkg/manta_wasm_wallet');
wasm.init_panic_hook();
const provingPrefix =
'https://media.githubusercontent.com/media/Manta-Network/manta-rs/main/manta-parameters/data/pay';
const parameterPrefix =
Expand Down Expand Up @@ -528,12 +584,15 @@ export class MantaPrivateWallet implements IMantaPrivateWallet {
// );
const storageStateOption = wasm.StorageStateOption.from_string('null');
console.log(`Signer initial time: ${performance.now()}`);

const wasmSigner = new wasm.Signer(fullParameters, multiProvingContext, storageStateOption);
console.log(`Signer initial end time: ${performance.now()}`);

// const wasmSigner = wasm.Signer.new_default_with_random_context();
const wasmLedger = new wasm.PolkadotJsLedger(wasmApi);
const wasmWallet = new wasm.Wallet();
wasmWallet.set_network(wasmLedger, wasmSigner, networkType);

return {
wasm,
wasmWallet,
Expand Down
6 changes: 6 additions & 0 deletions manta-js/package/src/wallet/crate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,12 @@ impl Wallet {
.into()
}

/// Saves `self` as a [`StorageStateOption`] in `network`.
#[inline]
pub fn get_storage_string(&self, network: Network) -> String {
serde_json::to_string(&self.set_storage(network)).unwrap()
}

/// Tries to update `self` from `storage_state` in `network`.
#[inline]
pub fn get_storage(&self, storage_state: StorageStateOption, network: Network) -> bool {
Expand Down
12 changes: 12 additions & 0 deletions manta-js/package/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1600,6 +1600,13 @@ hmac-drbg@^1.0.1:
minimalistic-assert "^1.0.0"
minimalistic-crypto-utils "^1.0.1"

idb-keyval@^6.2.0:
version "6.2.0"
resolved "https://registry.yarnpkg.com/idb-keyval/-/idb-keyval-6.2.0.tgz#3af94a3cc0689d6ee0bc9e045d2a3340ea897173"
integrity sha512-uw+MIyQn2jl3+hroD7hF8J7PUviBU7BPKWw4f/ISf32D4LoGu98yHjrzWWJDASu9QNrX10tCJqk9YY0ClWm8Ng==
dependencies:
safari-14-idb-fix "^3.0.0"

ieee754@^1.1.13:
version "1.2.1"
resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.2.1.tgz#8eb7a10a63fff25d15a57b001586d177d1b0d352"
Expand Down Expand Up @@ -2085,6 +2092,11 @@ rxjs@^7.3.0, rxjs@^7.4.0, rxjs@^7.5.5:
dependencies:
tslib "^2.1.0"

safari-14-idb-fix@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/safari-14-idb-fix/-/safari-14-idb-fix-3.0.0.tgz#450fc049b996ec7f3fd9ca2f89d32e0761583440"
integrity sha512-eBNFLob4PMq8JA1dGyFn6G97q3/WzNtFK4RnzT1fnLq+9RyrGknzYiM/9B12MnKAxuj1IXr7UKYtTNtjyKMBog==

safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.2, safe-buffer@^5.2.0, safe-buffer@~5.2.0:
version "5.2.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
Expand Down