import keysSDK from './sdk/keys.js';
import accountsSDK from './sdk/accounts.js';
import transactionSDK from './sdk/transaction.js';
-import http from './http.js';
+import {serverHttp, http} from './http.js';
//todo vue use
function Bytom(serverHost, wasmPath, baseURL, token) {
if(baseURL) {
this.http = new http(baseURL, token);
-
this.keys = new keysApi(this.http);
this.accounts = new accountsApi(this.http);
this.transactions = new transactionsApi(this.http);
}
Bytom.wasmPath = wasmPath;
+ this.serverHttp = new serverHttp(serverHost);
this.sdk = {};
this.sdk.keys = new keysSDK();
- this.sdk.accounts = new accountsSDK();
+ this.sdk.accounts = new accountsSDK(this.serverHttp);
this.sdk.transaction = new transactionSDK();
}
import {getDB} from '../db/db';
import {createAccount, createAccountReceiver} from '../wasm/func';
-function accountsSDK(){
+function accountsSDK(http){
+ this.http = http;
}
+accountsSDK.prototype.createAccountReceiverUseServer = function(guid) {
+ let retPromise = new Promise((resolve, reject) => {
+ this.http.request('account/new-address', {guid:guid}).then(resp => {
+ let dbData = resp.data.data;
+ dbData.guid = guid;
+ getDB().then(db => {
+ let transaction = db.transaction(['addresses-server'], 'readwrite');
+ let objectStore = transaction.objectStore('addresses-server');
+ delete dbData.rootXPub;
+ let request = objectStore.add(dbData);
+ request.onsuccess = function() {
+ resolve(dbData);
+ };
+ request.onerror = function() {
+ reject(request.error);
+ };
+ });
+ }).catch(error => {
+ reject(error);
+ });
+ });
+ return retPromise;
+};
+
+accountsSDK.prototype.createAccountUseServer = function(rootXPub) {
+ let retPromise = new Promise((resolve, reject) => {
+ this.http.request('account/create', {pubkey: rootXPub}).then(resp => {
+ let dbData = resp.data.data;
+ dbData.rootXPub = rootXPub;
+ getDB().then(db => {
+ let transaction = db.transaction(['accounts-server'], 'readwrite');
+ let objectStore = transaction.objectStore('accounts-server');
+ let request = objectStore.add(dbData);
+ request.onsuccess = function() {
+ let transaction = db.transaction(['addresses-server'], 'readwrite');
+ let objectStore = transaction.objectStore('addresses-server');
+ delete dbData.rootXPub;
+ let request = objectStore.add(dbData);
+ request.onsuccess = function() {
+ resolve(dbData);
+ };
+ request.onerror = function() {
+ reject(request.error);
+ };
+ };
+ request.onerror = function() {
+ reject(request.error);
+ };
+ });
+ }).catch(error => {
+ reject(error);
+ });
+ });
+ return retPromise;
+};
+
/**
* create account
*
* @param {String} rootXPub
*/
accountsSDK.prototype.createAccount = function(alias, quorum, rootXPub) {
- let returnPromise = new Promise((resolve, reject) => {
+ let retPromise = new Promise((resolve, reject) => {
getDB().then(db => {
let transaction = db.transaction(['accounts'], 'readwrite');
let objectStore = transaction.objectStore('accounts');
request.onsuccess = function () {
let data = {alias:alias, quorum:quorum, rootXPub:rootXPub, nextIndex:request.result};
createAccount(data).then(res => {
- let JsonData = JSON.parse(res.data);
+ let jsonData = JSON.parse(res.data);
let putTransaction = db.transaction(['accounts'], 'readwrite');
let putObjectStore = putTransaction.objectStore('accounts');
- let putRequest = putObjectStore.put(JsonData, request.result);
+ let putRequest = putObjectStore.put(jsonData, request.result);
putRequest.onsuccess = function() {
- resolve(JsonData);
+ resolve(jsonData);
};
putRequest.onerror = function() {
- resolve(putRequest.error);
+ reject(putRequest.error);
};
}).catch(error => {
reject(error);
reject(error);
});
});
- return returnPromise;
+ return retPromise;
};
/**
* @param {Int} nextIndex
*/
accountsSDK.prototype.createAccountReceiver = function(account) {
- let returnPromise = new Promise((resolve, reject) => {
+ let retPromise = new Promise((resolve, reject) => {
getDB().then(db => {
let transaction = db.transaction(['addresses'], 'readwrite');
let objectStore = transaction.objectStore('addresses');
request.onsuccess = function() {
let data = {account:JSON.stringify(account), nextIndex: request.result};
createAccountReceiver(data).then(res => {
- let JsonData = JSON.parse(res.data);
- let JsonDB = JSON.parse(res.db);
+ let jsonData = JSON.parse(res.data);
+ let jsonDB = JSON.parse(res.db);
let putTransaction = db.transaction(['addresses'], 'readwrite');
let putObjectStore = putTransaction.objectStore('addresses');
let putRequest = null;
- for (let key in JsonDB) {
- if(!JsonDB.hasOwnProperty(key)) continue;
- let putData = JSON.parse(JsonDB[key]);
+ for (let key in jsonDB) {
+ if(!jsonDB.hasOwnProperty(key)) continue;
+ let putData = JSON.parse(jsonDB[key]);
putRequest = putObjectStore.put(putData, request.result);
}
putRequest.onsuccess = function() {
- resolve(JsonData);
+ resolve(jsonData);
};
putRequest.onerror = function() {
- resolve(putRequest.error);
+ reject(putRequest.error);
};
}).catch(error => {
reject(error);
reject(error);
});
});
- return returnPromise;
+ return retPromise;
};
export default accountsSDK;
\ No newline at end of file
* @param {String} newPassword
*/
keysSDK.prototype.resetKeyPassword = function(rootXPub, oldPassword, newPassword) {
- let returnPromise = new Promise((resolve, reject) => {
+ let retPromise = new Promise((resolve, reject) => {
let data = {rootXPub: rootXPub, oldPassword:oldPassword, newPassword:newPassword};
resetKeyPassword(data).then(res => {
getDB().then(db => {
reject(error);
});
});
- return returnPromise;
+ return retPromise;
};
/**
* @param {String} xpub
*/
keysSDK.prototype.getKeyByXPub = function(xpub) {
- let returnPromise = new Promise((resolve, reject) => {
+ let retPromise = new Promise((resolve, reject) => {
getDB().then(db => {
let getRequest = db.transaction(['keys'], 'readonly')
.objectStore('keys')
reject(error);
});
});
- return returnPromise;
+ return retPromise;
};
/**
*/
keysSDK.prototype.create = function(alias, password) {
var normalizedAlias = alias.toLowerCase().trim();
- let returnPromise = new Promise((resolve, reject) => {
+ let retPromise = new Promise((resolve, reject) => {
getDB().then(db => {
let getRequest = db.transaction(['keys'], 'readonly')
.objectStore('keys')
reject(error);
});
});
- return returnPromise;
+ return retPromise;
};
export default keysSDK;
\ No newline at end of file