2016-07-25 10:55:16 +03:00
|
|
|
'use strict';
|
|
|
|
|
2016-07-22 00:03:32 +03:00
|
|
|
var rlp = require('rlp');
|
|
|
|
|
|
|
|
var Contract = require('./contract.js');
|
2016-07-27 00:57:11 +03:00
|
|
|
var Provider = require('./provider.js');
|
2016-07-22 00:03:32 +03:00
|
|
|
var SigningKey = require('./signing-key.js');
|
|
|
|
|
|
|
|
var utils = require('./utils.js');
|
|
|
|
|
|
|
|
|
|
|
|
var transactionFields = [
|
|
|
|
{name: 'nonce', maxLength: 32, },
|
|
|
|
{name: 'gasPrice', maxLength: 32, },
|
|
|
|
{name: 'gasLimit', maxLength: 32, },
|
|
|
|
{name: 'to', length: 20, },
|
|
|
|
{name: 'value', maxLength: 32, },
|
|
|
|
{name: 'data'},
|
|
|
|
];
|
|
|
|
|
2016-07-27 00:57:11 +03:00
|
|
|
function Wallet(privateKey, provider) {
|
2016-07-22 00:03:32 +03:00
|
|
|
if (!(this instanceof Wallet)) { throw new Error('missing new'); }
|
|
|
|
|
2016-07-23 10:27:56 +03:00
|
|
|
// Make sure we have a valid signing key
|
|
|
|
var signingKey = privateKey;
|
|
|
|
if (!(privateKey instanceof SigningKey)) {
|
|
|
|
signingKey = new SigningKey(privateKey);
|
|
|
|
}
|
|
|
|
utils.defineProperty(this, 'privateKey', signingKey.privateKey);
|
2016-07-22 00:03:32 +03:00
|
|
|
|
2016-07-27 00:57:11 +03:00
|
|
|
if (provider) {
|
|
|
|
utils.defineProperty(this, 'provider', new Provider(provider));
|
|
|
|
}
|
|
|
|
|
2016-07-22 00:03:32 +03:00
|
|
|
utils.defineProperty(this, 'address', signingKey.address);
|
|
|
|
|
|
|
|
utils.defineProperty(this, 'sign', function(transaction) {
|
|
|
|
var raw = [];
|
|
|
|
transactionFields.forEach(function(fieldInfo) {
|
|
|
|
var value = transaction[fieldInfo.name] || (new Buffer(0));
|
|
|
|
value = utils.hexOrBuffer(utils.hexlify(value), fieldInfo.name);
|
|
|
|
|
|
|
|
// Fixed-width field
|
|
|
|
if (fieldInfo.length && value.length !== fieldInfo.length) {
|
|
|
|
var error = new Error('invalid ' + fieldInfo.name);
|
|
|
|
error.reason = 'wrong length';
|
|
|
|
error.value = value;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Variable-width (with a maximum)
|
|
|
|
if (fieldInfo.maxLength) {
|
|
|
|
value = utils.stripZeros(value);
|
|
|
|
if (value.length > fieldInfo.maxLength) {
|
|
|
|
var error = new Error('invalid ' + fieldInfo.name);
|
|
|
|
error.reason = 'too long';
|
|
|
|
error.value = value;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
raw.push(value);
|
|
|
|
});
|
|
|
|
|
|
|
|
var digest = utils.sha3(rlp.encode(raw));
|
|
|
|
|
|
|
|
var signature = signingKey.signDigest(digest);
|
|
|
|
var s = signature.s;
|
|
|
|
var v = signature.recoveryParam;
|
|
|
|
|
|
|
|
raw.push(new Buffer([27 + v]));
|
|
|
|
raw.push(utils.bnToBuffer(signature.r));
|
|
|
|
raw.push(utils.bnToBuffer(s));
|
|
|
|
|
|
|
|
return ('0x' + rlp.encode(raw).toString('hex'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-27 00:57:11 +03:00
|
|
|
utils.defineProperty(Wallet.prototype, 'getContract', function(address, abi) {
|
|
|
|
return new Contract(this.provider, this, address, new Contract.Interface(abi));
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-07-22 00:03:32 +03:00
|
|
|
utils.defineProperty(Wallet, 'getAddress', SigningKey.getAddress);
|
|
|
|
utils.defineProperty(Wallet, 'getIcapAddress', SigningKey.getIcapAddress);
|
|
|
|
|
|
|
|
utils.defineProperty(Wallet, '_Contract', Contract);
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = Wallet;
|