QuantumMechanics / NEM-sdk

NEM Developer Kit for Node.js and the browser
MIT License
137 stars 82 forks source link

NEM-SDK returns diffrent results from the NanoWallet #14

Closed khawarizmus closed 7 years ago

khawarizmus commented 7 years ago

While nem.model.wallet.createBrain returns exactly the same information that the NanoWallet do. When creating a brain wallet the nem.model.wallet.createPRNG doesn't, and it's giving me completely different results from the NanoWallet.

Imgur

Imgur

The address and the private key are completely different even tho i provided the same name and password to both parties (nem.model.wallet.createPRNG and the NanoWallet).

khawarizmus commented 7 years ago

Another thing.. I just realized that it's generating a random private key for the exact same name and password each time i execute the code!

function() {
    const _this = this; // we preserve the this scope
    if(_this.$store.getters.getWtype === 'prng'){
      // for simple wallet
      const wallet = nem.model.wallet
        .createPRNG( // create a simple wallet
          _this.$store.getters.getName, 
          _this.$store.getters.getPassword, 
          nem.model.network.data.testnet.id);

      _this.$store.commit('setWallet', wallet); // push the wallet to the store just in case
      // create a common object it's the object that will contain the pass and pk
      const common = nem.model.objects.create('common')(_this.$store.getters.getPassword, ''); 
      // Get the wallet account to decrypt
      const account = wallet.accounts[0];
      // Decrypt account private key in the common object
      nem.crypto.helpers.passwordToPrivatekey(common, account, wallet.accounts[0].algo);
      _this.$store.commit('setCommon', common); // push the common to the store
      console.log(common.privateKey, '\nthis the next generated\n' , nem.crypto.helpers.derivePassSha(_this.$store.getters.getPassword, 6000).priv);
      console.log(nem.crypto.keyPair.create(common.privateKey));
    }else if(_this.$store.getters.getWtype === 'brain'){
      // for brain wallet
      const wallet = nem.model.wallet
        .createBrain( // create a brain wallet
          _this.$store.getters.getName,
          _this.$store.getters.getPassword, 
          nem.model.network.data.testnet.id);

      _this.$store.commit('setWallet', wallet); // we save the wallet
      // create a common object it's the object that will contain the pass and pk
      const common = nem.model.objects.create('common')(_this.$store.getters.getPassword, '');
      // Get the wallet account to decrypt
      const account = wallet.accounts[0];
      // Decrypt account private key in the common object
      nem.crypto.helpers.passwordToPrivatekey(common, account, wallet.accounts[0].algo);
      _this.$store.commit('setCommon', common); // push the common to the store
    }
  }

here is my code notice that the creation of a brain wallet works perfectly fine i can replicate the excat same information from my code and the NanoWallet

khawarizmus commented 7 years ago

After discussing the issue with @jabo38 he explained to me this

a simple wallet should always be random each time a brain wallet should always be the same each time

so the only way to replicate a simple wallet is to have the private key and use nem.model.wallet.importPrivateKey to create it

I hope that this may help someone in the future and i am closing this issue