github.com/ethersphere/bee/v2@v2.2.0/pkg/postage/testing/stamp.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package testing
     6  
     7  import (
     8  	crand "crypto/rand"
     9  	"encoding/binary"
    10  	"io"
    11  	"time"
    12  
    13  	"github.com/ethersphere/bee/v2/pkg/crypto"
    14  	"github.com/ethersphere/bee/v2/pkg/postage"
    15  	"github.com/ethersphere/bee/v2/pkg/swarm"
    16  )
    17  
    18  const signatureSize = 65
    19  
    20  // MustNewSignature will create a new random signature (65 byte slice). Panics on errors.
    21  func MustNewSignature() []byte {
    22  	sig := make([]byte, signatureSize)
    23  	_, err := io.ReadFull(crand.Reader, sig)
    24  	if err != nil {
    25  		panic(err)
    26  	}
    27  	return sig
    28  }
    29  
    30  // MustNewValidSignature will create a new valid signature. Panics on errors.
    31  func MustNewValidSignature(signer crypto.Signer, addr swarm.Address, id, index, timestamp []byte) []byte {
    32  	digest, err := postage.ToSignDigest(addr.Bytes(), id, index, timestamp)
    33  	if err != nil {
    34  		panic(err)
    35  	}
    36  
    37  	sig, err := signer.Sign(digest)
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  
    42  	return sig
    43  }
    44  
    45  // MustNewStamp will generate a invalid postage stamp with random data. Panics on errors.
    46  func MustNewStamp() *postage.Stamp {
    47  	return postage.NewStamp(MustNewID(), MustNewID()[:8], MustNewID()[:8], MustNewSignature())
    48  }
    49  
    50  // MustNewValidStamp will generate a valid postage stamp with random data. Panics on errors.
    51  func MustNewValidStamp(signer crypto.Signer, addr swarm.Address) *postage.Stamp {
    52  	id := MustNewID()
    53  	index := make([]byte, 8)
    54  	copy(index[:4], addr.Bytes()[:4])
    55  	timestamp := make([]byte, 8)
    56  	binary.BigEndian.PutUint64(timestamp, uint64(time.Now().UnixNano()))
    57  	sig := MustNewValidSignature(signer, addr, id, index, timestamp)
    58  	return postage.NewStamp(id, index, timestamp, sig)
    59  }
    60  
    61  // MustNewBatchStamp will generate a postage stamp with the provided batch ID and assign
    62  // random data to other fields. Panics on error
    63  func MustNewBatchStamp(batch []byte) *postage.Stamp {
    64  	return postage.NewStamp(batch, MustNewID()[:8], MustNewID()[:8], MustNewSignature())
    65  }
    66  
    67  // MustNewBatchStamp will generate a postage stamp with the provided batch ID and assign
    68  // random data to other fields. Panics on error
    69  func MustNewFields(batch []byte, index, ts uint64) *postage.Stamp {
    70  	indexBuf := make([]byte, 8)
    71  	binary.BigEndian.PutUint64(indexBuf, index)
    72  	tsBuf := make([]byte, 8)
    73  	binary.BigEndian.PutUint64(tsBuf, ts)
    74  	return postage.NewStamp(batch, indexBuf, tsBuf, MustNewSignature())
    75  }
    76  
    77  // MustNewStampWithTimestamp will generate a postage stamp with provided timestamp and
    78  // random data for other fields. Panics on errors.
    79  func MustNewStampWithTimestamp(ts uint64) *postage.Stamp {
    80  	tsBuf := make([]byte, 8)
    81  	binary.BigEndian.PutUint64(tsBuf, ts)
    82  	return postage.NewStamp(MustNewID(), MustNewID()[:8], tsBuf, MustNewSignature())
    83  }