GetScatter / scatter-js

Importable JavaScript library that allows web applications to directly interface with Scatter Desktop, Classic and Mobile.
MIT License
262 stars 148 forks source link

Uncaught (in promise) Error: No Identity #41

Closed Jeremyyang closed 6 years ago

Jeremyyang commented 6 years ago

Since I use scatter-core with chrome scatter extension, an error occurs as the title shows when I tried to transfer.

let holder = new Holder(new Index());
if(typeof window !== 'undefined') {
    // Catching extension instead of Desktop
    if(typeof document !== 'undefined'){
        const bindScatterClassic = () => {
            holder.scatter = window.scatter;  // **here holder.scatter is overridden by window.scatter**
            holder.scatter.isExtension = true;
            holder.scatter.connect = () => new Promise(resolve => resolve(true));
        };

        if(typeof window.scatter !== 'undefined') bindScatterClassic();
        else document.addEventListener('scatterLoaded', () => bindScatterClassic());
    }
    window.ScatterJS = holder;
}
constructor(){
    this.isExtension = false;
    this.identity = null;
}

loadPlugin(plugin){
    const noIdFunc = () => {
        //**here 'this' refers to instance of Index **
        //** so when 'scatterjs-plugin-eosjs' executes 'throwIfNoIdentity()', 'this.identity' still equals 'null';**
        if(!this.identity) throw new Error('No Identity') 
    };
    if(!plugin.isValid()) throw new Error(`${plugin.name} doesn't seem to be a valid ScatterJS plugin.`);
    PluginRepository.loadPlugin(plugin);
    if(plugin.isSignatureProvider()){
        this[plugin.name] = plugin.signatureProvider(noIdFunc);
        this[plugin.name+'Hook'] = plugin.hookProvider;
    }
}

// **this function would not be called since the override
getIdentity(requiredFields){
    throwNoAuth();
    return SocketService.sendApiRequest({
        type:'getOrRequestIdentity',
        payload:{
            fields:requiredFields
        }
    }).then(id => {
        if(id) this.identity = id;
        return id;
    });
}
nsjames commented 6 years ago

I need to see your code, not mine :)

Also throwNoAuth(); is not the noIdentity thrower. It's an authentication thrower, and doesn't even exist if using the extension.

Jeremyyang commented 6 years ago

Thank you for reply. Here's my code:

import ScatterJS from 'scatterjs-core';
import ScatterEOS from 'scatterjs-plugin-eosjs'
const Eos = require('eosjs');
ScatterJS.plugins(new ScatterEOS());

class EosApi {
    constructor() {
        this.eos = ScatterJS.scatter.eos(this.getNetwork(), Eos, {});
    }

    login() {
        // I think actually ScatterJS.scatter.getIdentity refers to the extension's scatter.getIdentity
        return ScatterJS.scatter.getIdentity({
            accounts: [this.getNetwork()]
        }).then( (identity) => {
            return !!identity;
        }).catch((error) => {
            return false;
        });
    }

    doTransfer(obj, opts) {
        const opts = { authorization: [`${obj.from}@${obj.authority}`] };
        return this.eos.transfer(obj.from, obj.to, obj.balance + " EOS", obj.memo, opts);
    }
}

the packages version are:

  "eosjs": "^16.0.9",
  "scatterjs-core": "^2.3.2",
  "scatterjs-plugin-eosjs": "^1.3.3"

The error threw from throwIfNoIdentity() of scatterjs-plugin-eosjs when I call eosApi.doTransfer() after I logined successfuly.

nsjames commented 6 years ago

Try setting this.eos after you login.

Jeremyyang commented 6 years ago

Thank you so much, it works!