|
| 1 | +namespace DotNetLightning.Channel |
| 2 | + |
| 3 | +open System |
| 4 | + |
| 5 | +open NBitcoin |
| 6 | +open NBitcoin.BuilderExtensions |
| 7 | +open DotNetLightning.Utils |
| 8 | +open DotNetLightning.Crypto |
| 9 | + |
| 10 | +open ResultUtils |
| 11 | +open ResultUtils.Portability |
| 12 | + |
| 13 | +type CommitmentAnchorParameters = |
| 14 | + { |
| 15 | + FundingPubKey: FundingPubKey |
| 16 | + } |
| 17 | + |
| 18 | + static member TryExtractParameters (scriptPubKey: Script): Option<CommitmentAnchorParameters> = |
| 19 | + let ops = |
| 20 | + scriptPubKey.ToOps() |
| 21 | + // we have to collect it into a list and convert back to a seq |
| 22 | + // because the IEnumerable that NBitcoin gives us is internally |
| 23 | + // mutable. |
| 24 | + |> List.ofSeq |
| 25 | + |> Seq.ofList |
| 26 | + let checkOpCode(opcodeType: OpcodeType) = |
| 27 | + seqParser<Op> { |
| 28 | + let! op = SeqParser.next() |
| 29 | + if op.Code = opcodeType then |
| 30 | + return () |
| 31 | + else |
| 32 | + return! SeqParser.abort() |
| 33 | + } |
| 34 | + let parseToCompletionResult = |
| 35 | + SeqParser.parseToCompletion ops <| seqParser { |
| 36 | + let! opFundingPubKey = SeqParser.next() |
| 37 | + let! fundingPubKey = seqParser { |
| 38 | + if isNull opFundingPubKey.PushData then |
| 39 | + return! SeqParser.abort() |
| 40 | + else |
| 41 | + try |
| 42 | + return FundingPubKey <| PubKey opFundingPubKey.PushData |
| 43 | + with |
| 44 | + | :? FormatException -> return! SeqParser.abort() |
| 45 | + } |
| 46 | + do! checkOpCode OpcodeType.OP_CHECKSIG |
| 47 | + do! checkOpCode OpcodeType.OP_IFDUP |
| 48 | + do! checkOpCode OpcodeType.OP_NOTIF |
| 49 | + do! checkOpCode OpcodeType.OP_16 |
| 50 | + do! checkOpCode OpcodeType.OP_CHECKSEQUENCEVERIFY |
| 51 | + do! checkOpCode OpcodeType.OP_ENDIF |
| 52 | + return { |
| 53 | + FundingPubKey = fundingPubKey |
| 54 | + } |
| 55 | + } |
| 56 | + match parseToCompletionResult with |
| 57 | + | Ok data -> Some data |
| 58 | + | Error _consumeAllError -> None |
| 59 | + |
| 60 | +type internal CommitmentAnchorExtension() = |
| 61 | + inherit BuilderExtension() |
| 62 | + override __.Match (coin: ICoin, _input: PSBTInput): bool = |
| 63 | + (CommitmentAnchorParameters.TryExtractParameters (coin.GetScriptCode())).IsSome |
| 64 | + |
| 65 | + override __.Sign(inputSigningContext: InputSigningContext, keyRepo: IKeyRepository, signer: ISigner) = |
| 66 | + let scriptPubKey = inputSigningContext.Coin.GetScriptCode() |
| 67 | + |
| 68 | + match keyRepo.FindKey scriptPubKey with |
| 69 | + | :? PubKey as pubKey when not (isNull pubKey) -> |
| 70 | + match signer.Sign pubKey with |
| 71 | + | :? TransactionSignature as signature when not (isNull signature) -> |
| 72 | + inputSigningContext.Input.PartialSigs.AddOrReplace(pubKey, signature) |
| 73 | + | _ -> () |
| 74 | + | _ -> () |
| 75 | + |
| 76 | + override __.CanDeduceScriptPubKey(_scriptSig: Script): bool = |
| 77 | + false |
| 78 | + |
| 79 | + override __.DeduceScriptPubKey(_scriptSig: Script): Script = |
| 80 | + raise <| NotSupportedException() |
| 81 | + |
| 82 | + override __.CanEstimateScriptSigSize(coin: ICoin): bool = |
| 83 | + (CommitmentAnchorParameters.TryExtractParameters (coin.GetScriptCode())).IsSome |
| 84 | + |
| 85 | + override __.EstimateScriptSigSize(_coin: ICoin): int = |
| 86 | + ChannelConstants.MaxSignatureSize |
| 87 | + |
| 88 | + override __.IsCompatibleKey(pubKey: IPubKey, scriptPubKey: Script): bool = |
| 89 | + match CommitmentAnchorParameters.TryExtractParameters scriptPubKey with |
| 90 | + | None -> false |
| 91 | + | Some parameters -> |
| 92 | + parameters.FundingPubKey.RawPubKey() :> IPubKey = pubKey |
| 93 | + |
| 94 | + |
| 95 | + override __.Finalize(inputSigningContext: InputSigningContext) = |
| 96 | + let scriptPubKey = inputSigningContext.Coin.GetScriptCode() |
| 97 | + let parameters = |
| 98 | + match (CommitmentAnchorParameters.TryExtractParameters scriptPubKey) with |
| 99 | + | Some parameters -> parameters |
| 100 | + | None -> |
| 101 | + failwith |
| 102 | + "NBitcoin should not call this unless Match returns true" |
| 103 | + |
| 104 | + let txIn = inputSigningContext.Input |
| 105 | + if txIn.PartialSigs.Count <> 0 then |
| 106 | + let keyAndSignatureOpt = |
| 107 | + txIn.PartialSigs |
| 108 | + |> Seq.tryExactlyOne |
| 109 | + match keyAndSignatureOpt with |
| 110 | + | Some keyAndSignature when keyAndSignature.Key = parameters.FundingPubKey.RawPubKey() -> |
| 111 | + inputSigningContext.Input.FinalScriptSig <- |
| 112 | + Script [ |
| 113 | + Op.GetPushOp (keyAndSignature.Value.ToBytes()) |
| 114 | + ] |
| 115 | + | _ -> () |
0 commit comments