OSDN Git Service

change net select api (⊙ˍ⊙)
authorxuhongxin <xuhongxin@luojilab.com>
Sun, 14 Oct 2018 10:57:54 +0000 (18:57 +0800)
committerxuhongxin <xuhongxin@luojilab.com>
Sun, 14 Oct 2018 10:57:54 +0000 (18:57 +0800)
README.md
package.json
src/http.js
src/index.js
src/sdk/accounts.js
src/sdk/transaction.js

index c093dbf..0864360 100644 (file)
--- a/README.md
+++ b/README.md
@@ -11,11 +11,15 @@ npm install bytom-js-sdk
 
 ```javascript
 let net = {
-    main : "http://main-net-host/",
+    main: "http://main-net-host/",
     test: "http://test-net-host/"
 };
 let bytom = new Bytom(net, chrome.runtime.getURL("main.wasm"));
 
+//set Bytom net type (main test), default main net.
+bytom.setNetType('test');
+console.log(bytom.getNetType());
+
 //create key
 bytom.sdk.keys.create("test_alias", "123456").then((res)=>{
     console.log(res)
index 4d190e0..a8a256e 100644 (file)
@@ -1,6 +1,6 @@
 {
   "name": "bytom-js-sdk",
-  "version": "1.3.0",
+  "version": "1.3.1",
   "description": "Bytom JS SDK",
   "main": "src/index.js",
   "keywords" :[
index 15e1ed5..73ce929 100644 (file)
@@ -12,7 +12,7 @@ export function serverHttp(host) {
                 Accept: 'application/json',
             },
             data: body,
-            timeout: 1000
+            timeout: 2000
         };
 
         //return Promise
index 7743c9d..e1eb762 100644 (file)
@@ -19,14 +19,33 @@ function Bytom(serverHost, wasmPath, baseURL, token) {
         this.accounts = new accountsApi(this.http);
         this.transactions = new transactionsApi(this.http);
     }
-
+    this.net = 'main';
     Bytom.wasmPath = wasmPath;
     this.serverHttp = new serverHttp(serverHost);
     this.sdk = {};
     this.sdk.keys = new keysSDK();
-    this.sdk.accounts = new accountsSDK(this.serverHttp);
-    this.sdk.transaction = new transactionSDK(this.serverHttp);
+    this.sdk.accounts = new accountsSDK(this);
+    this.sdk.transaction = new transactionSDK(this);
     this.sdk.wallet = new walletSDK(this);
 }
 
+
+/**
+ * Get net type;
+ *
+ * @returns
+ */
+Bytom.prototype.getNetType = function() {
+    return this.net;
+};
+
+/**
+ * Set net type
+ *
+ * @param {String} net net type (main test)
+ */
+Bytom.prototype.setNetType = function(net) {
+    this.net = net;
+};
+
 export default Bytom;
\ No newline at end of file
index 843770f..4b6758d 100644 (file)
@@ -2,20 +2,18 @@ import {getDB} from '../db/db';
 import {createAccount, createAccountReceiver} from '../wasm/func';
 import {handleAxiosError} from '../utils/http';
 
-function accountsSDK(http){
-    this.http = http;
+function accountsSDK(bytom){
+    this.http = bytom.serverHttp;
+    this.bytom = bytom;
 }
 
 /**
  * List of the account.
  *
- * @param {String} net list select net
  * @returns {Promise}
  */
-accountsSDK.prototype.listAccountUseServer = function(net) {
-    if (!net) {
-        net = 'main';
-    }
+accountsSDK.prototype.listAccountUseServer = function() {
+    let net = this.bytom.net;
     let retPromise = new Promise((resolve, reject) => {
         getDB().then(db => {
             let transaction = db.transaction(['accounts-server'], 'readonly');
@@ -48,13 +46,10 @@ accountsSDK.prototype.listAccountUseServer = function(net) {
  *
  * @see https://gist.github.com/HAOYUatHZ/0c7446b8f33e7cddd590256b3824b08f#apiv1btmaccountlist-addresses
  * @param {String} guid
- * @param {String} net server net
  * @returns
  */
-accountsSDK.prototype.listAddressUseServer = function(guid, net) {
-    if (!net) {
-        net = 'main';
-    }
+accountsSDK.prototype.listAddressUseServer = function(guid) {
+    let net = this.bytom.net;
     let retPromise = new Promise((resolve, reject) => {
         this.http.request('account/list-addresses', {guid:guid}, net).then(resp => {
             resolve(resp.data.data.addresses);
@@ -70,13 +65,10 @@ accountsSDK.prototype.listAddressUseServer = function(guid, net) {
  * @see https://gist.github.com/HAOYUatHZ/0c7446b8f33e7cddd590256b3824b08f#apiv1btmaccountnew-address
  * @param {String} guid unique id for each wallet
  * @param {String} label alias for the address to be created
- * @param {String} net address net
  * @returns {Promise}
  */
-accountsSDK.prototype.createAccountReceiverUseServer = function(guid, label, net) {
-    if (!net) {
-        net = 'main';
-    }
+accountsSDK.prototype.createAccountReceiverUseServer = function(guid, label) {
+    let net = this.bytom.net;
     let retPromise = new Promise((resolve, reject) => {
         let pm = {guid: guid};
         if (label) {
@@ -112,13 +104,10 @@ accountsSDK.prototype.createAccountReceiverUseServer = function(guid, label, net
  * @param {String} rootXPub
  * @param {String} alias alias for the account
  * @param {String} label alias for the first address
- * @param {String} net account net
  * @returns {Promise}
  */
-accountsSDK.prototype.createAccountUseServer = function(rootXPub, alias, label, net) {
-    if (!net) {
-        net = 'main';
-    }
+accountsSDK.prototype.createAccountUseServer = function(rootXPub, alias, label) {
+    let net = this.bytom.net;
     let that = this;
     let retPromise = new Promise((resolve, reject) => {
         getDB().then(db => {
index 03a571a..d0cabfb 100644 (file)
@@ -1,8 +1,9 @@
 import {signTransaction} from '../wasm/func';
 import {handleAxiosError} from '../utils/http';
 
-function transactionSDK(http) {
-    this.http = http;
+function transactionSDK(bytom) {
+    this.http = bytom.serverHttp;
+    this.bytom = bytom;
 }
 
 
@@ -15,12 +16,13 @@ function transactionSDK(http) {
  * @returns {Promise}
  */
 transactionSDK.prototype.list = function(guid, address) {
+    let net = this.bytom.net;
     let retPromise = new Promise((resolve, reject) => {
         let pm = {guid: guid};
         if (address) {
             pm.address = address;
         }
-        this.http.request('merchant/list-transactions', pm).then(resp => {
+        this.http.request('merchant/list-transactions', pm, net).then(resp => {
             resolve(resp.data);
         }).catch(err => {
             reject(handleAxiosError(err));
@@ -38,9 +40,10 @@ transactionSDK.prototype.list = function(guid, address) {
  * @param {Array} signatures signed data of each signing instruction
  */
 transactionSDK.prototype.submitPayment = function(guid, raw_transaction, signatures) {
+    let net = this.bytom.net;
     let retPromise = new Promise((resolve, reject) => {
         let pm = {guid: guid, raw_transaction: raw_transaction, signatures: signatures};
-        this.http.request('merchant/submit-payment', pm).then(resp => {
+        this.http.request('merchant/submit-payment', pm, net).then(resp => {
             resolve(resp.data);
         }).catch(err => {
             reject(handleAxiosError(err));
@@ -63,6 +66,7 @@ transactionSDK.prototype.submitPayment = function(guid, raw_transaction, signatu
  * @returns {Promise}
  */
 transactionSDK.prototype.buildPayment = function(guid, to, asset, amount, from, fee) {
+    let net = this.bytom.net;
     let retPromise = new Promise((resolve, reject) => {
         let pm = {guid: guid, to: to, asset: asset, amount: amount};
         if (from) {
@@ -71,7 +75,7 @@ transactionSDK.prototype.buildPayment = function(guid, to, asset, amount, from,
         if (fee) {
             pm.fee = fee;
         }
-        this.http.request('merchant/build-payment', pm).then(resp => {
+        this.http.request('merchant/build-payment', pm, net).then(resp => {
             resolve(resp.data);
         }).catch(err => {
             reject(handleAxiosError(err));