Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
applyTransaction(tx) {
// Check the from address matches the signature
const slicedTx = {
contents: tx.contents,
sigs: [],
};
const signer = EthCrypto.recover(tx.sigs[0], getTxHash(slicedTx));
if (signer !== tx.contents.from) {
throw new Error('Invalid signature!');
}
// If we don't have a record for this address, create one
if (!(tx.contents.to in this.state)) {
this.state[[tx.contents.to]] = {
balance: 0,
nonce: 0,
};
}
// Check that the nonce is correct for replay protection
if (tx.contents.nonce > this.state[tx.contents.from].nonce) {
if (!(tx.contents.from in this.invalidNonceTxs)) {
this.invalidNonceTxs[tx.contents.from] = {};
}
this.invalidNonceTxs[tx.contents.from][tx.contents.nonce] = tx;
function applyTransaction (state, tx) {
// Check the from address matches the signature
const signer = EthCrypto.recover(tx.sig, getTxHash(tx.contents))
if (signer !== tx.contents.from) {
throw new Error('Invalid signature!')
}
// If we don't have a record for this address, create one
if (!(tx.contents.to in state)) {
state[[tx.contents.to]] = {
balance: 0,
nonce: 0
}
}
// Check that the nonce is correct for replay protection
if (tx.contents.nonce !== state[[tx.contents.from]].nonce) {
throw new Error('Invalid nonce!')
}
// Mint coins **only if identity is PayPal**
if (tx.contents.type === 'mint' && tx.contents.from === accounts.paypal.address) {
applyTransaction(tx) {
// Check that the from address matches the signature in the transaction
const signer = EthCrypto.recover(tx.sig, getTxHash(tx.contents));
if (signer !== tx.contents.from) {
throw new Error('Invalid signature!');
}
// If we don't have a record for this address, create one
if (!(tx.contents.to in this.state)) {
this.state[[tx.contents.to]] = {
balance: 0,
nonce: 0,
};
}
// Check that the nonce is correct for replay protection
if (tx.contents.nonce !== this.state[[tx.contents.from]].nonce) {
// If it isn't correct, then we should add the transaction to invalidNonceTxs
if (!(tx.contents.from in this.invalidNonceTxs)) {
this.invalidNonceTxs[tx.contents.from] = {};
}
applyTransaction(tx) {
// Recover addresses for all signatures
const signers = [];
for (const sig of tx.sigs) {
signers.push(EthCrypto.recover(sig, this.toHash(tx.contents)));
}
let totalInputValue = 0;
// Check that all inputs are indeed unspent, and that we have the signature for the owner
for (const inputIndex of tx.contents.inputs) {
const input = this.state.txOutputs[inputIndex];
if (!signers.includes(input.owner)) {
throw new Error(`Missing signature for: ${input.owner}`);
}
if (this.state.isSpent[inputIndex]) {
throw new Error('Trying to spend spent tx output!');
}
// Add to the total input value
totalInputValue += input.value;
}
let totalOutputValue = 0;
for (const output of tx.contents.outputs) {
verify(signature, messageHash, address) {
const signer = EthCrypto.recover(signature, messageHash);
return signer === address;
}
static recover_signer_address(signature, hash) {
return EthCrypto.recover(signature, hash);
}
addressesFromSigs(tx) {
let addressSet = new Set()
for (let i = 0; i < tx.sigs.length; i++) {
const sig = tx.sigs[i]
const slicedTx = {
contents: tx.contents,
sigs: tx.sigs.slice(0,i)
}
const messageHash = getTxHash(slicedTx)
const address = EthCrypto.recover(sig, messageHash)
if(i===0 && address !== tx.contents.from) throw new Error('Invalid first signature!')
addressSet.add(address)
}
return addressSet
}
verify(signature, messageHash, address) {
const signer = EthCrypto.recover(signature, messageHash);
return signer === address;
}
verify(signature, messageHash, address) {
const signer = EthCrypto.recover(signature, messageHash)
return signer === address
}
}
verify(signature, messageHash, address) {
const signer = EthCrypto.recover(signature, messageHash);
return signer === address;
}