/**
* Create a wallet using a public key. Each wallet is identified by a guid. (by server)
+ *
+ * @see https://gist.github.com/HAOYUatHZ/0c7446b8f33e7cddd590256b3824b08f#endpoints
+ * @param {String} rootXPub
+ * @param {String} alias alias for the account
+ * @param {String} label alias for the first address
+ * @returns {Promise}
+ */
+accountsSDK.prototype.createNewAccount = function(rootXPub, label) {
+ let net = this.bytom.net;
+ let pm = {pubkey: rootXPub};
+ if (label) {
+ pm.label = label;
+ }
+ return this.http.request('account/create', pm, net);
+};
+
+
+/**
+ * Create a wallet using a public key. Each wallet is identified by a guid. (by server)
*
* @see https://gist.github.com/HAOYUatHZ/0c7446b8f33e7cddd590256b3824b08f#endpoints
* @param {String} rootXPub
import { createKey ,resetKeyPassword, createPubkey, signMessage, signTransaction} from '../wasm/func';
import {getDB} from '../db/db';
-import {createkey} from '../utils/key/createKey';
+import {createkey, isValidMnemonic} from '../utils/key/createKey';
import {encryptKey, decryptKey} from '../utils/key/keystore';
import { restoreFromKeyStore } from '../utils/account';
const res = createkey(data);
- const xpub = res.xpub;
+ return res;
+};
+
+/**
+ * Create a new key.
+ *
+ * @param {String} alias - User specified, unique identifier.
+ * @param {String} password - User specified, key password.
+ */
+keysSDK.prototype.restoreFromKeystore = function( password, keystore) {
- //Todo: /account/wallets api find if xpub exist in the blockcenter, yes restore, otherwise create new account
+ const walletImage = JSON.parse(keystore);
- return res;
+ let keys, key;
+ if(walletImage.key_images && walletImage.key_images.xkeys ){
+ keys = walletImage.key_images.xkeys;
+ }
+
+ // match older version of backups keystore files
+ else if(walletImage.keys>0){
+ keys = walletImage.keys.map(keyItem => JSON.parse( keyItem.key ) );
+ }else{
+ key = walletImage;
+ }
+
+ if(keys.length>1){
+ throw 'do not support multiple keystore imported.';
+ }
+ else if(keys.length === 1){
+ key = keys[0];
+ }
+
+ const result = decryptKey(key, password);
+
+ return result;
+};
+
+/**
+ * Create a new key.
+ *
+ * @param {String} alias - User specified, unique identifier.
+ * @param {String} password - User specified, key password.
+ */
+keysSDK.prototype.isValidMnemonic = function(mnemonic) {
+
+ return isValidMnemonic(mnemonic);
};
/**
import {initDB, getDB} from '../db/db';
import { restoreFromKeyStore } from '../utils/account';
+import accountsSDK from './accounts';
function walletSDK(bytom) {
this.http = bytom.serverHttp;
return retPromise;
};
+walletSDK.prototype.list = function(pubkey) {
+ let net = this.bytom.net;
+ let pm = {pubkey};
+ return this.http.request('account/wallets', pm, net);
+};
+
/**
* Restore wallet.
*
return {alias: obj.alias, xpub: obj.xPub.toString('hex'), keystore: obj.keystore, mnemonic:obj.mnemonic};
}
-function importKeyFromMnemonic(alias, password, mnemonic, language) {
+function isValidMnemonic(mnemonic, language){
// checksum length = entropy length /32
// mnemonic length = (entropy length + checksum length)/11
let mnemArray = mnemonic.trim().split(' ');
if (!bip39.validateMnemonic(mnemonic, WORDLISTS[language])) {
throw 'mnemonic is invalid';
}
+}
+
+function importKeyFromMnemonic(alias, password, mnemonic, language) {
+ isValidMnemonic(mnemonic, language)
return createKeyFromMnemonic(alias, password, mnemonic);
}
let mnemonic = bip39.generateMnemonic(EntropyLength,undefined, WORDLISTS[language]);
let object = createKeyFromMnemonic(alias, password, mnemonic);
- object.mnemonic = mnemonic
+ object.mnemonic = mnemonic;
return object;
}
export {
createkey,
importKeyFromMnemonic,
- createNewKey
+ createNewKey,
+ isValidMnemonic
};