Skip to content

Commit

Permalink
Cleaning-up nearlib for release (take 1) (near#452)
Browse files Browse the repository at this point in the history
* Re-hash nearlib imports so that only one `require` is needed
* Enable eslint and apply auto fixes
* Apply manual ESLint fixes
* Rename exports.js -> browser-exports.js
* Add dev subpackage in browser library (based on https://github.com/nearprotocol/NEARStudio/pull/70/files)
* Add dist/ to repo to allow serving via jsDelivr
* window.nearLib -> window.nearlib
* nearlib.dev.initDefault -> nearlib.dev.connect
* Create user if needed when connecting to near in dev env
* Remove superagent dependency
  • Loading branch information
vgrichina authored Jan 23, 2019
1 parent 46e813a commit c96e0d7
Show file tree
Hide file tree
Showing 22 changed files with 5,746 additions and 172 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ core/wasm/**/Cargo.lock
# Python env
.env

# JS build
nearlib/dist/

# Integration tests
tmp/

Expand Down
1 change: 1 addition & 0 deletions nearlib/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
25 changes: 25 additions & 0 deletions nearlib/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
env:
es6: true
node: true
extends: 'eslint:recommended'
parserOptions:
ecmaVersion: 2018
rules:
indent:
- error
- 4
linebreak-style:
- error
- unix
quotes:
- error
- single
semi:
- error
- always
no-console: off
globals:
window: true
fetch: true
Headers: true
document: true
14 changes: 7 additions & 7 deletions nearlib/account.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const KeyPair = require("./signing/key_pair");
const KeyPair = require('./signing/key_pair');

class Account {
constructor(nearClient) {
Expand All @@ -16,9 +16,9 @@ class Account {
public_key: publicKey,
};

const transactionResponse = await this.nearClient.submitTransaction("create_account", createAccountParams);
const transactionResponse = await this.nearClient.submitTransaction('create_account', createAccountParams);
return transactionResponse;
};
}

/**
* Generate a key from a random seed and create a new account with this key.
Expand All @@ -27,14 +27,14 @@ class Account {
const keyWithRandomSeed = await KeyPair.fromRandomSeed();
const createAccountResult = await this.createAccount(
newAccountId, keyWithRandomSeed.getPublicKey(), amount, originatorAccountId);
return { key: keyWithRandomSeed, ...createAccountResult }
};
return { key: keyWithRandomSeed, ...createAccountResult };
}

/**
* Retrieves account data by plain-text account id.
*/
async viewAccount (account_id) {
return await this.nearClient.viewAccount(account_id);
};
};
}
}
module.exports = Account;
2 changes: 2 additions & 0 deletions nearlib/browser-exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
window.nearlib = require('./index');
window.nearlib.dev = require('./dev');
41 changes: 41 additions & 0 deletions nearlib/dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const nearlib = require('./');
const sendJson = require('./internal/send-json');

const localStorageAccountIdKey = 'dev_near_user';
module.exports = {
getConfig: async function() {
return JSON.parse(decodeURIComponent(getCookie('fiddleConfig')));
},
connect: async function(nodeUrl) {
const studioConfig = await this.getConfig();
const near = nearlib.Near.createDefaultConfig(nodeUrl || studioConfig.nodeUrl);
await this.getOrCreateDevUser();
return near;
},
getOrCreateDevUser: async function () {
let tempUserAccountId = window.localStorage.getItem(localStorageAccountIdKey);
if (tempUserAccountId) {
return tempUserAccountId;
}

tempUserAccountId = 'devuser' + Date.now();
const keypair = await nearlib.KeyPair.fromRandomSeed();
const nearConfig = await this.getConfig();
await sendJson('POST', `${nearConfig.baseUrl}/account`, {
newAccountId: tempUserAccountId,
newAccountPublicKey: keypair.getPublicKey()
});
const keyStore = new nearlib.BrowserLocalStorageKeystore();
keyStore.setKey(tempUserAccountId, keypair);
window.localStorage.setItem(localStorageAccountIdKey, tempUserAccountId);
return tempUserAccountId;
},
get myAccountId() {
return window.localStorage.getItem(localStorageAccountIdKey);
}
};

function getCookie(name) {
var v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
return v ? v[2] : null;
}
Loading

0 comments on commit c96e0d7

Please sign in to comment.