Skip to content

Commit

Permalink
Make sure that webpack build doesn’t end up requiring fs
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrichina committed Feb 10, 2021
1 parent ca81785 commit be3fc8b
Show file tree
Hide file tree
Showing 21 changed files with 125 additions and 84 deletions.
8 changes: 8 additions & 0 deletions lib/browser-connect.d.ts

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

11 changes: 11 additions & 0 deletions lib/browser-connect.js

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

1 change: 1 addition & 0 deletions lib/browser-index.d.ts

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

1 change: 1 addition & 0 deletions lib/browser-index.js

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

4 changes: 2 additions & 2 deletions lib/common-index.d.ts

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

3 changes: 1 addition & 2 deletions lib/common-index.js

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

8 changes: 8 additions & 0 deletions lib/connect.d.ts

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

33 changes: 33 additions & 0 deletions lib/connect.js

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

1 change: 1 addition & 0 deletions lib/index.d.ts

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

1 change: 1 addition & 0 deletions lib/index.js

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

10 changes: 1 addition & 9 deletions lib/near.d.ts

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

31 changes: 1 addition & 30 deletions lib/near.js

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

2 changes: 1 addition & 1 deletion lib/signer.d.ts

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

4 changes: 2 additions & 2 deletions lib/signer.js

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

12 changes: 12 additions & 0 deletions src/browser-connect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Near, NearConfig } from './near';

export type ConnectConfig = NearConfig & {
keyPath?: string;
}

/**
* Initialize connection to Near network.
*/
export async function connect(config: ConnectConfig): Promise<Near> {
return new Near(config);
}
1 change: 1 addition & 0 deletions src/browser-index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * as keyStores from './key_stores/browser-index';
export * from './common-index';
export * from './browser-connect';

import 'error-polyfill';
3 changes: 1 addition & 2 deletions src/common-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Connection } from './connection';
import { Signer, InMemorySigner } from './signer';
import { Contract } from './contract';
import { KeyPair } from './utils/key_pair';
import { connect, Near } from './near';
import { Near } from './near';

// TODO: Deprecate and remove WalletAccount
import { WalletAccount, WalletConnection } from './wallet-account';
Expand All @@ -30,7 +30,6 @@ export {
Signer,
KeyPair,

connect,
Near,

WalletAccount,
Expand Down
33 changes: 33 additions & 0 deletions src/connect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { readKeyFile } from './key_stores/unencrypted_file_system_keystore';
import { InMemoryKeyStore, MergeKeyStore } from './key_stores';
import { Near, NearConfig } from './near';

export type ConnectConfig = NearConfig & {
keyPath?: string;
}

/**
* Initialize connection to Near network.
*/
export async function connect(config: ConnectConfig): Promise<Near> {
// Try to find extra key in `KeyPath` if provided.
if (config.keyPath && config.deps && config.deps.keyStore) {
try {
const accountKeyFile = await readKeyFile(config.keyPath);
if (accountKeyFile[0]) {
// TODO: Only load key if network ID matches
const keyPair = accountKeyFile[1];
const keyPathStore = new InMemoryKeyStore();
await keyPathStore.setKey(config.networkId, accountKeyFile[0], keyPair);
if (!config.masterAccount) {
config.masterAccount = accountKeyFile[0];
}
config.deps.keyStore = new MergeKeyStore([config.deps.keyStore, keyPathStore]);
console.log(`Loaded master account ${accountKeyFile[0]} key from ${config.keyPath} with public key = ${keyPair.getPublicKey()}`);
}
} catch (error) {
console.warn(`Failed to load master account key from ${config.keyPath}: ${error}`);
}
}
return new Near(config);
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * as keyStores from './key_stores/index';
export * from './common-index';
export * from './common-index';
export * from './connect';
36 changes: 2 additions & 34 deletions src/near.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@

import BN from 'bn.js';
import { Account } from './account';
import { Connection } from './connection';
import { Signer } from './signer';
import { Contract } from './contract';
import { readKeyFile } from './key_stores/unencrypted_file_system_keystore';
import { PublicKey } from './utils/key_pair';
import { AccountCreator, LocalAccountCreator, UrlAccountCreator } from './account_creator';
import { InMemoryKeyStore, KeyStore, MergeKeyStore } from './key_stores';
import { KeyStore } from './key_stores';

type NearConfig = {
export type NearConfig = {
keyStore?: KeyStore;
signer?: Signer;
deps?: { keyStore: KeyStore };
Expand Down Expand Up @@ -91,33 +89,3 @@ export class Near {
return result.transaction_outcome.id;
}
}

type ConnectConfig = NearConfig & {
keyPath?: string;
}

/**
* Initialize connection to Near network.
*/
export async function connect(config: ConnectConfig): Promise<Near> {
// Try to find extra key in `KeyPath` if provided.
if (config.keyPath && config.deps && config.deps.keyStore) {
try {
const accountKeyFile = await readKeyFile(config.keyPath);
if (accountKeyFile[0]) {
// TODO: Only load key if network ID matches
const keyPair = accountKeyFile[1];
const keyPathStore = new InMemoryKeyStore();
await keyPathStore.setKey(config.networkId, accountKeyFile[0], keyPair);
if (!config.masterAccount) {
config.masterAccount = accountKeyFile[0];
}
config.deps.keyStore = new MergeKeyStore([config.deps.keyStore, keyPathStore]);
console.log(`Loaded master account ${accountKeyFile[0]} key from ${config.keyPath} with public key = ${keyPair.getPublicKey()}`);
}
} catch (error) {
console.warn(`Failed to load master account key from ${config.keyPath}: ${error}`);
}
}
return new Near(config);
}
3 changes: 2 additions & 1 deletion src/signer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sha256 from 'js-sha256';
import { Signature, KeyPair, PublicKey } from './utils/key_pair';
import { KeyStore, InMemoryKeyStore } from './key_stores';
import { KeyStore } from './key_stores/keystore';
import { InMemoryKeyStore } from './key_stores/in_memory_key_store';

/**
* General signing interface, can be used for in memory signing, RPC singing, external wallet, HSM, etc.
Expand Down

0 comments on commit be3fc8b

Please sign in to comment.