Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

encoding.gno

3.08 Kb · 124 lines
  1package core
  2
  3import (
  4	"encoding/hex"
  5
  6	"gno.land/p/onbloc/ibc/union/types"
  7)
  8
  9func keccak(bz []byte) types.H256 {
 10	return types.Keccak(bz)
 11}
 12
 13// ABI-encoding helpers wrap the pure union/types encoders, which return an
 14// error (encoding can fail). core treats an encoding failure as a fatal
 15// invariant violation and panics, matching the Union spec's revert-on-failure
 16// semantics.
 17
 18func commitPacketHash(packet types.Packet) types.H256 {
 19	h, err := types.CommitPacket(packet)
 20	if err != nil {
 21		panic(err)
 22	}
 23
 24	return h
 25}
 26
 27func commitPacketsHash(packets []types.Packet) types.H256 {
 28	h, err := types.CommitPackets(packets)
 29	if err != nil {
 30		panic(err)
 31	}
 32
 33	return h
 34}
 35
 36func packetCommitmentPath(packet types.Packet) types.H256 {
 37	h, err := types.PacketCommitmentPath(packet)
 38	if err != nil {
 39		panic(err)
 40	}
 41
 42	return h
 43}
 44
 45func packetAcknowledgementPath(packet types.Packet) types.H256 {
 46	h, err := types.PacketAcknowledgementPath(packet)
 47	if err != nil {
 48		panic(err)
 49	}
 50
 51	return h
 52}
 53
 54func channelValue(channel types.Channel) types.H256 {
 55	bz, err := channel.EncodeABI()
 56	if err != nil {
 57		panic(err)
 58	}
 59
 60	return keccak(bz)
 61}
 62
 63func connectionValue(connection types.Connection) types.H256 {
 64	bz, err := connection.EncodeABI()
 65	if err != nil {
 66		panic(err)
 67	}
 68
 69	return keccak(bz)
 70}
 71
 72// h256Bytes returns a fresh byte slice containing h.
 73//
 74// Use it when passing package-level types.H256 values such as types.COMMITMENT_MAGIC to
 75// foreign-realm light clients. ics23 may copy and append against the input, so
 76// callers should pass a freshly allocated slice.
 77func h256Bytes(h types.H256) []byte {
 78	out := make([]byte, len(h))
 79	copy(out, h[:])
 80
 81	return out
 82}
 83
 84// cloneChannel returns a core-realm-owned copy of c with its byte field laundered,
 85// so persisting the copy does not taint the caller's channel (and vice versa).
 86// Without this, storing the channel makes its shared CounterpartyPortId slice
 87// externally stored, and the caller's later use (e.g. event emission) panics with
 88// "illegal conversion of readonly or externally stored value". Bytes.Clone copies
 89// the bytes element-wise (the only laundering gno's borrow rules permit on a
 90// foreign slice).
 91func cloneChannel(c types.Channel) types.Channel {
 92	return types.Channel{
 93		State:                 c.State,
 94		ConnectionId:          c.ConnectionId,
 95		CounterpartyChannelId: c.CounterpartyChannelId,
 96		CounterpartyPortId:    c.CounterpartyPortId.Clone(),
 97		Version:               c.Version,
 98	}
 99}
100
101func hexString(b []byte) string {
102	return "0x" + hex.EncodeToString(b)
103}
104
105// hexAttr encodes b as a lowercase 0x-prefixed hexadecimal string for use in
106// chain.Emit attributes.
107//
108// Use hexAttr for binary attribute values. Converting arbitrary []byte values
109// directly to string is lossy for non-UTF-8 input.
110func hexAttr(b []byte) string {
111	return hexString(b)
112}
113
114// hexAttrLen returns the length of b after hexAttr encoding.
115//
116// It avoids building the encoded string on validation paths that only need to
117// compare against an event attribute size budget.
118func hexAttrLen(b []byte) int {
119	return len(b)*2 + 2
120}
121
122func h256String(h types.H256) string {
123	return hexString(h[:])
124}