Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// amount of transaction
amount,
// which address the transaction is from
from: this.wallet.address,
// which address the transaction is going to
to,
// the nonce of the sender's address at the time of creating this transaction
nonce: this.state[this.wallet.address].nonce,
};
// create a transaction object
const tx = {
// unsigned transaction
contents: unsignedTx,
// signature of the unsigned transaction
// (hash the unsigned transaction object and then sign it with this node's private key)
sig: EthCrypto.sign(this.wallet.privateKey, getTxHash(unsignedTx)),
};
// return the transaction object
return tx;
}
async signMessage (message) {
const hex = Buffer.from(message).toString('hex')
const messageHash = EthCrypto.hash.keccak256(message);
const signature = EthCrypto.sign(
ensure0x(this.wallet.getPrivateKey().toString('hex')), // private key
messageHash // hash of message
);
return remove0x(signature)
}
sign(message) {
const messageHash = this.toHash(message)
return EthCrypto.sign(
this.wallet.privateKey,
messageHash
)
}
static doCreateSignature(privateKey: string, message: string) {
return sign(privateKey, Authenticator.createEthereumMessageHash(message))
}
sign(message) {
const messageHash = this.hash(message);
return EthCrypto.sign(this.wallet.privateKey, messageHash);
}
generateTx (to, amount) {
const unsignedTx = {
type: 'send',
amount: amount,
from: this.wallet.address,
to: to,
nonce: this.nonce
}
const tx = {
contents: unsignedTx,
sig: EthCrypto.sign(this.wallet.privateKey, getTxHash(unsignedTx))
}
this.nonce++ //added so a node can send multiple txs before applying them (before they are included in a block)
return tx
}
if(this.network.time >= this.timeout(tx.contents.timestamp, sigs.size)) return
//seen tx
this.seen.push(tx.contents)
//TODO Check that each signee is actually a peer in the network
//-possible attack: byzantine node signs a message 100 times with random Private Key
const finalTimeout = this.timeout(tx.contents.timestamp, this.network.agents.length)
if (!this.pendingTxs[finalTimeout]) this.pendingTxs[finalTimeout] = []
//add to pending ( we'll apply this transaction once we hit finalTimeout)
this.pendingTxs[finalTimeout].push(tx)
//Choice rule: if have two transactions with same sender, nonce, and timestamp apply the one with lower sig first
this.pendingTxs[finalTimeout].sort((a, b)=>{
return a.sigs[0] - b.sigs[0]
})
//add signature
tx.sigs.push(EthCrypto.sign(this.wallet.privateKey, getTxHash(tx)))
this.network.broadcast(this.pid, tx)
}
sign(message) {
const messageHash = this.toHash(message)
return EthCrypto.sign(
this.wallet.privateKey,
messageHash
)
}
generateTx(to, amount) {
const unsignedTx = {
type: 'send',
amount,
from: this.wallet.address,
to,
nonce: this.state[this.wallet.address].nonce,
};
const tx = {
contents: unsignedTx,
sigs: [],
};
tx.sigs.push(EthCrypto.sign(this.wallet.privateKey, getTxHash(tx)));
return tx;
}