Skip to content

Commit

Permalink
Added Transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
junderw committed Dec 29, 2018
1 parent 2eb9534 commit a652d04
Show file tree
Hide file tree
Showing 8 changed files with 493 additions and 387 deletions.
11 changes: 5 additions & 6 deletions src/block.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Transaction } from './transaction'
const Buffer = require('safe-buffer').Buffer
const bcrypto = require('./crypto')
const fastMerkleRoot = require('merkle-lib/fastRoot')
Expand All @@ -9,7 +10,7 @@ const errorMerkleNoTxes = new TypeError('Cannot compute merkle root for zero tra
const errorWitnessNotSegwit = new TypeError('Cannot compute witness commit for non-segwit block')
const errorBufferTooSmall = new Error('Buffer too small (< 80 bytes)')

function txesHaveWitness (transactions: Array<any>): boolean {
function txesHaveWitness (transactions: Array<Transaction>): boolean {
return transactions !== undefined &&
transactions instanceof Array &&
transactions[0] &&
Expand All @@ -21,8 +22,6 @@ function txesHaveWitness (transactions: Array<any>): boolean {
transactions[0].ins[0].witness.length > 0
}

const Transaction = require('./transaction')

export class Block {
version: number
prevHash: Buffer
Expand All @@ -31,7 +30,7 @@ export class Block {
witnessCommit: Buffer
bits: number
nonce: number
transactions: Array<any>
transactions: Array<Transaction>

constructor () {
this.version = 1
Expand Down Expand Up @@ -120,12 +119,12 @@ export class Block {
return target
}

static calculateMerkleRoot (transactions: Array<any>, forWitness: boolean | void): Buffer {
static calculateMerkleRoot (transactions: Array<Transaction>, forWitness: boolean | void): Buffer {
typeforce([{ getHash: types.Function }], transactions)
if (transactions.length === 0) throw errorMerkleNoTxes
if (forWitness && !txesHaveWitness(transactions)) throw errorWitnessNotSegwit

const hashes = transactions.map(transaction => transaction.getHash(forWitness))
const hashes = transactions.map(transaction => transaction.getHash((<boolean>forWitness)))

const rootHash = fastMerkleRoot(hashes, bcrypto.hash256)

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const opcodes = require('bitcoin-ops')

import { Block } from './block'
import * as ECPair from './ecpair'
import * as Transaction from './transaction'
import { Transaction } from './transaction'
import * as TransactionBuilder from './transaction_builder'
import * as address from './address'
import * as bip32 from 'bip32'
Expand Down
Loading

0 comments on commit a652d04

Please sign in to comment.