|
|
@ -12,49 +12,71 @@ import { Blockchain } from "../Blockchain"
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@Singleton()
|
|
|
|
@Singleton()
|
|
|
|
export class Transaction extends Unit {
|
|
|
|
export class Transaction extends Unit {
|
|
|
|
|
|
|
|
private processing: boolean = false
|
|
|
|
|
|
|
|
|
|
|
|
@Inject()
|
|
|
|
@Inject()
|
|
|
|
protected readonly firebase!: FirebaseUnit
|
|
|
|
protected readonly firebase!: FirebaseUnit
|
|
|
|
|
|
|
|
|
|
|
|
@Inject()
|
|
|
|
@Inject()
|
|
|
|
protected readonly blockchain!: Blockchain
|
|
|
|
protected readonly blockchain!: Blockchain
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Inject()
|
|
|
|
|
|
|
|
protected readonly logging!: Logging
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
claim() {
|
|
|
|
|
|
|
|
if ( !this.processing ) {
|
|
|
|
|
|
|
|
this.processing = true
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
release() {
|
|
|
|
|
|
|
|
this.processing = false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async compareTransactions(transaction1: TransactionResourceItem, transaction2: TransactionResourceItem) {
|
|
|
|
public async compareTransactions(transaction1: TransactionResourceItem, transaction2: TransactionResourceItem) {
|
|
|
|
// verify signature
|
|
|
|
// verify signature
|
|
|
|
const result1 = await openpgp.verify({
|
|
|
|
const result1 = await openpgp.verify({
|
|
|
|
publicKeys: await openpgp.readKey({
|
|
|
|
publicKeys: await openpgp.readKey({
|
|
|
|
armoredKey: transaction2.partnerPublicKey
|
|
|
|
armoredKey: transaction2.partnerPublicKey
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
message: openpgp.Message.fromText(transaction1.combinedHash),
|
|
|
|
message: await openpgp.readMessage({
|
|
|
|
signature: await openpgp.readSignature({
|
|
|
|
armoredMessage: transaction1.validationSignature,
|
|
|
|
armoredSignature: transaction1.validationSignature // parse detached signature
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const result2 = await openpgp.verify({
|
|
|
|
const result2 = await openpgp.verify({
|
|
|
|
publicKeys: await openpgp.readKey({
|
|
|
|
publicKeys: await openpgp.readKey({
|
|
|
|
armoredKey: transaction1.partnerPublicKey
|
|
|
|
armoredKey: transaction1.partnerPublicKey
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
message: openpgp.Message.fromText(transaction2.combinedHash),
|
|
|
|
message: await openpgp.readMessage({
|
|
|
|
signature: await openpgp.readSignature({
|
|
|
|
armoredMessage: transaction2.validationSignature,
|
|
|
|
armoredSignature: transaction2.validationSignature // parse detached signature
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return await (result1.signatures[0].verified) && await (result2.signatures[0].verified)
|
|
|
|
|
|
|
|
|
|
|
|
return (await result1.signatures[0].verified) && (await result2.signatures[0].verified)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async up() {
|
|
|
|
public async up() {
|
|
|
|
this.firebase.ref("transaction").on("value", async () => {
|
|
|
|
this.firebase.ref("transaction").on("child_added", async () => {
|
|
|
|
// array of pairs of tranaction resource items
|
|
|
|
this.logging.debug('Received child_added event for transactions reference.')
|
|
|
|
const groupedTransactions: [TransactionResourceItem, TransactionResourceItem][] = []
|
|
|
|
if ( !this.claim() ) return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// array of pairs of transaction resource items
|
|
|
|
|
|
|
|
let groupedTransactions: [TransactionResourceItem, TransactionResourceItem][] = []
|
|
|
|
// collection of transaction resource items
|
|
|
|
// collection of transaction resource items
|
|
|
|
let transactions = await TransactionResource.collect().collect()
|
|
|
|
let transactions = await TransactionResource.collect().collect()
|
|
|
|
|
|
|
|
|
|
|
|
// compare each item
|
|
|
|
// compare each item
|
|
|
|
transactions.each(transaction1 => {
|
|
|
|
await transactions.promiseMap(async transaction1 => {
|
|
|
|
// for each item that is not itself
|
|
|
|
// for each item that is not itself
|
|
|
|
transactions.where("combinedHash", "!=", transaction1.combinedHash)
|
|
|
|
await transactions.where('combinedHash', '!=', transaction1.combinedHash)
|
|
|
|
// get a second item
|
|
|
|
// get a second item
|
|
|
|
.each(transaction2 => {
|
|
|
|
.promiseMap(async transaction2 => {
|
|
|
|
//if the item matches
|
|
|
|
//if the item matches
|
|
|
|
if (this.compareTransactions(transaction1, transaction2)) {
|
|
|
|
if ( await this.compareTransactions(transaction1, transaction2) ) {
|
|
|
|
// and remove the two matching items
|
|
|
|
// and remove the two matching items
|
|
|
|
transactions = transactions.whereNotIn("combinedHash", [transaction1.combinedHash, transaction2.combinedHash])
|
|
|
|
transactions = transactions.whereNotIn("combinedHash", [transaction1.combinedHash, transaction2.combinedHash])
|
|
|
|
// insert grouped items into groupedTransactions
|
|
|
|
// insert grouped items into groupedTransactions
|
|
|
@ -62,14 +84,34 @@ export class Transaction extends Unit {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const seenCombinedHashes: string[] = []
|
|
|
|
|
|
|
|
groupedTransactions = groupedTransactions.filter(group => {
|
|
|
|
|
|
|
|
const key = group.map(x => x.combinedHash).sort().join('-')
|
|
|
|
|
|
|
|
if ( !seenCombinedHashes.includes(key) ) {
|
|
|
|
|
|
|
|
seenCombinedHashes.push(key)
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
for (const group of groupedTransactions) {
|
|
|
|
for (const group of groupedTransactions) {
|
|
|
|
await this.blockchain.submitTransactions(group)
|
|
|
|
const block = await this.blockchain.submitTransactions(group)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.logging.verbose('Created block:')
|
|
|
|
|
|
|
|
this.logging.verbose(block)
|
|
|
|
|
|
|
|
|
|
|
|
await this.firebase.ref("transaction").child(group[0].firebaseID).remove()
|
|
|
|
await this.firebase.ref("transaction").child(group[0].firebaseID).remove()
|
|
|
|
await this.firebase.ref("transaction").child(group[1].firebaseID).remove()
|
|
|
|
await this.firebase.ref("transaction").child(group[1].firebaseID).remove()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.release()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async down() {
|
|
|
|
public async down() {
|
|
|
|
|
|
|
|
// Release all subscriptions before shutdown
|
|
|
|
|
|
|
|
this.firebase.ref("transaction").off()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|