|
|
@ -34,6 +34,7 @@ function IdentityStore(opts = {}) { |
|
|
|
} |
|
|
|
} |
|
|
|
// not part of serilized metamask state - only kept in memory
|
|
|
|
// not part of serilized metamask state - only kept in memory
|
|
|
|
this._unconfTxCbs = {} |
|
|
|
this._unconfTxCbs = {} |
|
|
|
|
|
|
|
this._unconfMsgCbs = {} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
@ -140,6 +141,10 @@ IdentityStore.prototype.exportAccount = function(address, cb) { |
|
|
|
cb(null, privateKey) |
|
|
|
cb(null, privateKey) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// Transactions
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
// comes from dapp via zero-client hooked-wallet provider
|
|
|
|
// comes from dapp via zero-client hooked-wallet provider
|
|
|
|
IdentityStore.prototype.addUnconfirmedTransaction = function(txParams, cb){ |
|
|
|
IdentityStore.prototype.addUnconfirmedTransaction = function(txParams, cb){ |
|
|
|
|
|
|
|
|
|
|
@ -170,7 +175,6 @@ IdentityStore.prototype.addUnconfirmedTransaction = function(txParams, cb){ |
|
|
|
// comes from metamask ui
|
|
|
|
// comes from metamask ui
|
|
|
|
IdentityStore.prototype.approveTransaction = function(txId, cb){ |
|
|
|
IdentityStore.prototype.approveTransaction = function(txId, cb){ |
|
|
|
var txData = configManager.getTx(txId) |
|
|
|
var txData = configManager.getTx(txId) |
|
|
|
var txParams = txData.txParams |
|
|
|
|
|
|
|
var approvalCb = this._unconfTxCbs[txId] || noop |
|
|
|
var approvalCb = this._unconfTxCbs[txId] || noop |
|
|
|
|
|
|
|
|
|
|
|
// accept tx
|
|
|
|
// accept tx
|
|
|
@ -206,6 +210,73 @@ IdentityStore.prototype.signTransaction = function(txParams, cb){ |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// Messages
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// comes from dapp via zero-client hooked-wallet provider
|
|
|
|
|
|
|
|
IdentityStore.prototype.addUnconfirmedMessage = function(msgParams, cb){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// create txData obj with parameters and meta data
|
|
|
|
|
|
|
|
var time = (new Date()).getTime() |
|
|
|
|
|
|
|
var msgId = createId() |
|
|
|
|
|
|
|
var msgData = { |
|
|
|
|
|
|
|
id: msgId, |
|
|
|
|
|
|
|
msgParams: msgParams, |
|
|
|
|
|
|
|
time: time, |
|
|
|
|
|
|
|
status: 'unconfirmed', |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
configManager.addMsg(msgData) |
|
|
|
|
|
|
|
console.log('addUnconfirmedMessage:', msgData) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// keep the cb around for after approval (requires user interaction)
|
|
|
|
|
|
|
|
// This cb fires completion to the Dapp's write operation.
|
|
|
|
|
|
|
|
this._unconfMsgCbs[msgId] = cb |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// signal update
|
|
|
|
|
|
|
|
this._didUpdate() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return msgId |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// comes from metamask ui
|
|
|
|
|
|
|
|
IdentityStore.prototype.approveMessage = function(msgId, cb){ |
|
|
|
|
|
|
|
var msgData = configManager.getMsg(msgId) |
|
|
|
|
|
|
|
var approvalCb = this._unconfMsgCbs[msgId] || noop |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// accept msg
|
|
|
|
|
|
|
|
cb() |
|
|
|
|
|
|
|
approvalCb(null, true) |
|
|
|
|
|
|
|
// clean up
|
|
|
|
|
|
|
|
configManager.confirmMsg(msgId) |
|
|
|
|
|
|
|
delete this._unconfMsgCbs[msgId] |
|
|
|
|
|
|
|
this._didUpdate() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// comes from metamask ui
|
|
|
|
|
|
|
|
IdentityStore.prototype.cancelMessage = function(msgId){ |
|
|
|
|
|
|
|
var txData = configManager.getMsg(msgId) |
|
|
|
|
|
|
|
var approvalCb = this._unconfMsgCbs[msgId] || noop |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// reject tx
|
|
|
|
|
|
|
|
approvalCb(null, false) |
|
|
|
|
|
|
|
// clean up
|
|
|
|
|
|
|
|
configManager.rejectMsg(msgId) |
|
|
|
|
|
|
|
delete this._unconfTxCbs[msgId] |
|
|
|
|
|
|
|
this._didUpdate() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// performs the actual signing, no autofill of params
|
|
|
|
|
|
|
|
IdentityStore.prototype.signMessage = function(msgParams, cb){ |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
console.log('signing msg...', msgParams.data) |
|
|
|
|
|
|
|
var rawMsg = this._idmgmt.signMsg(msgParams.from, msgParams.data) |
|
|
|
|
|
|
|
cb(null, rawMsg) |
|
|
|
|
|
|
|
} catch (err) { |
|
|
|
|
|
|
|
cb(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// private
|
|
|
|
// private
|
|
|
|
//
|
|
|
|
//
|
|
|
@ -352,7 +423,7 @@ function IdManagement(opts) { |
|
|
|
var tx = new Transaction(txParams) |
|
|
|
var tx = new Transaction(txParams) |
|
|
|
|
|
|
|
|
|
|
|
// sign tx
|
|
|
|
// sign tx
|
|
|
|
var privKeyHex = ethUtil.addHexPrefix(this.keyStore.exportPrivateKey(txParams.from, this.derivedKey, this.hdPathString)) |
|
|
|
var privKeyHex = this.exportPrivateKey(txParams.from) |
|
|
|
var privKey = ethUtil.toBuffer(privKeyHex) |
|
|
|
var privKey = ethUtil.toBuffer(privKeyHex) |
|
|
|
tx.sign(privKey) |
|
|
|
tx.sign(privKey) |
|
|
|
|
|
|
|
|
|
|
@ -367,12 +438,23 @@ function IdManagement(opts) { |
|
|
|
return rawTx |
|
|
|
return rawTx |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.signMsg = function(address, message){ |
|
|
|
|
|
|
|
// sign message
|
|
|
|
|
|
|
|
var privKeyHex = this.exportPrivateKey(address) |
|
|
|
|
|
|
|
var privKey = ethUtil.toBuffer(privKeyHex) |
|
|
|
|
|
|
|
var msgHash = ethUtil.sha3(message) |
|
|
|
|
|
|
|
var msgSig = ethUtil.ecsign(msgHash, privKey) |
|
|
|
|
|
|
|
var rawMsgSig = ethUtil.bufferToHex(concatSig(msgSig.v, msgSig.r, msgSig.s)) |
|
|
|
|
|
|
|
return rawMsgSig |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.getSeed = function(){ |
|
|
|
this.getSeed = function(){ |
|
|
|
return this.keyStore.getSeed(this.derivedKey) |
|
|
|
return this.keyStore.getSeed(this.derivedKey) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.exportPrivateKey = function(address) { |
|
|
|
this.exportPrivateKey = function(address) { |
|
|
|
return this.keyStore.exportPrivateKey(address, this.derivedKey, this.hdPathString) |
|
|
|
var privKeyHex = ethUtil.addHexPrefix(this.keyStore.exportPrivateKey(address, this.derivedKey, this.hdPathString)) |
|
|
|
|
|
|
|
return privKeyHex |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -380,3 +462,14 @@ function IdManagement(opts) { |
|
|
|
// util
|
|
|
|
// util
|
|
|
|
|
|
|
|
|
|
|
|
function noop(){} |
|
|
|
function noop(){} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function concatSig(v, r, s) { |
|
|
|
|
|
|
|
r = ethUtil.fromSigned(r) |
|
|
|
|
|
|
|
s = ethUtil.fromSigned(s) |
|
|
|
|
|
|
|
v = ethUtil.bufferToInt(v) |
|
|
|
|
|
|
|
r = ethUtil.toUnsigned(r).toString('hex') |
|
|
|
|
|
|
|
s = ethUtil.toUnsigned(s).toString('hex') |
|
|
|
|
|
|
|
v = ethUtil.stripHexPrefix(ethUtil.intToHex(v)) |
|
|
|
|
|
|
|
return ethUtil.addHexPrefix(r.concat(s, v).toString("hex")) |
|
|
|
|
|
|
|
} |