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

cleanup Nodejs env var checks examples #1386

Merged
merged 8 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
37 changes: 14 additions & 23 deletions bindings/nodejs/examples/exchange/1-create-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,23 @@ require('dotenv').config({ path: '.env' });

async function run() {
try {
if (!process.env.WALLET_DB_PATH) {
throw new Error(
'.env WALLET_DB_PATH is undefined, see .env.example',
);
}
if (!process.env.NODE_URL) {
throw new Error('.env NODE_URL is undefined, see .env.example');
}
if (!process.env.STRONGHOLD_SNAPSHOT_PATH) {
throw new Error(
'.env STRONGHOLD_SNAPSHOT_PATH is undefined, see .env.example',
);
}
if (!process.env.STRONGHOLD_PASSWORD) {
throw new Error(
'.env STRONGHOLD_PASSWORD is undefined, see .env.example',
);
}
if (!process.env.MNEMONIC) {
throw new Error('.env MNEMONIC is undefined, see .env.example');
}
for (const envVar of [
'WALLET_DB_PATH',
'NODE_URL',
'STRONGHOLD_SNAPSHOT_PATH',
'STRONGHOLD_PASSWORD',
'MNEMONIC',
])
if (!(envVar in process.env)) {
throw new Error(
`.env ${envVar} is undefined, see .env.example`,
);
}

const walletOptions: WalletOptions = {
storagePath: process.env.WALLET_DB_PATH,
clientOptions: {
nodes: [process.env.NODE_URL],
nodes: [process.env.NODE_URL as string],
},
coinType: CoinType.IOTA,
secretManager: {
Expand All @@ -51,7 +42,7 @@ async function run() {
const wallet = new Wallet(walletOptions);

// Mnemonic only needs to be set the first time.
await wallet.storeMnemonic(process.env.MNEMONIC);
await wallet.storeMnemonic(process.env.MNEMONIC as string);

const account = await wallet.createAccount({
alias: 'Alice',
Expand Down
20 changes: 9 additions & 11 deletions bindings/nodejs/examples/exchange/2-generate-address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,20 @@ require('dotenv').config({ path: '.env' });

async function run() {
try {
if (!process.env.WALLET_DB_PATH) {
throw new Error(
'.env WALLET_DB_PATH is undefined, see .env.example',
);
}
if (!process.env.STRONGHOLD_PASSWORD) {
throw new Error(
'.env STRONGHOLD_PASSWORD is undefined, see .env.example',
);
}
for (const envVar of ['WALLET_DB_PATH', 'STRONGHOLD_PASSWORD'])
if (!(envVar in process.env)) {
throw new Error(
`.env ${envVar} is undefined, see .env.example`,
);
}

const wallet = new Wallet({
storagePath: process.env.WALLET_DB_PATH,
});

await wallet.setStrongholdPassword(process.env.STRONGHOLD_PASSWORD);
await wallet.setStrongholdPassword(
process.env.STRONGHOLD_PASSWORD as string,
);

const account = await wallet.getAccount('Alice');

Expand Down
27 changes: 13 additions & 14 deletions bindings/nodejs/examples/exchange/5-send-amount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,24 @@ require('dotenv').config({ path: '.env' });

async function run() {
try {
if (!process.env.WALLET_DB_PATH) {
throw new Error(
'.env WALLET_DB_PATH is undefined, see .env.example',
);
}
if (!process.env.STRONGHOLD_PASSWORD) {
throw new Error(
'.env STRONGHOLD_PASSWORD is undefined, see .env.example',
);
}
if (!process.env.EXPLORER_URL) {
throw new Error('.env EXPLORER_URL is undefined, see .env.example');
}
for (const envVar of [
'WALLET_DB_PATH',
'STRONGHOLD_PASSWORD',
'EXPLORER_URL',
])
if (!(envVar in process.env)) {
throw new Error(
`.env ${envVar} is undefined, see .env.example`,
);
}

const wallet = new Wallet({
storagePath: process.env.WALLET_DB_PATH,
});

await wallet.setStrongholdPassword(process.env.STRONGHOLD_PASSWORD);
await wallet.setStrongholdPassword(
process.env.STRONGHOLD_PASSWORD as string,
);

const account = await wallet.getAccount('Alice');
console.log('Account:', account);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,16 @@ require('dotenv').config({ path: '.env' });
async function run() {
initLogger();
try {
if (!process.env.WALLET_DB_PATH) {
throw new Error(
'.env WALLET_DB_PATH is undefined, see .env.example',
);
}

if (!process.env.STRONGHOLD_PASSWORD) {
throw new Error(
'.env STRONGHOLD_PASSWORD is undefined, see .env.example',
);
}

if (!process.env.EXPLORER_URL) {
throw new Error('.env EXPLORER_URL is undefined, see .env.example');
}
for (const envVar of [
'WALLET_DB_PATH',
'STRONGHOLD_PASSWORD',
'EXPLORER_URL',
])
if (!(envVar in process.env)) {
throw new Error(
`.env ${envVar} is undefined, see .env.example`,
);
}

const wallet = new Wallet({
storagePath: process.env.WALLET_DB_PATH,
Expand All @@ -37,7 +32,10 @@ async function run() {
const account = await wallet.getAccount('Alice');

// To create an address we need to unlock stronghold.
await wallet.setStrongholdPassword(process.env.STRONGHOLD_PASSWORD);

Thoralf-M marked this conversation as resolved.
Show resolved Hide resolved
await wallet.setStrongholdPassword(
process.env.STRONGHOLD_PASSWORD as string,
);

// Sync account to make sure account is updated with outputs from previous examples
account.sync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,22 @@ require('dotenv').config({ path: '.env' });
// This example creates a new database and account.
async function run() {
initLogger();
if (!process.env.NODE_URL) {
throw new Error('.env NODE_URL is undefined, see .env.example');
}
if (!process.env.STRONGHOLD_PASSWORD) {
throw new Error(
'.env STRONGHOLD_PASSWORD is undefined, see .env.example',
);
}
if (!process.env.STRONGHOLD_SNAPSHOT_PATH) {
throw new Error(
'.env STRONGHOLD_SNAPSHOT_PATH is undefined, see .env.example',
);
}
if (!process.env.MNEMONIC) {
throw new Error('.env MNEMONIC is undefined, see .env.example');
}
if (!process.env.WALLET_DB_PATH) {
throw new Error('.env WALLET_DB_PATH is undefined, see .env.example');
}
for (const envVar of [
'NODE_URL',
'STRONGHOLD_PASSWORD',
'STRONGHOLD_SNAPSHOT_PATH',
'MNEMONIC',
'WALLET_DB_PATH',
])
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}

try {
const walletOptions: WalletOptions = {
storagePath: process.env.WALLET_DB_PATH,
clientOptions: {
nodes: [process.env.NODE_URL],
nodes: [process.env.NODE_URL as string],
},
coinType: CoinType.Shimmer,
secretManager: {
Expand All @@ -51,7 +43,7 @@ async function run() {
// A mnemonic can be generated with `Utils.generateMnemonic()`.
// Store the mnemonic in the Stronghold snapshot, this needs to be done only the first time.
// The mnemonic can't be retrieved from the Stronghold file, so make a backup in a secure place!
await wallet.storeMnemonic(process.env.MNEMONIC);
await wallet.storeMnemonic(process.env.MNEMONIC as string);

// Create a new account
const account = await wallet.createAccount({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ require('dotenv').config({ path: '.env' });
// This example creates an address
async function run() {
initLogger();
if (!process.env.WALLET_DB_PATH) {
throw new Error('.env WALLET_DB_PATH is undefined, see .env.example');
}
if (!process.env.STRONGHOLD_PASSWORD) {
throw new Error(
'.env STRONGHOLD_PASSWORD is undefined, see .env.example',
);
}
for (const envVar of ['WALLET_DB_PATH', 'STRONGHOLD_PASSWORD'])
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}

try {
const wallet = new Wallet({
storagePath: process.env.WALLET_DB_PATH,
Expand All @@ -26,7 +23,10 @@ async function run() {
const account = await wallet.getAccount('Alice');

// To create an address we need to unlock stronghold.
await wallet.setStrongholdPassword(process.env.STRONGHOLD_PASSWORD);

Thoralf-M marked this conversation as resolved.
Show resolved Hide resolved
await wallet.setStrongholdPassword(
process.env.STRONGHOLD_PASSWORD as string,
);

const address = (await account.generateEd25519Addresses(1))[0];

Expand Down
25 changes: 13 additions & 12 deletions bindings/nodejs/examples/how_tos/alias/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@ require('dotenv').config({ path: '.env' });
// In this example we create alias.
async function run() {
initLogger();
if (!process.env.FAUCET_URL) {
throw new Error('.env FAUCET_URL is undefined, see .env.example');
}
if (!process.env.WALLET_DB_PATH) {
throw new Error('.env WALLET_DB_PATH is undefined, see .env.example');
}
if (!process.env.STRONGHOLD_PASSWORD) {
throw new Error(
'.env STRONGHOLD_PASSWORD is undefined, see .env.example',
);
}
for (const envVar of [
'FAUCET_URL',
'WALLET_DB_PATH',
'STRONGHOLD_PASSWORD',
])
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}

try {
// Create the wallet
const wallet = new Wallet({
Expand All @@ -42,7 +40,10 @@ async function run() {
console.log(`Aliases BEFORE:\n`, balance.aliases);

// To sign a transaction we need to unlock stronghold.
await wallet.setStrongholdPassword(process.env.STRONGHOLD_PASSWORD);

Thoralf-M marked this conversation as resolved.
Show resolved Hide resolved
await wallet.setStrongholdPassword(
process.env.STRONGHOLD_PASSWORD as string,
);

console.log('Sending the create-alias transaction...');

Expand Down
25 changes: 13 additions & 12 deletions bindings/nodejs/examples/how_tos/alias/destroy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@ require('dotenv').config({ path: '.env' });
// In this example we destroy alias.
async function run() {
initLogger();
if (!process.env.FAUCET_URL) {
throw new Error('.env FAUCET_URL is undefined, see .env.example');
}
if (!process.env.WALLET_DB_PATH) {
throw new Error('.env WALLET_DB_PATH is undefined, see .env.example');
}
if (!process.env.STRONGHOLD_PASSWORD) {
throw new Error(
'.env STRONGHOLD_PASSWORD is undefined, see .env.example',
);
}
for (const envVar of [
'FAUCET_URL',
'WALLET_DB_PATH',
'STRONGHOLD_PASSWORD',
])
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}

try {
// Create the wallet
const wallet = new Wallet({
Expand All @@ -48,7 +46,10 @@ async function run() {
);

// To sign a transaction we need to unlock stronghold.
await wallet.setStrongholdPassword(process.env.STRONGHOLD_PASSWORD);

Thoralf-M marked this conversation as resolved.
Show resolved Hide resolved
await wallet.setStrongholdPassword(
process.env.STRONGHOLD_PASSWORD as string,
);

console.log('Sending the destroy-alias transaction...');

Expand Down
24 changes: 12 additions & 12 deletions bindings/nodejs/examples/how_tos/alias/governance-transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@ require('dotenv').config({ path: '.env' });
// In this example we will update the state controller of an alias output.
async function run() {
initLogger();
if (!process.env.FAUCET_URL) {
throw new Error('.env FAUCET_URL is undefined, see .env.example');
}
if (!process.env.WALLET_DB_PATH) {
throw new Error('.env WALLET_DB_PATH is undefined, see .env.example');
}
if (!process.env.STRONGHOLD_PASSWORD) {
throw new Error(
'.env STRONGHOLD_PASSWORD is undefined, see .env.example',
);
}
for (const envVar of [
'FAUCET_URL',
'WALLET_DB_PATH',
'STRONGHOLD_PASSWORD',
])
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}

try {
// Create the wallet
const wallet = new Wallet({
Expand All @@ -56,7 +54,9 @@ async function run() {
`Alias ${aliasId} found in unspent output: ${aliasOutputData.outputId}`,
);

await wallet.setStrongholdPassword(process.env.STRONGHOLD_PASSWORD);
await wallet.setStrongholdPassword(
process.env.STRONGHOLD_PASSWORD as string,
);

const newStateController = Utils.parseBech32Address(
(await account.generateEd25519Addresses(1))[0].address,
Expand Down
Loading
Loading