OSDN Git Service

update the JS SDK API
authorZhiting Lin <zlin035@uottawa.ca>
Wed, 8 May 2019 05:51:24 +0000 (13:51 +0800)
committerZhiting Lin <zlin035@uottawa.ca>
Wed, 8 May 2019 05:51:24 +0000 (13:51 +0800)
src/api/keys.js [new file with mode: 0644]
src/api/transactions.js
src/http.js
src/index.js
src/sdk/accounts.js
src/sdk/keys.js
src/sdk/query.js
src/sdk/transaction.js
src/sdk/wallet.js

diff --git a/src/api/keys.js b/src/api/keys.js
new file mode 100644 (file)
index 0000000..585061a
--- /dev/null
@@ -0,0 +1,93 @@
+import {createKey, resetKeyPassword, createPubkey} from '../wasm/func';
+
+/**
+ * Represents a keysApi.
+ * @constructor
+ */
+function keysApi(http) {
+    this.http = http;
+}
+
+/**
+ * Create a new key, will generate the xpub.
+ *
+ * @param {Object} params - Parameters for accounts create.
+ * @param {String} params.alias - key alias,
+ * @param {String} params.password  - key Password.
+ * @returns {Promise} key alias, key xpub
+ */
+keysApi.prototype.create = function(params) {
+    let inputParams = params
+    inputParams.auth = params.password
+    delete(inputParams['password'])
+    let promise = new Promise((resolve, reject) => {
+        createKey(inputParams).then(resp =>{
+            let jsonData = JSON.parse(resp.data);
+            const result = {
+                alias: jsonData.alias,
+                xpub: jsonData.xpub,
+                crypto: jsonData.crypto
+            };
+            resolve(result);
+        }).catch(error => {
+            reject(error);
+        });
+    });
+
+    return promise;
+};
+
+/**
+ * Reset key password, will generate the new xpub.
+ *
+ * @param {Object} params - Parameters for accounts create.
+ * @param {String} params.xpub - key alias,
+ * @param {String} params.oldPassword  - key oldPassword.
+ * @param {String} params.newPassword  - key newPassword.
+ * @param {Object} params.crypto - key crypto
+ * @returns {Promise} key alias, key xpub
+ */
+keysApi.prototype.resetKeyPassword = function(params) {
+    let inputParams = params
+    inputParams.rootXPub = params.xpub
+    delete(inputParams['xpub'])
+    let promise = new Promise((resolve, reject) => {
+        resetKeyPassword(inputParams).then(resp =>{
+            let jsonData = JSON.parse(resp.data);
+            const result = {
+                alias: jsonData.alias,
+                xpub: jsonData.xpub,
+                crypto: jsonData.crypto
+            };
+            resolve(result);
+        }).catch(error => {
+            reject(error);
+        });
+    });
+
+    return promise;
+};
+
+/**
+ * Create a new key.
+ *
+ * @param {String} xpub - xpub.
+ * @returns {Promise} xpub, pubkey, derived_path
+ *
+ */
+keysApi.prototype.createPubkey = function(xpub) {
+    let retPromise = new Promise((resolve, reject) => {
+        let data = {};
+        data.xpub = xpub;
+        data.seed = 1;
+        createPubkey(data).then((resp) => {
+            let jsonData = JSON.parse(resp.data);
+            resolve(jsonData);
+        }).catch(error => {
+            reject(error);
+        });
+    });
+    return retPromise;
+};
+
+export default keysApi;
\ No newline at end of file
index f0ea0c9..9d77242 100644 (file)
@@ -1,9 +1,9 @@
+import {convertArguements} from '../utils/convertArguement';
+
 /**
  * Represents a transactionsApi.
  * @constructor
  */
-import {convertArguements} from '../utils/convertArguement';
-
 function transactionsApi(http) {
     this.http = http;
 }
@@ -59,7 +59,8 @@ transactionsApi.prototype.buildTransaction = function(params) {
  *
  * @param {Object} obj - Argument Object
  * @param {String} obj.type - Argument Type, included data, address, string, integer, boolean.
- * @param {String|Number|Boolean}- obj.raw_data.value - data value
+ * @param {Object} obj.raw_data - Object Raw Data
+ * @param {String|Number|Boolean} obj.raw_data.value - data value
  */
 transactionsApi.prototype.convertArguement = function (obj) {
     return convertArguements(obj);
index 9736a75..a21d1a0 100644 (file)
@@ -1,5 +1,5 @@
 import axios from 'axios';
-import {handleApiError} from "./utils/http";
+import {handleApiError} from './utils/http';
 
 const basePath = 'api/v1/btm/';
 
index 3cb2143..692bd01 100644 (file)
@@ -1,6 +1,7 @@
 import queryApi from './api/query.js';
 import accountsApi from './api/accounts.js';
 import transactionsApi from './api/transactions.js';
+import keysApi from './api/keys.js';
 import keysSDK from './sdk/keys.js';
 import accountsSDK from './sdk/accounts.js';
 import transactionSDK from './sdk/transaction.js';
@@ -17,6 +18,7 @@ function Bytom(serverHost, wasmPath, baseURL) {
         this.http = new http(baseURL);
         this.query = new queryApi(this.http);
         this.accounts = new accountsApi(this.http);
+        this.keys = new keysApi(this.http);
         this.transactions = new transactionsApi(this.http);
     }
 
index 83dcb96..e8c9ef5 100644 (file)
@@ -2,10 +2,7 @@ import {getDB} from '../db/db';
 import {createAccount, createAccountReceiver} from '../wasm/func';
 import {handleApiError, handleAxiosError} from '../utils/http';
 
-/**
- * Represents a accountSDK.
- * @constructor
- */
+
 function accountsSDK(bytom){
     this.http = bytom.serverHttp;
     this.bytom = bytom;
index e416299..26922ce 100644 (file)
@@ -2,10 +2,6 @@ import {createKey, resetKeyPassword, createPubkey} from '../wasm/func';
 import {getDB} from '../db/db';
 
 
-/**
- * Represents a keysSDK.
- * @constructor
- */
 function keysSDK() {
 }
 
index e8ed957..22345c5 100644 (file)
@@ -1,9 +1,5 @@
 import { handleAxiosError } from '../utils/http';
 
-/**
- * Represents a querySDK.
- * @constructor
- */
 function querySDK(bytom) {
     this.bytom =bytom; 
     this.http = bytom.serverHttp;
index d65a265..8a35eb9 100644 (file)
@@ -3,10 +3,6 @@ import { handleApiError, handleAxiosError } from '../utils/http';
 import { getDB } from '../db/db';
 import keysSDK from "./keys";
 
-/**
- * Represents a transactionSDK.
- * @constructor
- */
 function transactionSDK(bytom) {
     this.http = bytom.serverHttp;
     this.bytom = bytom;
@@ -82,20 +78,20 @@ transactionSDK.prototype.submitPayment = function(guid, raw_transaction, signatu
  * @param {String} to destination address
  * @param {String} asset hexdecimal asset id
  * @param {Number} amount transfer amount
- * @param {String} from source address
  * @param {Number} fee transaction fee amount
+ * @param {Number} confirmations - transaction confirmations
  * @returns {Promise}
  */
-transactionSDK.prototype.buildPayment = function(guid, to, asset, amount, from, fee) {
+transactionSDK.prototype.buildPayment = function(guid, to, asset, amount, fee, confirmations) {
     let net = this.bytom.net;
     let retPromise = new Promise((resolve, reject) => {
         let pm = {guid: guid, to: to, asset: asset, amount: amount};
-        if (from) {
-            pm.from = from;
-        }
         if (fee) {
             pm.fee = fee;
         }
+        if (confirmations) {
+            pm.confirmations = confirmations;
+        }
         this.http.request('merchant/build-payment', pm, net).then(resp => {
             if (resp.status !== 200 || resp.data.code !== 200) {
                 reject(handleApiError(resp));
index e6f87ec..19292a2 100644 (file)
@@ -1,9 +1,5 @@
 import {initDB, getDB} from '../db/db';
 
-/**
- * Represents a transactionSDK.
- * @constructor
- */
 function walletSDK(bytom) {
     this.bytom = bytom;
 }