scientistnik / btsdex

Package for work with BitShares DEX
MIT License
21 stars 27 forks source link

Account created with faucet with an custom password are not working with btsdex #18

Closed qualwebs closed 2 years ago

qualwebs commented 5 years ago

Hello Nik,

The account I'm creating with the faucet is unable to login with BTSDEX lib. It always returns exception of the invalid pair of username and password.

Below is the code snippet: `var keys = BitShares.generateKeys(username,password,["owner", "active", "memo"],'AIPR'); var activePubKey = keys.pubKeys.active; var ownerPubKey = keys.pubKeys.owner; var memoPubKey = keys.pubKeys.memo; var activePriKey = keys.privKeys.active.toWif(); var headers = { 'User-Agent': 'Super Agent/0.0.1', "Content-Type":"application/json; charset=utf-8" }

// Configure the request var options = { url: 'http://localhost:3000/api/v1/accounts', method: 'POST', headers: headers, form: {"account":{"name":username,"active_key":activePubKey,"owner_key":ownerPubKey,"memo_key":memoPubKey}} }`

The account created successfully and I'm able to login with bitsharesUI but not here: let bot = await BitShares.login(username, password,'AIPR')

Thanks

scientistnik commented 5 years ago

Firstly you need check equal generate public keys and keys in account. see this code and try to check how there

qualwebs commented 5 years ago

Nik, I found the issue, btsdex login works only for premium accounts. I tried to create some different accounts from bts-ui in same format, that is also not working.

Can you please let me know, How can I create premium account? or any idea?

For example: acc name with chris working but not abc-123...

scientistnik commented 5 years ago

It's work for all account. Try it:


const 
  BitShares = require("btsdex"),
  ACC_NAME = 'test-keys',
  ACC_PSW = 'P5K3QYfEHJmRZ4Qmw629QuFf72bAZMh4oyE6sHYuepsBj';

BitShares.node = "wss://node.testnet.bitshares.eu"
BitShares.subscribe("connected", start);

async function start() {
  let bot = await BitShares.login(ACC_NAME, ACC_PSW);
  let accKey = bot.activeKey.toPublicKey().toString()

  let genKey = BitShares.generateKeys(ACC_NAME, ACC_PSW).pubKeys.active

  console.log(accKey)
  console.log(key)
}
devqualwebs commented 2 years ago

Hi @scientistnik Still facing the same issue, actually genPubKey and acc.active.key_auths[0][0] is not matching... Just to let you know that I'm using a custom chain and changed the chain parameters accordingly.

Please suggest :)

devqualwebs commented 2 years ago

Hey Nik,

Here is the explanation of the issue, Please have a look...

So, I have registered an account with the below code:

var keys = BitShares.generateKeys(username, password, ["owner", "active", "memo"], 'BTS');
var activePubKey = keys.pubKeys.active;
var ownerPubKey = keys.pubKeys.owner;
var memoPubKey = keys.pubKeys.memo;
var activePriKey = keys.privKeys.active.toWif();

Passed all data to Faucet and, The account gets registered successfully, Verified on the blockchain.

let bot = await BitShares.login(username, password, 'BTS');

Above code generating same Public and Auth key as the time of registration.

The question is why we are comparing different keys here?

static async login(accountName, password) {
    let feeSymbol = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : BitShares.chain.coreAsset;
    let acc = await BitShares.accounts[accountName],
        activeKey = _btsdexEcc.PrivateKey.fromSeed(`${accountName}active${password}`),
        genPubKey = activeKey.toPublicKey().toString();
    if (genPubKey != acc.active.key_auths[0][0]) throw new Error("The pair of login and password do not match!");
    let account = new BitShares(accountName, activeKey.toWif(), feeSymbol);
    account.setMemoKey((acc.options.memo_key === genPubKey ? activeKey : _btsdexEcc.PrivateKey.fromSeed(`${accountName}memo${password}`)).toWif());
    await account.initPromise;
    return account;
  }

Not sure if the wrong keys are generating while registering.

if (genPubKey != acc.active.key_auths[0][0]) throw new Error("The pair of login and password do not match!");

This line will always throw an exception.

scientistnik commented 2 years ago

I don't know how work faucet, but if you send request to blockchain node, you need send tree public keys:

let BitShares = require("btsdex")

BitShares.subscribe("connected", start)

async function start() {
  let acc = await BitShares.login(<accountName>, <password>)

  let params = {
    fee: {amount: 0, asset_id: "1.3.0"},
    name: "trade-bot3",
    registrar: "1.2.21058",
    referrer: "1.2.21058",
    referrer_percent: 5000,
    owner: {
      weight_threshold: 1,
      account_auths: [],
      key_auths: [[<ownerPublicKey>, 1]],
      address_auths: []
    },
    active: {
      weight_threshold: 1,
      account_auths: [],
      key_auths: [[<activePublicKey>, 1]],
      address_auths: []
    },
    options: {
      memo_key: <memoPublicKey>,
      voting_account: "1.2.5",
      num_witness: 0,
      num_committee: 0,
      votes: []
    },
    extensions: []
  };

  let tx = acc.newTx()
  tx.account_create(params) // 'account_create' is the operation name
  await tx.broadcast()
}

params saved on blockchain as is. And acc.active.key_auths[0][0] is your generated public active key.

key_auths is array of public keys with weight. Faucet can added her own key (why?!) or change your (what?!).

If you want to see your account data stored in the blockchain, try to use REPL-mode:

# btsdex --account <account-name>