util

package
v0.24.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2022 License: AGPL-3.0 Imports: 4 Imported by: 3

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromBase64

func FromBase64(bytesStr string) ([]byte, error)

FromBase64 convert input base64 encoded string to decoded bytes

func FromUint64

func FromUint64(number uint64) string

FromUint64 convert uint64 to string

func SelectFilter

func SelectFilter(object interface{}, selectKeys []string) (interface{}, error)

SelectFilter selects the specified keys from the given object. The keys are in the json dot notation and must refer to leaf elements e.g. payload.collection_guarantees.signer_ids

Example
package main

import (
	"encoding/json"
	"fmt"
	"time"

	"github.com/onflow/flow-go/engine/access/rest/models"
	"github.com/onflow/flow-go/engine/access/rest/util"
)

func main() {

	blocks := make([]models.Block, 2)
	for i := range blocks {
		block, err := generateBlock()
		if err != nil {
			fmt.Println(err)
			return
		}
		blocks[i] = block
	}

	selectKeys := []string{
		"header.id",
		"payload.collection_guarantees.signature",
		"payload.block_seals.aggregated_approval_signatures.signer_ids",
		"payload.collection_guarantees.signer_ids",
		"execution_result.events.event_index",
		"something.nonexisting",
	}

	filteredBlock, err := util.SelectFilter(blocks, selectKeys)
	if err != nil {
		fmt.Println(err)
		return
	}

	marshalled, err := json.MarshalIndent(filteredBlock, "", "\t")
	if err != nil {
		panic(err.Error())
	}
	fmt.Println(string(marshalled))
}

func generateBlock() (models.Block, error) {

	dummySignature := "abcdef0123456789"
	multipleDummySignatures := []string{dummySignature, dummySignature}
	dummyID := "abcd"
	dateString := "2021-11-20T11:45:26.371Z"
	t, err := time.Parse(time.RFC3339, dateString)
	if err != nil {
		return models.Block{}, err
	}

	return models.Block{
		Header: &models.BlockHeader{
			Id:                   dummyID,
			ParentId:             dummyID,
			Height:               "100",
			Timestamp:            t,
			ParentVoterSignature: dummySignature,
		},
		Payload: &models.BlockPayload{
			CollectionGuarantees: []models.CollectionGuarantee{
				{
					CollectionId: "abcdef0123456789",
					SignerIds:    multipleDummySignatures,
					Signature:    dummySignature,
				},
			},
			BlockSeals: []models.BlockSeal{
				{
					BlockId:    dummyID,
					ResultId:   dummyID,
					FinalState: "final",
					AggregatedApprovalSignatures: []models.AggregatedSignature{
						{
							VerifierSignatures: multipleDummySignatures,
							SignerIds:          multipleDummySignatures,
						},
					},
				},
			},
		},
		ExecutionResult: &models.ExecutionResult{
			Id:      dummyID,
			BlockId: dummyID,
			Events: []models.Event{
				{
					Type_:            "type",
					TransactionId:    dummyID,
					TransactionIndex: "1",
					EventIndex:       "2",
					Payload:          "payload",
				},
				{
					Type_:            "type",
					TransactionId:    dummyID,
					TransactionIndex: "1",
					EventIndex:       "3",
					Payload:          "payload",
				},
			},
			Links: &models.Links{
				Self: "link",
			},
		},
	}, nil
}
Output:

[
	{
		"execution_result": {
			"events": [
				{
					"event_index": "2"
				},
				{
					"event_index": "3"
				}
			]
		},
		"header": {
			"id": "abcd"
		},
		"payload": {
			"block_seals": [
				{
					"aggregated_approval_signatures": [
						{
							"signer_ids": [
								"abcdef0123456789",
								"abcdef0123456789"
							]
						}
					]
				}
			],
			"collection_guarantees": [
				{
					"signature": "abcdef0123456789",
					"signer_ids": [
						"abcdef0123456789",
						"abcdef0123456789"
					]
				}
			]
		}
	},
	{
		"execution_result": {
			"events": [
				{
					"event_index": "2"
				},
				{
					"event_index": "3"
				}
			]
		},
		"header": {
			"id": "abcd"
		},
		"payload": {
			"block_seals": [
				{
					"aggregated_approval_signatures": [
						{
							"signer_ids": [
								"abcdef0123456789",
								"abcdef0123456789"
							]
						}
					]
				}
			],
			"collection_guarantees": [
				{
					"signature": "abcdef0123456789",
					"signer_ids": [
						"abcdef0123456789",
						"abcdef0123456789"
					]
				}
			]
		}
	}
]

func ToBase64

func ToBase64(byteValue []byte) string

ToBase64 converts byte input to string base64 encoded output

func ToUint64

func ToUint64(uint64Str string) (uint64, error)

ToUint64 convert input string to uint64 number

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL