| 103 | } |
| 104 | |
| 105 | func newBlock(args []string) { |
| 106 | fs := flag.NewFlagSet("new", flag.PanicOnError) |
| 107 | |
| 108 | var ( |
| 109 | quorum = fs.Int("quorum", 0, "number of signatures required to authorize block") |
| 110 | timeStr = fs.String("time", "", "block timestamp") |
| 111 | ) |
| 112 | |
| 113 | err := fs.Parse(args) |
| 114 | must(err) |
| 115 | |
| 116 | pubkeysHex := fs.Args() |
| 117 | var pubkeys []ed25519.PublicKey |
| 118 | for _, pubkeyHex := range pubkeysHex { |
| 119 | b, err := hex.DecodeString(pubkeyHex) |
| 120 | must(err) |
| 121 | if len(b) != ed25519.PublicKeySize { |
| 122 | panic(fmt.Errorf("bad pubkey length %d, want 32", len(b))) |
| 123 | } |
| 124 | pubkeys = append(pubkeys, ed25519.PublicKey(b)) |
| 125 | } |
| 126 | |
| 127 | if *quorum < 0 || *quorum > len(pubkeys) { |
| 128 | panic(fmt.Errorf("-quorum must be between 1 and %d", len(pubkeys))) |
| 129 | } |
| 130 | if *quorum == 0 { |
| 131 | // There may be zero pubkeys, in which case *quorum will remain |
| 132 | // zero. But if there are any pubkeys then quorum should be at |
| 133 | // least 1. |
| 134 | *quorum = len(pubkeys) |
| 135 | } |
| 136 | |
| 137 | var ts time.Time |
| 138 | if *timeStr == "" { |
| 139 | ts = time.Now() |
| 140 | } else { |
| 141 | ts, err = time.Parse(time.RFC3339, *timeStr) |
| 142 | must(err) |
| 143 | } |
| 144 | |
| 145 | block, err := protocol.NewInitialBlock(pubkeys, *quorum, ts) |
| 146 | must(err) |
| 147 | |
| 148 | blockBytes, err := block.Bytes() |
| 149 | must(err) |
| 150 | |
| 151 | os.Stdout.Write(blockBytes) |
| 152 | } |
| 153 | |
| 154 | func sign(args []string) { |
| 155 | fs := flag.NewFlagSet("sign", flag.PanicOnError) |