-
Notifications
You must be signed in to change notification settings - Fork 19
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
157d32d
commit b4111ad
Showing
20 changed files
with
1,019 additions
and
62 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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Tapleaf } from '../consts/Types'; | ||
import { LiquidSwapTree } from './consts/Types'; | ||
|
||
type ProbabilityNode<T> = { probability: number; value: T }; | ||
|
||
type TreeNode<T> = Tree<T> | T; | ||
type Tree<T> = [TreeNode<T>, TreeNode<T>]; | ||
|
||
const subSortTree = <T>(nodes: ProbabilityNode<T>[]): TreeNode<T> => { | ||
if (nodes.length === 1) { | ||
return nodes[0].value; | ||
} else if (nodes.length === 2) { | ||
return [nodes[0].value, nodes[1].value]; | ||
} | ||
|
||
const sum = nodes.reduce((sum, node) => sum + node.probability, 0); | ||
|
||
let mid = 0; | ||
let midSum = 0; | ||
|
||
while (midSum < sum / 2) { | ||
midSum += nodes[mid].probability; | ||
mid++; | ||
} | ||
|
||
return [subSortTree(nodes.slice(0, mid)), subSortTree(nodes.slice(mid))]; | ||
}; | ||
|
||
export const sortTree = <T>(nodes: ProbabilityNode<T>[]): TreeNode<T> => | ||
subSortTree(nodes.sort((a, b) => b.probability - a.probability)); | ||
|
||
export const assignTreeProbabilities = ( | ||
tree: Omit<LiquidSwapTree, 'tree'>, | ||
): ProbabilityNode<Tapleaf>[] => { | ||
if (tree.covenantClaimLeaf) { | ||
return [ | ||
{ | ||
probability: 51, | ||
value: tree.covenantClaimLeaf, | ||
}, | ||
{ | ||
probability: 25, | ||
value: tree.claimLeaf, | ||
}, | ||
{ | ||
probability: 24, | ||
value: tree.refundLeaf, | ||
}, | ||
]; | ||
} | ||
|
||
return [ | ||
{ | ||
probability: 51, | ||
value: tree.claimLeaf, | ||
}, | ||
{ | ||
probability: 49, | ||
value: tree.refundLeaf, | ||
}, | ||
]; | ||
}; | ||
|
||
export { Tree, TreeNode, ProbabilityNode }; |
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 @@ | ||
// Reference: https://github.com/ElementsProject/elements/blob/master/doc/tapscript_opcodes.md#new-opcodes-for-additional-functionality | ||
export default { | ||
OP_INSPECTOUTPUTSCRIPTPUBKEY: 0xd1, | ||
OP_INSPECTOUTPUTASSET: 0xce, | ||
OP_INSPECTOUTPUTVALUE: 0xcf, | ||
}; |
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 |
---|---|---|
@@ -1,12 +1,18 @@ | ||
import { BIP32Interface } from 'bip32'; | ||
import { ECPairInterface } from 'ecpair'; | ||
import { Transaction, TxOutput } from 'liquidjs-lib'; | ||
import { RefundDetails } from '../../consts/Types'; | ||
import { RefundDetails, SwapTree, Tapleaf } from '../../consts/Types'; | ||
|
||
export type LiquidRefundDetails = Omit<RefundDetails, 'value'> & | ||
export type LiquidSwapTree = SwapTree & { covenantClaimLeaf?: Tapleaf }; | ||
|
||
export type LiquidRefundDetails = Omit<RefundDetails, 'value' | 'swapTree'> & | ||
TxOutput & { | ||
legacyTx?: Transaction; | ||
swapTree?: LiquidSwapTree; | ||
blindingPrivateKey?: Buffer; | ||
}; | ||
|
||
export type LiquidClaimDetails = LiquidRefundDetails & { | ||
export type LiquidClaimDetails = Omit<LiquidRefundDetails, 'keys'> & { | ||
preimage: Buffer; | ||
keys?: ECPairInterface | BIP32Interface; | ||
}; |
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 |
---|---|---|
@@ -1,18 +1,29 @@ | ||
import { getOutputValue } from './Utils'; | ||
import * as Utils from './Utils'; | ||
import Networks from './consts/Networks'; | ||
import ops from './consts/Ops'; | ||
import { LiquidClaimDetails, LiquidRefundDetails } from './consts/Types'; | ||
import { init } from './init'; | ||
import { constructClaimTransaction } from './swap/Claim'; | ||
import { constructRefundTransaction } from './swap/Refund'; | ||
import reverseSwapTree, { | ||
Feature, | ||
FeatureOption, | ||
} from './swap/ReverseSwapTree'; | ||
import * as TaprootUtils from './swap/TaprootUtils'; | ||
|
||
export { | ||
ops, | ||
Utils, | ||
Feature, | ||
Networks, | ||
TaprootUtils, | ||
FeatureOption, | ||
LiquidClaimDetails, | ||
LiquidRefundDetails, | ||
init, | ||
getOutputValue, | ||
reverseSwapTree, | ||
constructClaimTransaction, | ||
constructRefundTransaction, | ||
}; |
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
Oops, something went wrong.