Skip to content

Commit

Permalink
refactor ComposableBuffer.hex for better clarity of intent
Browse files Browse the repository at this point in the history
  • Loading branch information
fuxingloh committed May 19, 2021
1 parent 6c43bb8 commit 73221ff
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('ComposableBuffer deep implementation', () => {
return [
ComposableBuffer.uInt16(() => data.ver, v => data.ver = v),
ComposableBuffer.uInt32(() => data.val, v => data.val = v),
ComposableBuffer.hexLE(8, () => data.hex, v => data.hex = v)
ComposableBuffer.hex(8, () => data.hex, v => data.hex = v)
]
}

Expand Down Expand Up @@ -183,7 +183,7 @@ describe('ComposableBuffer.varUIntArray', () => {
composers (data: VarItem): BufferComposer[] {
return [
ComposableBuffer.uInt16(() => data.val, v => data.val = v),
ComposableBuffer.hexLE(12, () => data.hex, v => data.hex = v)
ComposableBuffer.hex(12, () => data.hex, v => data.hex = v)
]
}
}
Expand Down Expand Up @@ -233,7 +233,7 @@ describe('ComposableBuffer.varUIntArray', () => {

expect(() => {
composer.toBuffer(new SmartBuffer())
}).toThrow('ComposableBuffer.hexLE.toBuffer invalid as length != getter().length')
}).toThrow('ComposableBuffer.hex.toBuffer invalid as length != getter().length')
})

it('should fail toBuffer deeply due to value out of range', () => {
Expand Down Expand Up @@ -263,7 +263,7 @@ describe('ComposableBuffer.array', () => {
composers (data: Item): BufferComposer[] {
return [
ComposableBuffer.bigNumberUInt64(() => data.value, v => data.value = v),
ComposableBuffer.hexLE(32, () => data.txid, v => data.txid = v)
ComposableBuffer.hex(32, () => data.txid, v => data.txid = v)
]
}
}
Expand Down Expand Up @@ -312,7 +312,7 @@ describe('ComposableBuffer.array', () => {

expect(() => {
composer.toBuffer(new SmartBuffer())
}).toThrow('ComposableBuffer.hexLE.toBuffer invalid as length != getter().length')
}).toThrow('ComposableBuffer.hex.toBuffer invalid as length != getter().length')
})

it('should fail toBuffer deeply due to value out of range', () => {
Expand Down Expand Up @@ -344,7 +344,7 @@ describe('ComposableBuffer.single', () => {
class CSingle extends ComposableBuffer<Single> {
composers (data: Single): BufferComposer[] {
return [
ComposableBuffer.hexLE(16, () => data.id, v => data.id = v),
ComposableBuffer.hex(16, () => data.id, v => data.id = v),
ComposableBuffer.uInt16(() => data.value, v => data.value = v)
]
}
Expand Down Expand Up @@ -385,7 +385,7 @@ describe('ComposableBuffer.single', () => {

expect(() => {
composer.toBuffer(new SmartBuffer())
}).toThrow('ComposableBuffer.hexLE.toBuffer invalid as length != getter().length')
}).toThrow('ComposableBuffer.hex.toBuffer invalid as length != getter().length')
})

it('should fail toBuffer deeply due to value out of range', () => {
Expand All @@ -401,8 +401,8 @@ describe('ComposableBuffer.single', () => {
})
})

describe('ComposableBuffer.hexLE', () => {
const composer = ComposableBuffer.hexLE(16, () => value, (v: string) => value = v)
describe('ComposableBuffer.hex', () => {
const composer = ComposableBuffer.hex(16, () => value, (v: string) => value = v)
const expectedBuffer = Buffer.from('9ea83a5c6579d282d189cc04b8e151ef', 'hex')
let value = ''

Expand Down Expand Up @@ -435,12 +435,12 @@ describe('ComposableBuffer.hexLE', () => {

expect(() => {
composer.toBuffer(new SmartBuffer())
}).toThrow('ComposableBuffer.hexLE.toBuffer invalid as length != getter().length')
}).toThrow('ComposableBuffer.hex.toBuffer invalid as length != getter().length')
})
})

describe('ComposableBuffer.hexBE', () => {
const composer = ComposableBuffer.hexBE(16, () => value, (v: string) => value = v)
const composer = ComposableBuffer.hexBEBufferLE(16, () => value, (v: string) => value = v)
const expectedBuffer = Buffer.from('9ea83a5c6579d282d189cc04b8e151ef', 'hex')
let value = ''

Expand Down Expand Up @@ -473,7 +473,7 @@ describe('ComposableBuffer.hexBE', () => {

expect(() => {
composer.toBuffer(new SmartBuffer())
}).toThrow('ComposableBuffer.hexBE.toBuffer invalid as length != getter().length')
}).toThrow('ComposableBuffer.hexBEBufferLE.toBuffer invalid as length != getter().length')
})
})

Expand Down
19 changes: 9 additions & 10 deletions packages/jellyfish-transaction/src/buffer/buffer_composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,15 @@ export abstract class ComposableBuffer<T> implements BufferComposer {
}

/**
* LE ordered HEX String with length specified, encoded in LE order buffer.
* HEX String with length specified, encoded into Buffer as the same order of the Hex String.
* In short this read a hex and push it into the Buffer. It will not re-order the endian.
*
* @param length of the bytes to read/set
* @param getter to read LE ordered HEX String and write as LE ordered Buffer
* @param setter to read LE ordered Buffer and set as LE ordered HEX String
* @param getter to read HEX String and write as the same ordered Buffer
* @param setter to read ordered Buffer and set as the same ordered HEX String
* @throws Error if length != getter().length in set
*/
static hexLE (length: number, getter: () => string, setter: (data: string) => void): BufferComposer {
static hex (length: number, getter: () => string, setter: (data: string) => void): BufferComposer {
return {
fromBuffer: (buffer: SmartBuffer): void => {
const buff = Buffer.from(buffer.readBuffer(length))
Expand All @@ -193,7 +194,7 @@ export abstract class ComposableBuffer<T> implements BufferComposer {
toBuffer: (buffer: SmartBuffer): void => {
const hex = getter()
if (hex.length !== length * 2) {
throw new Error('ComposableBuffer.hexLE.toBuffer invalid as length != getter().length')
throw new Error('ComposableBuffer.hex.toBuffer invalid as length != getter().length')
}
const buff: Buffer = Buffer.from(hex, 'hex')
buffer.writeBuffer(buff)
Expand All @@ -203,13 +204,14 @@ export abstract class ComposableBuffer<T> implements BufferComposer {

/**
* BE ordered HEX String with length specified, encoded in LE order buffer.
* Different from BufferComposer.hex, this will reorder the Buffer from LE to BE and BE to LE.
*
* @param length of the bytes to read/set
* @param getter to read BE ordered HEX String and write as LE ordered Buffer
* @param setter to read LE ordered Buffer and set as BE ordered HEX String
* @throws Error if length != getter().length in set
*/
static hexBE (length: number, getter: () => string, setter: (data: string) => void): BufferComposer {
static hexBEBufferLE (length: number, getter: () => string, setter: (data: string) => void): BufferComposer {
return {
fromBuffer: (buffer: SmartBuffer): void => {
const buff = Buffer.from(buffer.readBuffer(length)).reverse()
Expand All @@ -218,7 +220,7 @@ export abstract class ComposableBuffer<T> implements BufferComposer {
toBuffer: (buffer: SmartBuffer): void => {
const hex = getter()
if (hex.length !== length * 2) {
throw new Error('ComposableBuffer.hexBE.toBuffer invalid as length != getter().length')
throw new Error('ComposableBuffer.hexBEBufferLE.toBuffer invalid as length != getter().length')
}
const buff: Buffer = Buffer.from(hex, 'hex').reverse()
buffer.writeBuffer(buff)
Expand Down Expand Up @@ -320,9 +322,6 @@ export abstract class ComposableBuffer<T> implements BufferComposer {
}
}

// TODO(jellyfish): with the introduction of hexLE, hexBE, utf8LE, utf8BE
// might want to indicate LE for all unsigned integer

/**
* Unsigned Int8, 1 byte
*
Expand Down
2 changes: 1 addition & 1 deletion packages/jellyfish-transaction/src/tx_composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class CVin extends ComposableBuffer<Vin> implements Vin {
composers (vin: Vin): BufferComposer[] {
return [
// defid returns txid in BE order hence we need to reverse it
ComposableBuffer.hexBE(32, () => vin.txid, v => vin.txid = v),
ComposableBuffer.hexBEBufferLE(32, () => vin.txid, v => vin.txid = v),
ComposableBuffer.uInt32(() => vin.index, v => vin.index = v),
ComposableBuffer.single<Script>(() => vin.script, v => vin.script = v, v => new CScript(v)),
ComposableBuffer.uInt32(() => vin.sequence, v => vin.sequence = v)
Expand Down
8 changes: 4 additions & 4 deletions packages/jellyfish-transaction/src/tx_segwit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ export class CWitnessProgram extends ComposableBuffer<WitnessProgram> {
composers (wp: WitnessProgram): BufferComposer[] {
return [
ComposableBuffer.uInt32(() => wp.version, v => wp.version = v),
ComposableBuffer.hexLE(32, () => wp.hashPrevouts, v => wp.hashPrevouts = v),
ComposableBuffer.hexLE(32, () => wp.hashSequence, v => wp.hashSequence = v),
ComposableBuffer.hexBE(32, () => wp.outpointTxId, v => wp.outpointTxId = v),
ComposableBuffer.hex(32, () => wp.hashPrevouts, v => wp.hashPrevouts = v),
ComposableBuffer.hex(32, () => wp.hashSequence, v => wp.hashSequence = v),
ComposableBuffer.hexBEBufferLE(32, () => wp.outpointTxId, v => wp.outpointTxId = v),
ComposableBuffer.uInt32(() => wp.outpointIndex, v => wp.outpointIndex = v),
ComposableBuffer.single<Script>(() => wp.scriptCode, v => wp.scriptCode = v, v => new CScript(v)),
ComposableBuffer.satoshiAsBigNumber(() => wp.value, v => wp.value = v),
ComposableBuffer.uInt32(() => wp.sequence, v => wp.sequence = v),
ComposableBuffer.hexLE(32, () => wp.hashOutputs, v => wp.hashOutputs = v),
ComposableBuffer.hex(32, () => wp.hashOutputs, v => wp.hashOutputs = v),
ComposableBuffer.uInt32(() => wp.lockTime, v => wp.lockTime = v),
ComposableBuffer.uInt32(() => wp.hashType, v => wp.hashType = v)
]
Expand Down

0 comments on commit 73221ff

Please sign in to comment.