|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/hex" |
4 | 5 | "flag" |
5 | 6 | "fmt" |
6 | | - bitstream "github.com/pyropy/bitstream-go/pkg" |
7 | 7 | "io" |
8 | 8 | "log" |
9 | 9 | "os" |
| 10 | + |
| 11 | + "github.com/btcsuite/btcd/btcec/v2" |
| 12 | + bitstream "github.com/pyropy/bitstream-go/pkg" |
10 | 13 | ) |
11 | 14 |
|
12 | 15 | var ( |
13 | | - inPath string |
14 | | - outPath string |
15 | | - preimage string |
16 | | - chunkSize int64 |
| 16 | + action string |
| 17 | + inPath string |
| 18 | + outPath string |
| 19 | + preimage string |
| 20 | + paymentHash string |
| 21 | + chunkSize int64 |
| 22 | + pk *btcec.PrivateKey |
17 | 23 | ) |
18 | 24 |
|
19 | 25 | func init() { |
| 26 | + flag.StringVar(&action, "action", "encrypt", "encrypt or decrypt") |
20 | 27 | flag.StringVar(&inPath, "in", "", "File to encrypt/decrypt") |
21 | 28 | flag.StringVar(&outPath, "out", "", "Path to output encrypted/decrypted file") |
22 | 29 | flag.StringVar(&preimage, "preimage", "", "Preimage to use for encryption/decryption") |
| 30 | + flag.StringVar(&paymentHash, "hash", "", "Payment hash") |
23 | 31 | flag.Int64Var(&chunkSize, "size", 32, "Size of chunks to encrypt/decrypt") |
| 32 | + |
| 33 | + pkBytes, err := hex.DecodeString("80faa7d1c150a903a4028bf87ca8800aff507b24df90e8434bfa1f34d639c053") |
| 34 | + if err != nil { |
| 35 | + panic(err) |
| 36 | + } |
| 37 | + |
| 38 | + pk, _ = btcec.PrivKeyFromBytes(pkBytes) |
24 | 39 | } |
25 | 40 |
|
26 | 41 | // TODO: Implement encrypt and decrypt cli commands |
27 | 42 | func main() { |
28 | 43 | flag.Parse() |
29 | 44 |
|
30 | | - err := encrypt(inPath, outPath) |
| 45 | + var err error |
| 46 | + |
| 47 | + switch action { |
| 48 | + case "encrypt": |
| 49 | + err = encrypt(inPath, outPath, preimage, chunkSize) |
| 50 | + case "decrypt": |
| 51 | + err = decrypt(inPath, outPath, preimage, chunkSize) |
| 52 | + } |
| 53 | + |
31 | 54 | if err != nil { |
32 | 55 | panic(err) |
33 | 56 | } |
34 | 57 | } |
35 | 58 |
|
36 | | -func encrypt(inPath, outPath string) error { |
| 59 | +func encrypt(inPath, outPath, preimage string, chunkSize int64) error { |
37 | 60 | f, err := os.Open(inPath) |
38 | 61 | if err != nil { |
39 | 62 | return err |
40 | 63 | } |
41 | 64 |
|
42 | | - encryptedFile, tree, err := bitstream.EncryptFile([]byte(preimage), f, chunkSize) |
| 65 | + defer f.Close() |
| 66 | + |
| 67 | + out, err := os.OpenFile(outPath, os.O_RDWR|os.O_CREATE, 0644) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + preimageBytes, err := hex.DecodeString(preimage) |
| 73 | + if err != nil { |
| 74 | + panic(err) |
| 75 | + } |
| 76 | + |
| 77 | + paymentHashBytes, err := hex.DecodeString(paymentHash) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + err = bitstream.Encrypt(pk, paymentHashBytes, preimageBytes, f, out, chunkSize) |
43 | 83 | if err != nil { |
44 | 84 | return err |
45 | 85 | } |
46 | 86 |
|
47 | | - l := fmt.Sprintf("Merkle tree hash 0x%x", tree.GetHash()) |
48 | | - log.Println(l) |
| 87 | + return nil |
| 88 | +} |
49 | 89 |
|
50 | | - b, err := io.ReadAll(encryptedFile) |
| 90 | +func decrypt(inPath, outPath, preimage string, chunkSize int64) error { |
| 91 | + f, err := os.Open(inPath) |
51 | 92 | if err != nil { |
52 | 93 | return err |
53 | 94 | } |
54 | 95 |
|
55 | | - err = os.WriteFile(outPath, b, 0644) |
| 96 | + defer f.Close() |
| 97 | + |
| 98 | + preimageBytes, err := hex.DecodeString(preimage) |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + |
| 103 | + decryptedFile, index, err := bitstream.Decrypt(preimageBytes, f, chunkSize) |
| 104 | + if err != nil { |
| 105 | + if index != -1 { |
| 106 | + proof, err := bitstream.GenerateProof(f, chunkSize, 2*index) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + |
| 111 | + root := fmt.Sprintf("%x", proof.MerkleProof.Root) |
| 112 | + log.Println("Generated proof", "proof root", root) |
| 113 | + } |
| 114 | + |
| 115 | + return err |
| 116 | + } |
| 117 | + |
| 118 | + decrypted, err := io.ReadAll(decryptedFile) |
56 | 119 | if err != nil { |
57 | 120 | return err |
58 | 121 | } |
59 | 122 |
|
60 | | - err = f.Close() |
| 123 | + err = os.WriteFile(outPath, decrypted, 0644) |
61 | 124 | if err != nil { |
62 | 125 | return err |
63 | 126 | } |
|
0 commit comments