|
| 1 | +// Bitcoin Transaction Tool |
| 2 | +// Copyright (c) 2017 Coding Enthusiast |
| 3 | +// Distributed under the MIT software license, see the accompanying |
| 4 | +// file LICENCE or http://www.opensource.org/licenses/mit-license.php. |
| 5 | + |
| 6 | +using BitcoinTransactionTool.Backend.Blockchain.Scripts; |
| 7 | +using BitcoinTransactionTool.Backend.Blockchain.Scripts.Operations; |
| 8 | +using BitcoinTransactionTool.Backend.Encoders; |
| 9 | +using BitcoinTransactionTool.Backend.MVVM; |
| 10 | +using System; |
| 11 | +using System.Collections.Generic; |
| 12 | +using System.Linq; |
| 13 | + |
| 14 | +namespace BitcoinTransactionTool.ViewModels |
| 15 | +{ |
| 16 | + public class ScrHashCollisionViewModel : ScriptVMBase |
| 17 | + { |
| 18 | + public ScrHashCollisionViewModel() |
| 19 | + { |
| 20 | + VmName = "Hash collision puzzles"; |
| 21 | + Description = "Build complete signature script for claiming the hash collision puzzles. " + |
| 22 | + "More information could be found at https://bitcointalk.org/index.php?topic=293382.0 "; |
| 23 | + |
| 24 | + PuzzleList = Enum.GetValues(typeof(PuzzleTypes)).Cast<PuzzleTypes>(); |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + public enum PuzzleTypes |
| 30 | + { |
| 31 | + SHA1, |
| 32 | + SHA256, |
| 33 | + RIPEMD160, |
| 34 | + RIPEMD160_SHA256, |
| 35 | + SHA256_double, |
| 36 | + Absolute |
| 37 | + } |
| 38 | + |
| 39 | + public IEnumerable<PuzzleTypes> PuzzleList { get; private set; } |
| 40 | + |
| 41 | + private PuzzleTypes _selPuz; |
| 42 | + public PuzzleTypes SelectedPuzzle |
| 43 | + { |
| 44 | + get => _selPuz; |
| 45 | + set => SetField(ref _selPuz, value); |
| 46 | + } |
| 47 | + |
| 48 | + private bool _isRun; |
| 49 | + public bool MakeRunnable |
| 50 | + { |
| 51 | + get => _isRun; |
| 52 | + set => SetField(ref _isRun, value); |
| 53 | + } |
| 54 | + |
| 55 | + public string MakeRunnableToolTip => |
| 56 | + "If checked, the redeem script part will be pulled out of the signature script " + |
| 57 | + "so that it could be run here." + Environment.NewLine + |
| 58 | + "When unchecked, the result is a valid signature script."; |
| 59 | + |
| 60 | + |
| 61 | + private string _d1; |
| 62 | + public string Data1 |
| 63 | + { |
| 64 | + get => _d1; |
| 65 | + set => SetField(ref _d1, value); |
| 66 | + } |
| 67 | + |
| 68 | + private string _d2; |
| 69 | + public string Data2 |
| 70 | + { |
| 71 | + get => _d2; |
| 72 | + set => SetField(ref _d2, value); |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + public string Example1ToolTip => |
| 77 | + "SHA1 hash collision example found in txid=8d31992805518fd62daa3bdd2a5c4fd2cd3054c9b3dca1d78055e9528cff6adc"; |
| 78 | + public string Example2ToolTip => "Absolute valueS of unequal numbers i and -i are equal!"; |
| 79 | + |
| 80 | + |
| 81 | + public RelayCommand Example1Command => new RelayCommand(Example1); |
| 82 | + private void Example1() |
| 83 | + { |
| 84 | + SelectedPuzzle = PuzzleTypes.SHA1; |
| 85 | + Data1 = "255044462d312e330a25e2e3cfd30a0a0a312030206f626a0a3c3c2f57696474682032203020522f4865696768742033203020522f547970652034203020522f537562747970652035203020522f46696c7465722036203020522f436f6c6f7253706163652037203020522f4c656e6774682038203020522f42697473506572436f6d706f6e656e7420383e3e0a73747265616d0affd8fffe00245348412d3120697320646561642121212121852fec092339759c39b1a1c63c4c97e1fffe017f46dc93a6b67e013b029aaa1db2560b45ca67d688c7f84b8c4c791fe02b3df614f86db1690901c56b45c1530afedfb76038e972722fe7ad728f0e4904e046c230570fe9d41398abe12ef5bc942be33542a4802d98b5d70f2a332ec37fac3514e74ddc0f2cc1a874cd0c78305a21566461309789606bd0bf3f98cda8044629a1"; |
| 86 | + Data2 = "255044462d312e330a25e2e3cfd30a0a0a312030206f626a0a3c3c2f57696474682032203020522f4865696768742033203020522f547970652034203020522f537562747970652035203020522f46696c7465722036203020522f436f6c6f7253706163652037203020522f4c656e6774682038203020522f42697473506572436f6d706f6e656e7420383e3e0a73747265616d0affd8fffe00245348412d3120697320646561642121212121852fec092339759c39b1a1c63c4c97e1fffe017346dc9166b67e118f029ab621b2560ff9ca67cca8c7f85ba84c79030c2b3de218f86db3a90901d5df45c14f26fedfb3dc38e96ac22fe7bd728f0e45bce046d23c570feb141398bb552ef5a0a82be331fea48037b8b5d71f0e332edf93ac3500eb4ddc0decc1a864790c782c76215660dd309791d06bd0af3f98cda4bc4629b1"; |
| 87 | + } |
| 88 | + |
| 89 | + public RelayCommand Example2Command => new RelayCommand(Example2); |
| 90 | + private void Example2() |
| 91 | + { |
| 92 | + SelectedPuzzle = PuzzleTypes.Absolute; |
| 93 | + Random rand = new Random(); |
| 94 | + int i = rand.Next(); |
| 95 | + Data1 = i.ToString(); |
| 96 | + Data2 = (-i).ToString(); |
| 97 | + } |
| 98 | + |
| 99 | + public override bool SetOperations() |
| 100 | + { |
| 101 | + Errors = null; |
| 102 | + |
| 103 | + PushDataOp push1, push2; |
| 104 | + IOperation op = null; |
| 105 | + if (SelectedPuzzle == PuzzleTypes.Absolute) |
| 106 | + { |
| 107 | + if (!int.TryParse(Data1, out int a) || !int.TryParse(Data2, out int b)) |
| 108 | + { |
| 109 | + Errors = "Data inputs for absolute puzzle must be a number."; |
| 110 | + return false; |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + push1 = new PushDataOp(a); |
| 115 | + push2 = new PushDataOp(b); |
| 116 | + op = new ABSOp(); |
| 117 | + } |
| 118 | + } |
| 119 | + else if (!Base16.IsValid(Data1) || !Base16.IsValid(Data2)) |
| 120 | + { |
| 121 | + Errors = "Data inputs for hash puzzles must be using base-16 encoding."; |
| 122 | + return false; |
| 123 | + } |
| 124 | + else |
| 125 | + { |
| 126 | + byte[] ba1 = Base16.ToByteArray(Data1); |
| 127 | + byte[] ba2 = Base16.ToByteArray(Data2); |
| 128 | + |
| 129 | + push1 = new PushDataOp(ba1); |
| 130 | + push2 = new PushDataOp(ba2); |
| 131 | + |
| 132 | + switch (SelectedPuzzle) |
| 133 | + { |
| 134 | + case PuzzleTypes.SHA1: |
| 135 | + op = new Sha1Op(); |
| 136 | + break; |
| 137 | + case PuzzleTypes.SHA256: |
| 138 | + op = new Sha256Op(); |
| 139 | + break; |
| 140 | + case PuzzleTypes.RIPEMD160: |
| 141 | + op = new RipeMd160Op(); |
| 142 | + break; |
| 143 | + case PuzzleTypes.RIPEMD160_SHA256: |
| 144 | + new Hash160Op(); |
| 145 | + break; |
| 146 | + case PuzzleTypes.SHA256_double: |
| 147 | + op = new Hash256Op(); |
| 148 | + break; |
| 149 | + default: |
| 150 | + Errors = "Puzzle type was not defined."; |
| 151 | + return false; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + if (MakeRunnable) |
| 156 | + { |
| 157 | + OpsToAdd = new IOperation[] |
| 158 | + { |
| 159 | + push1, |
| 160 | + push2, |
| 161 | + new DUP2Op(), |
| 162 | + new EqualOp(), |
| 163 | + new NOTOp(), |
| 164 | + new VerifyOp(), |
| 165 | + op, |
| 166 | + new SWAPOp(), |
| 167 | + op, |
| 168 | + new EqualOp() |
| 169 | + }; |
| 170 | + } |
| 171 | + else |
| 172 | + { |
| 173 | + RedeemScript rdm = new RedeemScript() |
| 174 | + { |
| 175 | + OperationList = new IOperation[] |
| 176 | + { |
| 177 | + new DUP2Op(), |
| 178 | + new EqualOp(), |
| 179 | + new NOTOp(), |
| 180 | + new VerifyOp(), |
| 181 | + op, |
| 182 | + new SWAPOp(), |
| 183 | + op, |
| 184 | + new EqualOp() |
| 185 | + } |
| 186 | + }; |
| 187 | + |
| 188 | + OpsToAdd = new IOperation[] |
| 189 | + { |
| 190 | + push1, |
| 191 | + push2, |
| 192 | + new PushDataOp(rdm) |
| 193 | + }; |
| 194 | + } |
| 195 | + |
| 196 | + |
| 197 | + return true; |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments