-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bcd895d
commit 7ae8a57
Showing
38 changed files
with
1,426 additions
and
18 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
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,6 @@ | ||
import { KeyValue, TapKeySig } from '../../interfaces'; | ||
export declare function decode(keyVal: KeyValue): TapKeySig; | ||
export declare function encode(value: TapKeySig): KeyValue; | ||
export declare const expected = "Buffer"; | ||
export declare function check(data: any): data is TapKeySig; | ||
export declare function canAdd(currentData: any, newData: any): boolean; |
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,32 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const typeFields_1 = require('../../typeFields'); | ||
function decode(keyVal) { | ||
if (keyVal.key[0] !== typeFields_1.InputTypes.TAP_KEY_SIG) { | ||
throw new Error( | ||
'Decode Error: could not decode tapKeySig with key 0x' + | ||
keyVal.key.toString('hex'), | ||
); | ||
} | ||
if (!check(keyVal.value)) { | ||
throw new Error( | ||
'Decode Error: tapKeySig not a valid 64-65-byte BIP340 signature', | ||
); | ||
} | ||
return keyVal.value; | ||
} | ||
exports.decode = decode; | ||
function encode(value) { | ||
const key = Buffer.from([typeFields_1.InputTypes.TAP_KEY_SIG]); | ||
return { key, value }; | ||
} | ||
exports.encode = encode; | ||
exports.expected = 'Buffer'; | ||
function check(data) { | ||
return Buffer.isBuffer(data) && (data.length === 64 || data.length === 65); | ||
} | ||
exports.check = check; | ||
function canAdd(currentData, newData) { | ||
return !!currentData && !!newData && currentData.tapKeySig === undefined; | ||
} | ||
exports.canAdd = canAdd; |
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,6 @@ | ||
import { KeyValue, TapLeafScript } from '../../interfaces'; | ||
export declare function decode(keyVal: KeyValue): TapLeafScript; | ||
export declare function encode(tScript: TapLeafScript): KeyValue; | ||
export declare const expected = "{ controlBlock: Buffer; leafVersion: number, script: Buffer; }"; | ||
export declare function check(data: any): data is TapLeafScript; | ||
export declare function canAddToArray(array: TapLeafScript[], item: TapLeafScript, dupeSet: Set<string>): boolean; |
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,62 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const typeFields_1 = require('../../typeFields'); | ||
const varuint = require('../varint'); | ||
function decode(keyVal) { | ||
if (keyVal.key[0] !== typeFields_1.InputTypes.TAP_LEAF_SCRIPT) { | ||
throw new Error( | ||
'Decode Error: could not decode tapLeafScript with key 0x' + | ||
keyVal.key.toString('hex'), | ||
); | ||
} | ||
if ((keyVal.key.length - 2) % 32 !== 0) { | ||
throw new Error( | ||
'Decode Error: tapLeafScript has invalid control block in key 0x' + | ||
keyVal.key.toString('hex'), | ||
); | ||
} | ||
const leafVersion = keyVal.value[keyVal.value.length - 1]; | ||
if ((keyVal.key[1] & 0xfe) !== leafVersion) { | ||
throw new Error( | ||
'Decode Error: tapLeafScript bad leaf version in key 0x' + | ||
keyVal.key.toString('hex'), | ||
); | ||
} | ||
const script = keyVal.value.slice(0, -1); | ||
const controlBlock = keyVal.key.slice(1); | ||
return { controlBlock, script, leafVersion }; | ||
} | ||
exports.decode = decode; | ||
function encode(tScript) { | ||
const head = Buffer.from([typeFields_1.InputTypes.TAP_LEAF_SCRIPT]); | ||
const _offset = varuint.encodingLength(tScript.script.length); | ||
const value = Buffer.allocUnsafe(_offset + tScript.script.length + 1); | ||
varuint.encode(tScript.script.length, value); | ||
value[_offset + tScript.script.length] = tScript.leafVersion; | ||
tScript.script.copy(value, _offset); | ||
return { | ||
key: Buffer.concat([head, tScript.controlBlock]), | ||
value, | ||
}; | ||
} | ||
exports.encode = encode; | ||
exports.expected = | ||
'{ controlBlock: Buffer; leafVersion: number, script: Buffer; }'; | ||
function check(data) { | ||
return ( | ||
Buffer.isBuffer(data.controlBlock) && | ||
(data.controlBlock.length - 2) % 32 === 0 && | ||
(data.leafVersion & 0xfe) === data.leafVersion && | ||
Buffer.isBuffer(data.script) | ||
); | ||
} | ||
exports.check = check; | ||
function canAddToArray(array, item, dupeSet) { | ||
const dupeString = item.controlBlock.toString('hex'); | ||
if (dupeSet.has(dupeString)) return false; | ||
dupeSet.add(dupeString); | ||
return ( | ||
array.filter(v => v.controlBlock.equals(item.controlBlock)).length === 0 | ||
); | ||
} | ||
exports.canAddToArray = canAddToArray; |
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,6 @@ | ||
import { KeyValue, TapMerkleRoot } from '../../interfaces'; | ||
export declare function decode(keyVal: KeyValue): TapMerkleRoot; | ||
export declare function encode(value: TapMerkleRoot): KeyValue; | ||
export declare const expected = "Buffer"; | ||
export declare function check(data: any): data is TapMerkleRoot; | ||
export declare function canAdd(currentData: any, newData: any): boolean; |
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,30 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const typeFields_1 = require('../../typeFields'); | ||
function decode(keyVal) { | ||
if (keyVal.key[0] !== typeFields_1.InputTypes.TAP_MERKLE_ROOT) { | ||
throw new Error( | ||
'Decode Error: could not decode tapMerkleRoot with key 0x' + | ||
keyVal.key.toString('hex'), | ||
); | ||
} | ||
if (!check(keyVal.value)) { | ||
throw new Error('Decode Error: tapMerkleRoot not a 32-byte hash'); | ||
} | ||
return keyVal.value; | ||
} | ||
exports.decode = decode; | ||
function encode(value) { | ||
const key = Buffer.from([typeFields_1.InputTypes.TAP_MERKLE_ROOT]); | ||
return { key, value }; | ||
} | ||
exports.encode = encode; | ||
exports.expected = 'Buffer'; | ||
function check(data) { | ||
return Buffer.isBuffer(data) && data.length === 32; | ||
} | ||
exports.check = check; | ||
function canAdd(currentData, newData) { | ||
return !!currentData && !!newData && currentData.tapMerkleRoot === undefined; | ||
} | ||
exports.canAdd = canAdd; |
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,6 @@ | ||
import { KeyValue, TapScriptSig } from '../../interfaces'; | ||
export declare function decode(keyVal: KeyValue): TapScriptSig; | ||
export declare function encode(tSig: TapScriptSig): KeyValue; | ||
export declare const expected = "{ pubkey: Buffer; signature: Buffer; }"; | ||
export declare function check(data: any): data is TapScriptSig; | ||
export declare function canAddToArray(array: TapScriptSig[], item: TapScriptSig, dupeSet: Set<string>): boolean; |
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,58 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const typeFields_1 = require('../../typeFields'); | ||
function decode(keyVal) { | ||
if (keyVal.key[0] !== typeFields_1.InputTypes.TAP_SCRIPT_SIG) { | ||
throw new Error( | ||
'Decode Error: could not decode tapScriptSig with key 0x' + | ||
keyVal.key.toString('hex'), | ||
); | ||
} | ||
if (keyVal.key.length !== 65) { | ||
throw new Error( | ||
'Decode Error: tapScriptSig has invalid key 0x' + | ||
keyVal.key.toString('hex'), | ||
); | ||
} | ||
if (keyVal.value.length !== 64 && keyVal.value.length !== 65) { | ||
throw new Error( | ||
'Decode Error: tapScriptSig has invalid signature in key 0x' + | ||
keyVal.key.toString('hex'), | ||
); | ||
} | ||
const pubkey = keyVal.key.slice(1, 33); | ||
const leafHash = keyVal.key.slice(33); | ||
return { | ||
pubkey, | ||
leafHash, | ||
signature: keyVal.value, | ||
}; | ||
} | ||
exports.decode = decode; | ||
function encode(tSig) { | ||
const head = Buffer.from([typeFields_1.InputTypes.TAP_SCRIPT_SIG]); | ||
return { | ||
key: Buffer.concat([head, tSig.pubkey, tSig.leafHash]), | ||
value: tSig.signature, | ||
}; | ||
} | ||
exports.encode = encode; | ||
exports.expected = '{ pubkey: Buffer; signature: Buffer; }'; | ||
function check(data) { | ||
return ( | ||
Buffer.isBuffer(data.pubkey) && | ||
Buffer.isBuffer(data.leafHash) && | ||
Buffer.isBuffer(data.signature) && | ||
data.pubkey.length === 32 && | ||
data.leafHash.length === 32 && | ||
(data.signature.length === 64 || data.signature.length === 65) | ||
); | ||
} | ||
exports.check = check; | ||
function canAddToArray(array, item, dupeSet) { | ||
const dupeString = item.pubkey.toString('hex'); | ||
if (dupeSet.has(dupeString)) return false; | ||
dupeSet.add(dupeString); | ||
return array.filter(v => v.pubkey.equals(item.pubkey)).length === 0; | ||
} | ||
exports.canAddToArray = canAddToArray; |
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,6 @@ | ||
import { KeyValue, TapTree } from '../../interfaces'; | ||
export declare function decode(keyVal: KeyValue): TapTree; | ||
export declare function encode(tree: TapTree): KeyValue; | ||
export declare const expected = "[{ depth: number; leafVersion: number, script: Buffer; }]"; | ||
export declare function check(data: any): data is TapTree; | ||
export declare function canAdd(currentData: any, newData: any): boolean; |
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,65 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const typeFields_1 = require('../../typeFields'); | ||
const varuint = require('../varint'); | ||
function decode(keyVal) { | ||
if (keyVal.key[0] !== typeFields_1.OutputTypes.TAP_TREE) { | ||
throw new Error( | ||
'Decode Error: could not decode tapTree with key 0x' + | ||
keyVal.key.toString('hex'), | ||
); | ||
} | ||
let _offset = 0; | ||
const data = []; | ||
while (_offset < keyVal.value.length) { | ||
const depth = keyVal.value[_offset++]; | ||
const leafVersion = keyVal.value[_offset++]; | ||
const scriptLen = varuint.decode(keyVal.value, _offset); | ||
_offset += varuint.encodingLength(scriptLen); | ||
data.push({ | ||
depth, | ||
leafVersion, | ||
script: keyVal.value.slice(_offset, _offset + scriptLen), | ||
}); | ||
_offset += scriptLen; | ||
} | ||
return data; | ||
} | ||
exports.decode = decode; | ||
function encode(tree) { | ||
const key = Buffer.from([typeFields_1.OutputTypes.TAP_TREE]); | ||
const bufs = new Array(tree.length * 2); | ||
for (const tapLeaf of tree) { | ||
const headBuf = Buffer.allocUnsafe( | ||
2 + varuint.encodingLength(tapLeaf.script.length), | ||
); | ||
headBuf[0] = tapLeaf.depth; | ||
headBuf[1] = tapLeaf.leafVersion; | ||
varuint.encode(tapLeaf.script.length, headBuf, 2); | ||
bufs.push(headBuf); | ||
bufs.push(tapLeaf.script); | ||
} | ||
return { | ||
key, | ||
value: Buffer.concat(bufs), | ||
}; | ||
} | ||
exports.encode = encode; | ||
exports.expected = '[{ depth: number; leafVersion: number, script: Buffer; }]'; | ||
function check(data) { | ||
return ( | ||
Array.isArray(data) && | ||
data.every( | ||
tapLeaf => | ||
tapLeaf.depth >= 0 && | ||
tapLeaf.depth <= 128 && | ||
(tapLeaf.leafVersion & 0xfe) === tapLeaf.leafVersion && | ||
Buffer.isBuffer(tapLeaf.script), | ||
) | ||
); | ||
} | ||
exports.check = check; | ||
function canAdd(currentData, newData) { | ||
return !!currentData && !!newData && currentData.tapTree === undefined; | ||
} | ||
exports.canAdd = canAdd; |
Oops, something went wrong.