-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v0.4.3, added correct parsing of auxpow, transaction fixes
- Loading branch information
Showing
17 changed files
with
548 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,6 @@ module.exports = { | |
singleQuote: true, | ||
trailingComma: 'es5', | ||
arrowParens: 'always', | ||
semi: true, | ||
printWidth: 80, | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { Transaction } from '../transaction'; | ||
import { BytesBuilder } from '../utils/bytesBuilder'; | ||
import { BytesReader } from '../utils/bytesReader'; | ||
import { hexToU8Array, u8ArrayToHex } from '../utils/data'; | ||
import { IMerkleBranch, IStandardBlockHeader } from './types'; | ||
import { | ||
merkleBranchEncodingLength, | ||
readMerkleBranch, | ||
readStandardBlockHeader, | ||
writeMerkleBranch, | ||
writeStandardBlockHeader, | ||
} from './utils'; | ||
|
||
class AuxPow { | ||
coinbaseTransaction: Transaction; | ||
blockHash: string; | ||
coinbaseBranch: IMerkleBranch; | ||
blockchainBranch: IMerkleBranch; | ||
parentBlock: IStandardBlockHeader; | ||
|
||
constructor( | ||
coinbaseTransaction: Transaction, | ||
blockHash: string, | ||
coinbaseBranch: IMerkleBranch, | ||
blockchainBranch: IMerkleBranch, | ||
parentBlock: IStandardBlockHeader | ||
) { | ||
this.coinbaseTransaction = coinbaseTransaction; | ||
this.blockHash = blockHash; | ||
this.coinbaseBranch = coinbaseBranch; | ||
this.blockchainBranch = blockchainBranch; | ||
this.parentBlock = parentBlock; | ||
} | ||
|
||
byteLength(): number { | ||
return ( | ||
this.coinbaseTransaction.byteLength(true) + | ||
32 + | ||
merkleBranchEncodingLength(this.coinbaseBranch) + | ||
merkleBranchEncodingLength(this.blockchainBranch) + | ||
80 | ||
); | ||
} | ||
|
||
writeToBytesBuilder(builder: BytesBuilder): BytesBuilder { | ||
this.coinbaseTransaction.writeToBytesBuilder(builder, true, null); | ||
builder.writeBytes(hexToU8Array(this.blockHash)); | ||
writeMerkleBranch(builder, this.coinbaseBranch); | ||
writeMerkleBranch(builder, this.blockchainBranch); | ||
writeStandardBlockHeader(builder, this.parentBlock); | ||
return builder; | ||
} | ||
|
||
toBuffer(): Uint8Array { | ||
return this.writeToBytesBuilder( | ||
new BytesBuilder(this.byteLength()) | ||
).toBuffer(); | ||
} | ||
|
||
toHex() { | ||
return u8ArrayToHex(this.toBuffer()); | ||
} | ||
|
||
static fromBuffer(buffer: Uint8Array): AuxPow { | ||
return AuxPow.fromBytesReader(new BytesReader(buffer)); | ||
} | ||
|
||
static fromHex(hex: string): AuxPow { | ||
return AuxPow.fromBuffer(hexToU8Array(hex)); | ||
} | ||
|
||
static fromBytesReader(reader: BytesReader): AuxPow { | ||
const coinbaseTransaction = Transaction.fromBytesReader(reader); | ||
const blockHash = u8ArrayToHex(reader.readBytes(32)); | ||
const coinbaseBranch = readMerkleBranch(reader); | ||
const blockchainBranch = readMerkleBranch(reader); | ||
const parentBlock = readStandardBlockHeader(reader); | ||
|
||
return new AuxPow( | ||
coinbaseTransaction, | ||
blockHash, | ||
coinbaseBranch, | ||
blockchainBranch, | ||
parentBlock | ||
); | ||
} | ||
} | ||
|
||
|
||
export { | ||
AuxPow, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { BytesBuilder } from '../utils/bytesBuilder'; | ||
import { BytesReader } from '../utils/bytesReader'; | ||
import { hexToU8Array, u8ArrayToHex } from '../utils/data'; | ||
import { AuxPow } from './auxpow'; | ||
import { IStandardBlockHeaderAuxPow } from './types'; | ||
import { readStandardBlockHeader, writeStandardBlockHeader } from './utils'; | ||
|
||
class BlockHeader implements IStandardBlockHeaderAuxPow { | ||
auxData?: AuxPow | undefined; | ||
version: number; | ||
prevHash: string; | ||
merkleRoot: string; | ||
timestamp: number; | ||
bits: number; | ||
nonce: number; | ||
|
||
constructor({ | ||
version, | ||
prevHash, | ||
merkleRoot, | ||
timestamp, | ||
bits, | ||
nonce, | ||
auxData, | ||
}: IStandardBlockHeaderAuxPow) { | ||
this.version = version; | ||
this.prevHash = prevHash; | ||
this.merkleRoot = merkleRoot; | ||
this.timestamp = timestamp; | ||
this.bits = bits; | ||
this.nonce = nonce; | ||
this.auxData = auxData; | ||
} | ||
|
||
byteLength(): number { | ||
return this.auxData ? (80 + this.auxData.byteLength()) : 80; | ||
} | ||
|
||
writeToBytesBuilder(builder: BytesBuilder): BytesBuilder { | ||
writeStandardBlockHeader(builder, this); | ||
if(this.auxData) { | ||
this.auxData.writeToBytesBuilder(builder); | ||
} | ||
return builder; | ||
} | ||
|
||
toBuffer(): Uint8Array { | ||
return this.writeToBytesBuilder( | ||
new BytesBuilder(this.byteLength()) | ||
).toBuffer(); | ||
} | ||
|
||
toHex(): string { | ||
return u8ArrayToHex(this.toBuffer()); | ||
} | ||
|
||
static fromBytesReader(reader: BytesReader): BlockHeader { | ||
const block = new BlockHeader( | ||
readStandardBlockHeader(reader), | ||
); | ||
if ((block.version & 0x100) !== 0) { | ||
block.auxData = AuxPow.fromBytesReader(reader); | ||
} | ||
return block; | ||
} | ||
|
||
static fromBuffer(buffer: Uint8Array): BlockHeader { | ||
if (buffer.length < 80) { | ||
throw new Error('Block must be at least 80 bytes long'); | ||
} | ||
const reader = new BytesReader(buffer); | ||
return this.fromBytesReader(reader); | ||
} | ||
|
||
static fromHex(hex: string): BlockHeader { | ||
return this.fromBuffer(hexToU8Array(hex)); | ||
} | ||
|
||
} | ||
|
||
|
||
export { | ||
BlockHeader, | ||
} |
Oops, something went wrong.