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

After I refresh the signed page, what can I do that bring up a new transaction without repeating the signature? #137

Closed yangjing0319 closed 5 years ago

yangjing0319 commented 5 years ago

Hi, @nsjames

Initially I getting an identity and save the user identity in my localstorage ,and then send a transaction successfully. And when I try to refresh the page without sign it again, I hope to finish the new transaction by the cache identity before the cache expired. The problem I met is that I can use the cache identity do something before I send a transaction?

  1. This is getIdentity codes.

    openPlugin = ({successCallback = () => { }, errorCallBack = () => { } }) => {
    const that = this
    const network = that.getNetworks() 
    const requirements = { accounts: [network] }
    
    ScatterJS.scatter.getIdentity(requirements).then(identity => {
      if (!identity) { 
        return errorCallBack('获取身份信息失败!')
      }
      that.store = { 
        ...that.store,
        userIdentity: identity,
        currentAccount: identity.accounts[0]
      }
      const scatterUser = JSON.stringify(identity.accounts[0])
      setStore('scatter_user', scatterUser)
      successCallback()
    }).catch(error => { 
      errorCallBack(error);
    })
    }
getSign = () => {
    const that = this
    return new Promise((resolve, reject) => {
      that.getPublicKey().then(response => {
        if (typeof response !== 'string' && that.store.userIdentity && that.store.userIdentity.accounts) {
          const {userIdentity: {accounts}} = that.store
          const {authority, name} = accounts[0]
          const [targetObj] = (response.permissions || []).filter(item => item.perm_name === authority)
          const {key: publicKey} = targetObj.required_auth.keys[0]
          const timestamp = Math.floor((new Date().getTime()) / 1000)
          const data = `account=${name}&permission=${authority}&publicKey=${publicKey}&timestamp=${timestamp}`
          ScatterJS.scatter.getArbitrarySignature(publicKey, data).then(signature => {
            resolve({
              publicKey,
              account: name,
              timestamp,
              permission: authority,
              sign: signature
            })
          }).catch(error => {
            resolve('获取签名失败!')
          })
        } else {
          resolve('获取用户信息失败!')
        }
      }).catch(error => {
        resolve('获取公钥失败!')
      })
    })
  }
login = () => {
    const that = this
    return new Promise((resolve, reject) => {
      if (!that.hasScatter()) {
        resolve('请安装 Scatter !')
      } else {
        ScatterJS.scatter.connect('SAMPLE').then(connected => {
          if (!connected) { 
            resolve('连接 Scatter 失败!')
          } else { 
            that.openPlugin({ 
              successCallback: () => { 
                that.getSign().then(payload => {
                  resolve(payload) 
                }).catch(error => { 
                  resolve('连接 Scatter 失败!')
                })
              },
              errorCallBack: tips => { 
                resolve(tips)
              }
            })
          }
        }).catch(error => { 
          console.log(error)
          resolve('连接 Scatter 失败!')
        })
      }
    })
  }
  1. This is my traction codes.
rechargeGameCoin = ({appUser, scatterUser, coin, toUrl}) => {
    const that = this
    return new Promise((resolve, reject) => {
      const network = that.getNetworks()
      const eos = ScatterJS.scatter.eos(network, Eos)
      eos.transaction({
        actions: [{
          account: 'eosio.token',
          name: 'transfer',
          authorization: [{
            actor: (scatterUser || {}).name,
            permission: (scatterUser || {}).authority
          }],
          data: {
            from: (scatterUser || {}).name,
            to: toUrl,
            quantity: `${coin} EOS`,
            memo: appUser
          }
        }]
      }).then(response => {
        resolve(response)
      }).catch(error => { // error = {type: "signature_rejected", message: "User rejected the signature request", code: 1000010, isError: true}
        console.log(error)
        if (error.code !== 1000010) {
          try {
            const message = JSON.parse(error)
            if (typeof message === 'object') {
              if (message.code === 500) {
                if (message.error.code === 305003) {
                  resolve('电子钱包余额不足')
                } else {
                  const {error: {name}} = message
                  resolve(name)
                }
              }
            } else {
              resolve('充值失败,请稍后再试!')
            }
          } catch (error) {
            reject('未知错误!')
          }
        } else {
          resolve('充值失败!')
        }
      })
    })
  }
  1. This is re-transction error without getting identity.

Error: No Identity at scatterjs-core.min.js:28 at scatterjs-plugin-eosjs.min.js:1 at f (scatterjs-plugin-eosjs.min.js:1) at Generator._invoke (scatterjs-plugin-eosjs.min.js:1) at Generator.t. [as next] (scatterjs-plugin-eosjs.min.js:1) at e (scatterjs-plugin-eosjs.min.js:1) at a (scatterjs-plugin-eosjs.min.js:1) at scatterjs-plugin-eosjs.min.js:1 at new Promise () at scatterjs-plugin-eosjs.min.js:1

yangjing0319 commented 5 years ago

The related references vision: scatterjs-core.js -> v2.7.18 scatterjs-plugin-eosjs -> v1.5.1 eosjs -> v16.0.9

Thank you for spending time on my recently trouble !

yangjing0319 commented 5 years ago

resolved.