Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const MAINNET_IDENTITY_SERVICE_SECRET = stringFromEnv('MAINNET_IDENTITY_SERVICE_SECRET');
export const ROPSTEN_IDENTITY_SERVICE_APPID = stringFromEnv('ROPSTEN_IDENTITY_SERVICE_APPID');
export const MAINNET_IDENTITY_SERVICE_APPID = stringFromEnv('MAINNET_IDENTITY_SERVICE_APPID');
const trimmableWalletApiEndpoint = (endpoint) => (trimmed) => {
if (trimmed) return endpoint.split('//')[1].split('/')[0]; // trim https:// prefix and / suffix
return endpoint;
};
const ROPSTEN_URL = 'https://api2.dev.hubii.net/';
const MAINNET_URL = 'https://api.nahmii.io/';
export const SUPPORTED_NETWORKS = {
mainnet: {
provider: getDefaultProvider('mainnet'),
nahmiiProvider: new nahmii.NahmiiProvider(
trimmableWalletApiEndpoint(MAINNET_URL)(true),
MAINNET_IDENTITY_SERVICE_APPID,
MAINNET_IDENTITY_SERVICE_SECRET
),
walletApiEndpoint: trimmableWalletApiEndpoint(MAINNET_URL),
identityServiceSecret: process.env.NODE_ENV === 'test' ? 'secret' : MAINNET_IDENTITY_SERVICE_SECRET,
identityServiceAppId: process.env.NODE_ENV === 'test' ? 'appid' : MAINNET_IDENTITY_SERVICE_APPID,
},
ropsten: {
provider: getDefaultProvider('ropsten'),
nahmiiProvider: new nahmii.NahmiiProvider(
trimmableWalletApiEndpoint(ROPSTEN_URL)(true),
ROPSTEN_IDENTITY_SERVICE_APPID,
ROPSTEN_IDENTITY_SERVICE_SECRET
),
walletApiEndpoint: trimmableWalletApiEndpoint(ROPSTEN_URL),
export function* requestToken() {
while (true) { // eslint-disable-line no-constant-condition
let nahmiiProvider;
try {
const network = yield select(makeSelectCurrentNetwork());
nahmiiProvider = new nahmii.NahmiiProvider(
network.walletApiEndpoint(true),
network.identityServiceAppId,
network.identityServiceSecret
);
const token = yield call([nahmiiProvider, 'getApiAccessToken']);
yield put(loadIdentityServiceTokenSuccess(token));
return;
} catch (e) {
// try again in 2sec
const TWO_SEC_IN_MS = 2 * 1000;
yield delay(TWO_SEC_IN_MS);
} finally {
nahmiiProvider.stopUpdate();
}
}
}
export function* loadStagingBalances({ address }, network) {
if (network.provider._network.chainId === 1) {
yield put(actions.loadStagingBalancesSuccess(address, []));
return;
}
let supportedAssets = (yield select(makeSelectSupportedAssets())).toJS();
if (supportedAssets.loading) {
yield take(LOAD_SUPPORTED_TOKENS_SUCCESS);
supportedAssets = (yield select(makeSelectSupportedAssets())).toJS();
}
const { nahmiiProvider } = network;
const driipSettlementChallengeContract = new DriipSettlementChallengeContract(nahmiiProvider);
while (true) { // eslint-disable-line no-constant-condition
try {
const driipSettlementChallengeContractAddress = driipSettlementChallengeContract.address;
// derive function selector
const funcBytes = utils.solidityKeccak256(['string'], ['proposalStageAmount(address,address,uint256)']);
const funcSelector = funcBytes.slice(0, 10);
// send a batch of RPC requests asking for all staging balances
// https://www.jsonrpc.org/specification#batch
const currencyCtList = supportedAssets.assets.map((a) => a.currency);
const requestBatch = currencyCtList.map((ct) => {
// encode arguments, prepare them for being sent
const encodedArgs = utils.defaultAbiCoder.encode(['address', 'address', 'int256'], [address, ct, 0]);
const dataArr = utils.concat([funcSelector, encodedArgs]);
import { makeSelectCurrentWalletWithInfo } from 'containers/WalletHoc/selectors';
import { SET_CURRENT_WALLET } from 'containers/WalletHoc/constants';
import { LOAD_IDENTITY_SERVICE_TOKEN_SUCCESS } from 'containers/HubiiApiHoc/constants';
import { makeSelectCurrentNetwork } from 'containers/App/selectors';
import { REGISTER } from './constants';
import {
registerationSuccess,
registerationFailed,
register as registerAction,
checkAddressRegistrationSuccess,
} from './actions';
import { makeSelectNahmiiAirdriipRegistration } from './selectors';
const MESSAGE = 'I agree to the terms and conditions for signing up for the nahmii airdriip.';
const MESSAGE_HASH = utils.hash(MESSAGE);
const API_PATH = 'airdriips/registrations';
export function* checkRegistrationStatus({ address }) {
try {
// this saga is special, as it may be called before the JWT token for the hubii
// API has been loaded. do a quick check here and ensure the JWT is loaded
let network = yield select(makeSelectCurrentNetwork());
if (!network.identityServiceToken) {
yield take(LOAD_IDENTITY_SERVICE_TOKEN_SUCCESS);
network = yield select(makeSelectCurrentNetwork());
}
const addressStatus = (yield select(makeSelectNahmiiAirdriipRegistration()))
.getIn(['addressStatuses', address]);
if (addressStatus === 'registered') return;
export function* loadStagedBalances({ address }, network) {
if (network.provider._network.chainId === 1) {
yield put(actions.loadStagedBalancesSuccess(address, []));
return;
}
let supportedAssets = (yield select(makeSelectSupportedAssets())).toJS();
if (supportedAssets.loading) {
yield take(LOAD_SUPPORTED_TOKENS_SUCCESS);
supportedAssets = (yield select(makeSelectSupportedAssets())).toJS();
}
const { nahmiiProvider } = network;
const balanceTrackerContract = new BalanceTrackerContract(nahmiiProvider);
while (true) { // eslint-disable-line no-constant-condition
try {
const balanceTrackerContractAddress = balanceTrackerContract.address;
// derive function selector
const balanceType = yield balanceTrackerContract.stagedBalanceType();
const funcBytes = utils.solidityKeccak256(['string'], ['get(address,bytes32,address,uint256)']);
const funcSelector = funcBytes.slice(0, 10);
// send a batch of RPC requests asking for all staged balances
// https://www.jsonrpc.org/specification#batch
const currencyCtList = supportedAssets.assets.map((a) => a.currency);
const requestBatch = currencyCtList.map((ct) => {
// encode arguments, prepare them for being sent
const encodedArgs = utils.defaultAbiCoder.encode(['address', 'bytes32', 'address', 'int256'], [address, balanceType, ct, 0]);
export function* approveTokenDeposit({ address, symbol, amount, options }) {
let confOnDeviceDone;
let confOnDevice;
let signer;
try {
const { nahmiiProvider } = yield select(makeSelectCurrentNetwork());
const wallet = (yield select(makeSelectWallets())).toJS().find((w) => w.address === address);
[signer, confOnDevice, confOnDeviceDone] = yield call(getSdkWalletSigner, wallet);
const nahmiiWallet = new nahmii.Wallet(signer, nahmiiProvider);
if (confOnDevice) yield put(confOnDevice);
const { hash } = yield call(() => nahmiiWallet.approveTokenDeposit(amount, symbol, options));
if (confOnDeviceDone) yield put(confOnDeviceDone);
yield call(() => nahmiiProvider.getTransactionConfirmation(hash));
yield put(actions.nahmiiApproveTokenDepositSuccess());
} catch (e) {
if (confOnDeviceDone) yield put(confOnDeviceDone);
yield put(notify('error', getIntl().formatMessage({ id: 'send_transaction_failed_message_error' }, { message: e.message })));
yield put(actions.nahmiiDepositFailed(`An error occured: ${e.message}`));
}
}
export function* completeTokenDeposit({ address, symbol, amount, options }) {
let confOnDeviceDone;
let confOnDevice;
let signer;
try {
const { nahmiiProvider } = yield select(makeSelectCurrentNetwork());
const wallet = (yield select(makeSelectWallets())).toJS().find((w) => w.address === address);
[signer, confOnDevice, confOnDeviceDone] = yield call(getSdkWalletSigner, wallet);
const nahmiiWallet = new nahmii.Wallet(signer, nahmiiProvider);
if (confOnDevice) yield put(confOnDevice);
const { hash } = yield call(() => nahmiiWallet.completeTokenDeposit(amount, symbol, options));
if (confOnDeviceDone) yield put(confOnDeviceDone);
yield call(() => nahmiiProvider.getTransactionConfirmation(hash));
yield put(actions.nahmiiCompleteTokenDepositSuccess());
yield put(notify('success', getIntl().formatMessage({ id: 'deposit_success' })));
} catch (e) {
if (confOnDeviceDone) yield put(confOnDeviceDone);
yield put(notify('error', getIntl().formatMessage({ id: 'send_transaction_failed_message_error' }, { message: e.message })));
yield put(actions.nahmiiDepositFailed(`An error occured: ${e.message}`));
}
}
export function* getNahmiiProvider() {
const network = yield select(makeSelectCurrentNetwork());
const nahmiiProvider = new nahmii.NahmiiProvider(
network.walletApiEndpoint(true),
network.identityServiceAppId,
network.identityServiceSecret
);
return nahmiiProvider;
}
export const SUPPORTED_NETWORKS = {
mainnet: {
provider: getDefaultProvider('mainnet'),
nahmiiProvider: new nahmii.NahmiiProvider(
trimmableWalletApiEndpoint(MAINNET_URL)(true),
MAINNET_IDENTITY_SERVICE_APPID,
MAINNET_IDENTITY_SERVICE_SECRET
),
walletApiEndpoint: trimmableWalletApiEndpoint(MAINNET_URL),
identityServiceSecret: process.env.NODE_ENV === 'test' ? 'secret' : MAINNET_IDENTITY_SERVICE_SECRET,
identityServiceAppId: process.env.NODE_ENV === 'test' ? 'appid' : MAINNET_IDENTITY_SERVICE_APPID,
},
ropsten: {
provider: getDefaultProvider('ropsten'),
nahmiiProvider: new nahmii.NahmiiProvider(
trimmableWalletApiEndpoint(ROPSTEN_URL)(true),
ROPSTEN_IDENTITY_SERVICE_APPID,
ROPSTEN_IDENTITY_SERVICE_SECRET
),
walletApiEndpoint: trimmableWalletApiEndpoint(ROPSTEN_URL),
identityServiceSecret: process.env.NODE_ENV === 'test' ? 'secret' : ROPSTEN_IDENTITY_SERVICE_SECRET,
identityServiceAppId: process.env.NODE_ENV === 'test' ? 'appid' : ROPSTEN_IDENTITY_SERVICE_APPID,
},
};
export function* makePayment({ monetaryAmount, recipient, walletOverride }) {
let signer;
let confOnDevice;
let confOnDeviceDone;
try {
const wallet = walletOverride || (yield (select(makeSelectCurrentWalletWithInfo()))).toJS();
if (wallet.encrypted && !wallet.decrypted) {
yield put(showDecryptWalletModal(actions.makeNahmiiPayment(monetaryAmount, recipient, walletOverride)));
yield put(actions.nahmiiPaymentError(new Error(getIntl().formatMessage({ id: 'wallet_encrypted_error' }))));
return;
}
const network = yield select(makeSelectCurrentNetwork());
const nahmiiProvider = network.nahmiiProvider;
[signer, confOnDevice, confOnDeviceDone] = yield call(getSdkWalletSigner, wallet);
const nahmiiWallet = new nahmii.Wallet(signer, nahmiiProvider);
const payment = new nahmii.Payment(monetaryAmount, wallet.address, recipient, nahmiiWallet);
if (confOnDevice) yield put(confOnDevice);
yield call([payment, 'sign']);
if (confOnDeviceDone) yield put(confOnDeviceDone);
yield call([payment, 'register']);
yield put(actions.nahmiiPaymentSuccess());
yield put(notify('success', getIntl().formatMessage({ id: 'sent_transaction_success' })));
} catch (e) {
if (confOnDeviceDone) yield put(confOnDeviceDone);
yield put(actions.nahmiiPaymentError(e));
yield put(notify('error', getIntl().formatMessage({ id: 'send_transaction_failed_message_error' }, { message: e.message })));
}
}