mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00

* [DEV-134] Implement Continuous Integration Squashed commit: [5e41d830] Dev 223 fix txindex (#100) * [DEV-201] In handleGetBlockDAGInfo calculate difficulty by the tip with the lowest bits * [DEV-202] Move VirtualBlock.GetUTXOEntry to BlockDAG * [DEV-203] Move VirtualBlock.SelectedTip() to BlockDAG * [DEV-203] Move VirtualBlock.SelectedTip() to BlockDAG * [DEV-204] Unexport VirtualBlock() and add CalcMedianTime method for DAG * [DEV-204] add explanation about difficulty in CurrentBits() comment * [DEV-204] unexport VirtualBlock type * [DEV-223] make applyUTXOChanges return pastUTXOResults * [DEV-223] add bluestxdata for current block as well * [DEV-223] re-design tx index * [DEV-223] edit txindex comments * [DEV-223] rename BluesTxData -> AcceptedTxData, and return from applyUTXOChanges only transactions that got accepted * [DEV-223] add unit test for txindex * [DEV-223] fix comments and unite blueTransaction and AcceptedTxData to one type * [DEV-223] use bucket cursor for dbFetchFirstTxRegion * [DEV-223] use the same cursor instance for dbFetchFirstTxRegion * [DEV-223] write in dbFetchFirstTxRegion's comment that it returns the first block region * [DEV-223] rename type BlueBlockTransaction to TxWithBlockHash * [DEV-223] add named returned value for applyUTXOChanges [4c95e293] [DEV-134] Made golint ignore the vendor directory. [21736dbc] [DEV-134] Renamed ExampleBlockChain_ProcessBlock to ExampleBlockDAG_ProcessBlock to satisfy go vet. [beea6486] [DEV-134] Removed pushing the built docker to a remove repository. That's unnecessary at this stage. [bee911ed] [DEV-134] Made all precompilation checks run on everything instead of only the root dir. [585f92ae] [DEV-134] Added "github.com/pkg/errors" to dep. [5f02f570] [DEV-134] -vendor-only is written with only one hyphen. [3eee7f95] [DEV-134] go vet instead of go tool vet. [0c2d4343] [DEV-134] Split all the pre-compile checks to separate lines to be able to tell which of them is failing. [780519c8] [DEV-134] Ran gofmt on everything. [8247146b] Dev 223 fix txindex (#100) * [DEV-201] In handleGetBlockDAGInfo calculate difficulty by the tip with the lowest bits * [DEV-202] Move VirtualBlock.GetUTXOEntry to BlockDAG * [DEV-203] Move VirtualBlock.SelectedTip() to BlockDAG * [DEV-203] Move VirtualBlock.SelectedTip() to BlockDAG * [DEV-204] Unexport VirtualBlock() and add CalcMedianTime method for DAG * [DEV-204] add explanation about difficulty in CurrentBits() comment * [DEV-204] unexport VirtualBlock type * [DEV-223] make applyUTXOChanges return pastUTXOResults * [DEV-223] add bluestxdata for current block as well * [DEV-223] re-design tx index * [DEV-223] edit txindex comments * [DEV-223] rename BluesTxData -> AcceptedTxData, and return from applyUTXOChanges only transactions that got accepted * [DEV-223] add unit test for txindex * [DEV-223] fix comments and unite blueTransaction and AcceptedTxData to one type * [DEV-223] use bucket cursor for dbFetchFirstTxRegion * [DEV-223] use the same cursor instance for dbFetchFirstTxRegion * [DEV-223] write in dbFetchFirstTxRegion's comment that it returns the first block region * [DEV-223] rename type BlueBlockTransaction to TxWithBlockHash * [DEV-223] add named returned value for applyUTXOChanges [bff68aa3] [DEV-134] Gave executable permission to deploy.sh [638a99d9] [DEV-134] Added jenkinsfile and deploy script. * [DEV-134] Added a robust testing script. * [DEV-134] Fixed a bash-ism. * [DEV-134] Disabled testing with coverage for now. * [DEV-134] Disabled golint and removed removing debug symbols. * [DEV-134] Disabled aligncheck. * [DEV-134] Disabled structcheck and varcheck. * [DEV-134] Added "don't inline functions" to compiler flags for testing. * [DEV-134] Made build fail if gofmt prints out anything. * [DEV-134] Fixed misleading comment. * [DEV-134] Added comments to test.sh. * [DEV-134] Renamed tm to measure_runtime and removed do_ prefixes from functions. * [DEV-134] Fixed gofmt line in build script. * [DEV-134] Fixed gofmt some more. * [DEV-134] Fixed gofmt not actually failing due to logical or.
145 lines
3.5 KiB
Go
145 lines
3.5 KiB
Go
// Copyright (c) 2013-2015 The btcsuite developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package util
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/ecdsa"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
_ "crypto/sha512" // Needed for RegisterHash in init
|
|
"crypto/x509"
|
|
"crypto/x509/pkix"
|
|
"encoding/pem"
|
|
"errors"
|
|
"fmt"
|
|
"math/big"
|
|
"net"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
// NewTLSCertPair returns a new PEM-encoded x.509 certificate pair
|
|
// based on a 521-bit ECDSA private key. The machine's local interface
|
|
// addresses and all variants of IPv4 and IPv6 localhost are included as
|
|
// valid IP addresses.
|
|
func NewTLSCertPair(organization string, validUntil time.Time, extraHosts []string) (cert, key []byte, err error) {
|
|
now := time.Now()
|
|
if validUntil.Before(now) {
|
|
return nil, nil, errors.New("validUntil would create an already-expired certificate")
|
|
}
|
|
|
|
priv, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
// end of ASN.1 time
|
|
endOfTime := time.Date(2049, 12, 31, 23, 59, 59, 0, time.UTC)
|
|
if validUntil.After(endOfTime) {
|
|
validUntil = endOfTime
|
|
}
|
|
|
|
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
|
|
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("failed to generate serial number: %s", err)
|
|
}
|
|
|
|
host, err := os.Hostname()
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
ipAddresses := []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("::1")}
|
|
dnsNames := []string{host}
|
|
if host != "localhost" {
|
|
dnsNames = append(dnsNames, "localhost")
|
|
}
|
|
|
|
addIP := func(ipAddr net.IP) {
|
|
for _, ip := range ipAddresses {
|
|
if bytes.Equal(ip, ipAddr) {
|
|
return
|
|
}
|
|
}
|
|
ipAddresses = append(ipAddresses, ipAddr)
|
|
}
|
|
addHost := func(host string) {
|
|
for _, dnsName := range dnsNames {
|
|
if host == dnsName {
|
|
return
|
|
}
|
|
}
|
|
dnsNames = append(dnsNames, host)
|
|
}
|
|
|
|
addrs, err := interfaceAddrs()
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
for _, a := range addrs {
|
|
ipAddr, _, err := net.ParseCIDR(a.String())
|
|
if err == nil {
|
|
addIP(ipAddr)
|
|
}
|
|
}
|
|
|
|
for _, hostStr := range extraHosts {
|
|
host, _, err := net.SplitHostPort(hostStr)
|
|
if err != nil {
|
|
host = hostStr
|
|
}
|
|
if ip := net.ParseIP(host); ip != nil {
|
|
addIP(ip)
|
|
} else {
|
|
addHost(host)
|
|
}
|
|
}
|
|
|
|
template := x509.Certificate{
|
|
SerialNumber: serialNumber,
|
|
Subject: pkix.Name{
|
|
Organization: []string{organization},
|
|
CommonName: host,
|
|
},
|
|
NotBefore: now.Add(-time.Hour * 24),
|
|
NotAfter: validUntil,
|
|
|
|
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature |
|
|
x509.KeyUsageCertSign,
|
|
IsCA: true, // so can sign self.
|
|
BasicConstraintsValid: true,
|
|
|
|
DNSNames: dnsNames,
|
|
IPAddresses: ipAddresses,
|
|
}
|
|
|
|
derBytes, err := x509.CreateCertificate(rand.Reader, &template,
|
|
&template, &priv.PublicKey, priv)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("failed to create certificate: %v", err)
|
|
}
|
|
|
|
certBuf := &bytes.Buffer{}
|
|
err = pem.Encode(certBuf, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("failed to encode certificate: %v", err)
|
|
}
|
|
|
|
keybytes, err := x509.MarshalECPrivateKey(priv)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("failed to marshal private key: %v", err)
|
|
}
|
|
|
|
keyBuf := &bytes.Buffer{}
|
|
err = pem.Encode(keyBuf, &pem.Block{Type: "EC PRIVATE KEY", Bytes: keybytes})
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("failed to encode private key: %v", err)
|
|
}
|
|
|
|
return certBuf.Bytes(), keyBuf.Bytes(), nil
|
|
}
|