Skip to content

Commit

Permalink
xcore: more security for HDKeyRand and add EstimateNormalSize to pre-…
Browse files Browse the repository at this point in the history
…computed P2PKH transaction size
  • Loading branch information
BohuTANG committed Jul 15, 2019
1 parent b5bada9 commit ae0c000
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
6 changes: 2 additions & 4 deletions xcore/bip32/hdkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ package bip32

import (
"bytes"
"crypto/rand"
"fmt"
"math/rand"
"strconv"
"strings"
"time"

"crypto/hmac"
"crypto/sha512"
Expand Down Expand Up @@ -84,8 +83,7 @@ func NewHDKey(seed []byte) *HDKey {
// NewHDKeyRand -- returns the HDKey with random seed.
func NewHDKeyRand() (*HDKey, error) {
seed := make([]byte, 256)
random := rand.New(rand.NewSource(time.Now().UnixNano()))
if _, err := random.Read(seed); err != nil {
if _, err := rand.Read(seed); err != nil {
return nil, err
}
return NewHDKey(seed), nil
Expand Down
27 changes: 27 additions & 0 deletions xcore/estimate.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,33 @@ func EstimateSize(txins []*TxIn, txouts []*TxOut) int64 {
return int64((baseSize*(witnessScaleFactor-1) + (baseSize + witnessSize)) / witnessScaleFactor)
}

// EstimateNormalSize -- estimate the normal size by input and output count.
func EstimateNormalSize(ins int, outs int) int64 {
baseSize := 0

// Core size.
{
// Version.
baseSize += 4
// Input size.
{
baseSize += xbase.VarIntSerializeSize(uint64(ins))
baseSize += inputP2PKHSize * ins
}

// Output size.
{
baseSize += xbase.VarIntSerializeSize(uint64(outs))
// 8 value size.
// 25 is the p2pkh script length.
baseSize += (8 + 25) * outs
}
// Locktime.
baseSize += 4
}
return int64(baseSize)
}

// EstimateFees -- estimate the fee.
func EstimateFees(estimateSize int64, relayFeePerKb int64) int64 {
fees := 0.0
Expand Down
17 changes: 17 additions & 0 deletions xcore/estimate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// tokucore
//
// Copyright (c) 2019 TokuBlock
// BSD License

package xcore

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestEstimateNormalSize(t *testing.T) {
size := EstimateNormalSize(2, 2)
assert.Equal(t, int64(374), size)
}

0 comments on commit ae0c000

Please sign in to comment.