import {getDB} from '../db/db';
import {createAccount, createAccountReceiver} from '../wasm/func';
-import {handleAxiosError} from '../utils/http';
+import {handleApiError, handleAxiosError} from '../utils/http';
-function accountsSDK(http){
- this.http = http;
+
+function accountsSDK(bytom){
+ this.http = bytom.serverHttp;
+ this.bytom = bytom;
}
/**
* List of the account.
*
- * @returns {Promise}
+ * @returns {Promise} List of Accounts
*/
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');
- let objectStore = transaction.objectStore('accounts-server');
- let oc = objectStore.openCursor();
+ let objectStore = transaction.objectStore('accounts-server').index('net');
+ let keyRange = IDBKeyRange.only(net);
+ let oc = objectStore.openCursor(keyRange);
let ret = [];
oc.onsuccess = function (event) {
var cursor = event.target.result;
*
* @see https://gist.github.com/HAOYUatHZ/0c7446b8f33e7cddd590256b3824b08f#apiv1btmaccountlist-addresses
* @param {String} guid
- * @returns
+ * @returns {Promise} list of all addresses
*/
accountsSDK.prototype.listAddressUseServer = function(guid) {
+ let net = this.bytom.net;
let retPromise = new Promise((resolve, reject) => {
- this.http.request('account/list-addresses', {guid:guid}).then(resp => {
- resolve(resp.data.data.addresses);
+ this.http.request('account/list-addresses', {guid:guid}, net).then(resp => {
+ resolve(resp.data.result.data);
}).catch(error => {
reject(handleAxiosError(error));
});
* @returns {Promise}
*/
accountsSDK.prototype.createAccountReceiverUseServer = function(guid, label) {
+ let net = this.bytom.net;
let retPromise = new Promise((resolve, reject) => {
let pm = {guid: guid};
if (label) {
pm.label = label;
}
- this.http.request('account/new-address', pm).then(resp => {
- let dbData = resp.data.data;
+ this.http.request('account/new-address', pm, net).then(resp => {
+ if (resp.status !== 200 || resp.data.code !== 200) {
+ reject(handleApiError(resp));
+ return;
+ }
+ let dbData = resp.data.result.data;
dbData.guid = guid;
+ dbData.net = net;
getDB().then(db => {
let transaction = db.transaction(['addresses-server'], 'readwrite');
let objectStore = transaction.objectStore('addresses-server');
* @returns {Promise}
*/
accountsSDK.prototype.createAccountUseServer = function(rootXPub, alias, label) {
+ let net = this.bytom.net;
let that = this;
let retPromise = new Promise((resolve, reject) => {
getDB().then(db => {
if (label) {
pm.label = label;
}
- that.http.request('account/create', pm).then(resp => {
- let dbData = resp.data.data;
+ that.http.request('account/create', pm, net).then(resp => {
+ if (resp.status !== 200 || resp.data.code !== 200) {
+ reject(handleApiError(resp));
+ return;
+ }
+ let dbData = resp.data.result.data;
dbData.rootXPub = rootXPub;
dbData.alias = alias;
+ dbData.net = net;
getDB().then(db => {
let transaction = db.transaction(['accounts-server'], 'readwrite');
let objectStore = transaction.objectStore('accounts-server');
*
* @param {String} alias
* @param {Int} quorum
- * @param {String} rootXPub
+ * @param {String} rootXPub
+ * @returns {Promise}
*/
accountsSDK.prototype.createAccount = function(alias, quorum, rootXPub) {
let retPromise = new Promise((resolve, reject) => {
* create account address
*
* @param {Object} account createAccount return account Object val
- * @param {Int} nextIndex
+ * @param {Int} nextIndex
+ * @returns {Promise}
*/
accountsSDK.prototype.createAccountReceiver = function(account) {
let retPromise = new Promise((resolve, reject) => {