-
Notifications
You must be signed in to change notification settings - Fork 47
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
f89e6c5
commit f64d6ae
Showing
17 changed files
with
722 additions
and
242 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
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,40 +1,73 @@ | ||
import axios from 'axios'; | ||
import { SwapUpdateEvent } from '../consts/Enums'; | ||
|
||
type PartialSignature = { | ||
pubNonce: string; | ||
partialSignature: string; | ||
}; | ||
|
||
class BoltzApiClient { | ||
public static readonly regtestEndpoint = 'http://127.0.0.1:9001'; | ||
|
||
constructor(private readonly endpoint = BoltzApiClient.regtestEndpoint) {} | ||
|
||
public getStatus = async ( | ||
swapId: string, | ||
): Promise<{ | ||
status: SwapUpdateEvent; | ||
transaction?: { | ||
hex: string; | ||
}; | ||
}> => | ||
( | ||
await axios.post(`${this.endpoint}/swapstatus`, { | ||
id: swapId, | ||
}) | ||
).data; | ||
|
||
public getSwapTransaction = async ( | ||
swapId: string, | ||
): Promise<{ | ||
transactionHex: string; | ||
}> => { | ||
return ( | ||
}> => | ||
( | ||
await axios.post(`${this.endpoint}/getswaptransaction`, { | ||
id: swapId, | ||
}) | ||
).data; | ||
}; | ||
|
||
public refundSwap = async ( | ||
public getSwapRefundPartialSignature = async ( | ||
swapId: string, | ||
transaction: string, | ||
vin: number, | ||
pubNonce: string, | ||
): Promise<{ | ||
pubNonce: string; | ||
partialSignature: string; | ||
}> => { | ||
return ( | ||
): Promise<PartialSignature> => | ||
( | ||
await axios.post(`${this.endpoint}/v2/swap/submarine/refund`, { | ||
pubNonce, | ||
transaction, | ||
id: swapId, | ||
index: vin, | ||
}) | ||
).data; | ||
}; | ||
|
||
public getReverseClaimPartialSignature = async ( | ||
swapId: string, | ||
preimage: string, | ||
transaction: string, | ||
vin: number, | ||
pubNonce: string, | ||
): Promise<PartialSignature> => | ||
( | ||
await axios.post(`${this.endpoint}/v2/swap/reverse/claim`, { | ||
pubNonce, | ||
preimage, | ||
transaction, | ||
id: swapId, | ||
index: vin, | ||
}) | ||
).data; | ||
} | ||
|
||
export default BoltzApiClient; | ||
export { PartialSignature }; |
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,138 @@ | ||
import { Arguments } from 'yargs'; | ||
import { randomBytes } from 'crypto'; | ||
import { ECPairInterface } from 'ecpair'; | ||
import { Network, Transaction } from 'bitcoinjs-lib'; | ||
import { LiquidRefundDetails } from 'boltz-core/lib/liquid'; | ||
Check failure on line 5 in lib/cli/TaprootHelper.ts
|
||
import { | ||
detectSwap, | ||
Musig, | ||
OutputType, | ||
RefundDetails, | ||
SwapTreeSerializer, | ||
TaprootUtils, | ||
Types, | ||
} from 'boltz-core'; | ||
import { Network as LiquidNetwork } from 'liquidjs-lib/src/networks'; | ||
import { Transaction as LiquidTransaction } from 'liquidjs-lib/src/transaction'; | ||
import { TaprootUtils as LiquidTaprootDetails } from 'boltz-core/dist/lib/liquid'; | ||
import { getHexBuffer } from '../Utils'; | ||
import { ECPair } from '../ECPairHelper'; | ||
import { CurrencyType } from '../consts/Enums'; | ||
import { PartialSignature } from './BoltzApiClient'; | ||
import { | ||
constructClaimTransaction, | ||
setup, | ||
tweakMusig, | ||
zkpMusig, | ||
} from '../Core'; | ||
import { | ||
currencyTypeFromNetwork, | ||
getWalletStub, | ||
parseNetwork, | ||
} from './Command'; | ||
|
||
export const setupCooperativeTransaction = async ( | ||
argv: Arguments<any>, | ||
keyExtractionFunc: (tree: Types.SwapTree) => Buffer, | ||
) => { | ||
await setup(); | ||
|
||
const network = parseNetwork(argv.network); | ||
const currencyType = currencyTypeFromNetwork(argv.network); | ||
|
||
const swapTree = SwapTreeSerializer.deserializeSwapTree(argv.swapTree); | ||
const keys = ECPair.fromPrivateKey(getHexBuffer(argv.privateKey)); | ||
const theirPublicKey = keyExtractionFunc(swapTree); | ||
|
||
const musig = new Musig(zkpMusig, keys, randomBytes(32), [ | ||
theirPublicKey, | ||
keys.publicKey, | ||
]); | ||
const tweakedKey = tweakMusig(currencyType, musig, swapTree); | ||
|
||
return { | ||
keys, | ||
musig, | ||
network, | ||
tweakedKey, | ||
currencyType, | ||
theirPublicKey, | ||
}; | ||
}; | ||
|
||
export const prepareCooperativeTransaction = < | ||
T extends Transaction | LiquidTransaction, | ||
>( | ||
argv: Arguments<any>, | ||
network: Network | LiquidNetwork, | ||
currencyType: CurrencyType, | ||
keys: ECPairInterface, | ||
tweakedKey: Buffer, | ||
lockupTx: T, | ||
): { tx: T; details: RefundDetails | LiquidRefundDetails } => { | ||
const swapOutput = detectSwap(tweakedKey, lockupTx); | ||
if (swapOutput === undefined) { | ||
throw 'could not find swap output'; | ||
} | ||
|
||
const details = { | ||
...swapOutput, | ||
keys, | ||
txHash: lockupTx.getHash(), | ||
type: OutputType.Taproot, | ||
cooperative: true, | ||
} as any; | ||
const tx = constructClaimTransaction( | ||
getWalletStub( | ||
currencyType, | ||
network, | ||
argv.destinationAddress, | ||
argv.blindingKey, | ||
), | ||
[details], | ||
argv.destinationAddress, | ||
argv.feePerVbyte, | ||
); | ||
|
||
return { | ||
details, | ||
tx: tx as T, | ||
}; | ||
}; | ||
|
||
export const finalizeCooperativeTransaction = < | ||
T extends Transaction | LiquidTransaction, | ||
>( | ||
tx: T, | ||
musig: Musig, | ||
network: Network | LiquidNetwork, | ||
currencyType: CurrencyType, | ||
otherPublicKey: Buffer, | ||
details: RefundDetails | LiquidRefundDetails, | ||
partialSig: PartialSignature, | ||
): T => { | ||
musig.aggregateNonces([[otherPublicKey, getHexBuffer(partialSig.pubNonce)]]); | ||
|
||
let hash: Buffer; | ||
if (currencyType === CurrencyType.BitcoinLike) { | ||
hash = TaprootUtils.hashForWitnessV1( | ||
[details] as RefundDetails[], | ||
tx as Transaction, | ||
0, | ||
); | ||
} else { | ||
hash = LiquidTaprootDetails.hashForWitnessV1( | ||
network as LiquidNetwork, | ||
[details] as LiquidRefundDetails[], | ||
tx as LiquidTransaction, | ||
0, | ||
); | ||
} | ||
|
||
musig.initializeSession(hash); | ||
musig.signPartial(); | ||
musig.addPartial(otherPublicKey, getHexBuffer(partialSig.partialSignature)); | ||
tx.setWitness(0, [musig.aggregatePartials()]); | ||
|
||
return tx; | ||
}; |
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.