Skip to content

Add BIP370 fields #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions ts_src/lib/converter/input/hash160.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { HASH160, KeyValue } from '../../interfaces';
import { InputTypes } from '../../typeFields';
import * as crypto from 'crypto';

export function decode(keyVal: KeyValue): HASH160 {
if (keyVal.key[0] !== InputTypes.RIPEMD160) {
throw new Error(
'Decore Error: could not decode ripemd160 with key 0x' +
keyVal.key.toString('hex'),
);
}

return {
preimage: keyVal.value,
hash: keyVal.key.slice(1),
};
}

export function encode(data: HASH160): KeyValue {
const head = Buffer.from([InputTypes.HASH160]);
const key = Buffer.concat([head, data.hash]);

const value = data.preimage;

return {
key,
value,
};
}

export const expected = `{ preimage: Buffer; hash: Buffer; }`;

export function check(data: any): data is HASH160 {
return (
Buffer.isBuffer(data.preimage) &&
Buffer.isBuffer(data.hash) &&
crypto
.createHash('hash160')
.update(
crypto
.createHash('sha256')
.update(data.preimage)
.digest(),
)
.digest()
.compare(data.hash) == 0
);
}
48 changes: 48 additions & 0 deletions ts_src/lib/converter/input/hash256.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { HASH256, KeyValue } from '../../interfaces';
import { InputTypes } from '../../typeFields';
import * as crypto from 'crypto';

export function decode(keyVal: KeyValue): HASH256 {
if (keyVal.key[0] !== InputTypes.RIPEMD160) {
throw new Error(
'Decore Error: could not decode ripemd160 with key 0x' +
keyVal.key.toString('hex'),
);
}

return {
preimage: keyVal.value,
hash: keyVal.key.slice(1),
};
}

export function encode(data: HASH256): KeyValue {
const head = Buffer.from([InputTypes.HASH160]);
const key = Buffer.concat([head, data.hash]);

const value = data.preimage;

return {
key,
value,
};
}

export const expected = `{ preimage: Buffer; hash: Buffer; }`;

export function check(data: any): data is HASH256 {
return (
Buffer.isBuffer(data.preimage) &&
Buffer.isBuffer(data.hash) &&
crypto
.createHash('sha256')
.update(
crypto
.createHash('sha256')
.update(data.preimage)
.digest(),
)
.digest()
.compare(data.hash) == 0
);
}
26 changes: 26 additions & 0 deletions ts_src/lib/converter/input/previousTxid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { KeyValue, PreviousTxId } from '../../interfaces';
import { InputTypes } from '../../typeFields';

export function decode(keyVal: KeyValue): PreviousTxId {
if (keyVal.key[0] !== InputTypes.PREVIOUS_TXID) {
throw new Error(
'Decode Error: could not decode previousTxid with key 0x' +
keyVal.key.toString('hex'),
);
}
return keyVal.value;
}

export function encode(data: PreviousTxId): KeyValue {
const key = Buffer.from([InputTypes.PREVIOUS_TXID]);
return {
key,
value: data,
};
}

export const expected = 'Buffer';

export function check(data: any): data is PreviousTxId {
return Buffer.isBuffer(data);
}
32 changes: 32 additions & 0 deletions ts_src/lib/converter/input/requiredHeightLocktime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { KeyValue, RequiredHeightLocktime } from '../../interfaces';
import { InputTypes } from '../../typeFields';

export function decode(keyVal: KeyValue): RequiredHeightLocktime {
if (keyVal.key[0] !== InputTypes.REQUIRED_HEIGHT_LOCKTIME) {
throw new Error(
'Decode Error: could not decode requiredHeightLocktime with key 0x' +
keyVal.key.toString('hex'),
);
}
return keyVal.value.readUInt32LE(0);
}

export function encode(data: RequiredHeightLocktime): KeyValue {
const key = Buffer.from([InputTypes.REQUIRED_HEIGHT_LOCKTIME]);

const value = Buffer.alloc(4);
value.writeUInt32LE(data, 0);

return {
key,
value,
};
}

export const expected = 'Buffer';

export function check(data: any): data is RequiredHeightLocktime {
return (
typeof data === 'number' && !isNaN(data) && data < 0xffffffff && data >= 0 //add check if it's < 500000000 ?
);
}
32 changes: 32 additions & 0 deletions ts_src/lib/converter/input/requiredTimeLocktime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { KeyValue, RequiredTimeLocktime } from '../../interfaces';
import { InputTypes } from '../../typeFields';

export function decode(keyVal: KeyValue): RequiredTimeLocktime {
if (keyVal.key[0] !== InputTypes.REQUIRED_TIME_LOCKTIME) {
throw new Error(
'Decode Error: could not decode requiredTimeLocktime with key 0x' +
keyVal.key.toString('hex'),
);
}
return keyVal.value.readUInt32LE(0);
}

export function encode(data: RequiredTimeLocktime): KeyValue {
const key = Buffer.from([InputTypes.REQUIRED_TIME_LOCKTIME]);

const value = Buffer.alloc(4);
value.writeUInt32LE(data, 0);

return {
key,
value,
};
}

export const expected = 'Buffer';

export function check(data: any): data is RequiredTimeLocktime {
return (
typeof data === 'number' && !isNaN(data) && data < 0xffffffff && data >= 0 //add check if it's > 500000000 ?
);
}
43 changes: 43 additions & 0 deletions ts_src/lib/converter/input/ripemd160.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { KeyValue, RIPEMD160 } from '../../interfaces';
import { InputTypes } from '../../typeFields';
import * as crypto from 'crypto';

export function decode(keyVal: KeyValue): RIPEMD160 {
if (keyVal.key[0] !== InputTypes.RIPEMD160) {
throw new Error(
'Decore Error: could not decode ripemd160 with key 0x' +
keyVal.key.toString('hex'),
);
}

return {
preimage: keyVal.value,
hash: keyVal.key.slice(1),
};
}

export function encode(data: RIPEMD160): KeyValue {
const head = Buffer.from([InputTypes.RIPEMD160]);
const key = Buffer.concat([head, data.hash]);

const value = data.preimage;

return {
key,
value,
};
}

export const expected = `{ preimage: Buffer; hash: Buffer; }`;

export function check(data: any): data is RIPEMD160 {
return (
Buffer.isBuffer(data.preimage) &&
Buffer.isBuffer(data.hash) &&
crypto
.createHash('ripemd160')
.update(data.preimage)
.digest()
.compare(data.hash) == 0
);
}
32 changes: 32 additions & 0 deletions ts_src/lib/converter/input/sequenceNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { KeyValue, SequenceNumber } from '../../interfaces';
import { InputTypes } from '../../typeFields';

export function decode(keyVal: KeyValue): SequenceNumber {
if (keyVal.key[0] !== InputTypes.SEQUENCE_NUMBER) {
throw new Error(
'Decode Error: could not decode spentOutputIndex with key 0x' +
keyVal.key.toString('hex'),
);
}
return keyVal.value.readUInt32LE(0);
}

export function encode(data: SequenceNumber): KeyValue {
const key = Buffer.from([InputTypes.SEQUENCE_NUMBER]);

const value = Buffer.alloc(4);
value.writeUInt32LE(data, 0);

return {
key,
value,
};
}

export const expected = 'Buffer';

export function check(data: any): data is SequenceNumber {
return (
typeof data === 'number' && !isNaN(data) && data < 0xffffffff && data >= 0
);
}
43 changes: 43 additions & 0 deletions ts_src/lib/converter/input/sha256.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { KeyValue, SHA256 } from '../../interfaces';
import { InputTypes } from '../../typeFields';
import * as crypto from 'crypto';

export function decode(keyVal: KeyValue): SHA256 {
if (keyVal.key[0] !== InputTypes.SHA256) {
throw new Error(
'Decore Error: could not decode sha256 with key 0x' +
keyVal.key.toString('hex'),
);
}

return {
preimage: keyVal.value,
hash: keyVal.key.slice(1),
};
}

export function encode(data: SHA256): KeyValue {
const head = Buffer.from([InputTypes.SHA256]);
const key = Buffer.concat([head, data.hash]);

const value = data.preimage;

return {
key,
value,
};
}

export const expected = `{ preimage: Buffer; hash: Buffer; }`;

export function check(data: any): data is SHA256 {
return (
Buffer.isBuffer(data.preimage) &&
Buffer.isBuffer(data.hash) &&
crypto
.createHash('sha256')
.update(data.preimage)
.digest()
.compare(data.hash) == 0
);
}
31 changes: 31 additions & 0 deletions ts_src/lib/converter/input/spentOutputIndex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { KeyValue, SpentOutputIndex } from '../../interfaces';
import { InputTypes } from '../../typeFields';

export function decode(keyVal: KeyValue): SpentOutputIndex {
if (keyVal.key[0] !== InputTypes.SPENT_OUTPUT_INDEX) {
throw new Error(
'Decode Error: could not decode spentOutputIndex with key 0x' +
keyVal.key.toString('hex'),
);
}
return keyVal.value.readUInt32LE(0);
}

export function encode(data: SpentOutputIndex): KeyValue {
const key = Buffer.from([InputTypes.SPENT_OUTPUT_INDEX]);
const value = Buffer.alloc(4);
value.writeUInt32LE(data, 0);

return {
key,
value,
};
}

export const expected = 'Buffer';

export function check(data: any): data is SpentOutputIndex {
return (
typeof data === 'number' && !isNaN(data) && data < 0xffffffff && data >= 0
);
}
34 changes: 34 additions & 0 deletions ts_src/lib/converter/output/outputAmount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { KeyValue, OutputAmount } from '../../interfaces';
import { InputTypes } from '../../typeFields';

export function decode(keyVal: KeyValue): OutputAmount {
if (keyVal.key[0] !== InputTypes.SEQUENCE_NUMBER) {
throw new Error(
'Decode Error: could not decode outputAmount with key 0x' +
keyVal.key.toString('hex'),
);
}
return +keyVal.value.readBigInt64BE(0).toString();
}

export function encode(data: OutputAmount): KeyValue {
const key = Buffer.from([InputTypes.SEQUENCE_NUMBER]);

const value = Buffer.alloc(8);
value.writeBigInt64BE(BigInt(data), 0);

return {
key,
value,
};
}

export const expected = 'Buffer';

export function check(data: any): data is OutputAmount {
return (
typeof data === 'number' &&
!isNaN(data) &&
data <= BigInt('0x7fffffffffffffff')
);
}
Loading