Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinaco committed Sep 20, 2023
0 parents commit 3966d54
Show file tree
Hide file tree
Showing 6,205 changed files with 1,484,258 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
solidity-protobuf/
*.bak/
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2023-present Calvin Lau (https://github.com/calvinaco)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
34 changes: 34 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
This project contains portions of code derived from the following libraries:

* yui-ibc-solidity
* Copyright: Copyright 2021 Datachain, Inc
* License: Apache License 2.0
* Repository: https://github.com/hyperledger-labs/yui-ibc-solidity

* Cosmos SDK
* Copyright: Copyright Cosmos-SDK Authors
* Copyright: Copyright 2016 All in Bits, Inc
* License: Apache License 2.0
* Repository: https://github.com/cosmos/cosmos-sdk

* Pulsar
* Copyright: Copyright 2021 Regen Network Development, Inc. & All in Bits, Inc.
* License: Apache License 2.0
* Repository: https://github.com/cosmos/cosmos-proto

* Protocol Buffers for Go with Gadgets
* Copyright: Copyright (c) 2022, The Cosmos SDK Authors. All rights reserved.
* Copyright: Copyright (c) 2013, The GoGo Authors. All rights reserved.
* Copyright: Copyright 2010 The Go Authors. All rights reserved.
* License: https://github.com/cosmos/gogoproto/blob/main/LICENSE
* Repository: https://github.com/cosmos/gogoproto

* ibc-go
* Copyright: Copyright (c) 2022 COSMOS
* License: MIT License
* Repository: https://github.com/cosmos/ibc-go

* Google APIs
* Copyright: Copyright 2023 Google LLC
* License: Apache License 2.0
* Repository: https://github.com/googleapis/googleapis
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Cosmos Protobuf En/Decoder for Solidity

Pre-generated Cosmos SDK and IBC Protobuf Encoder and Decoder for Solidity.

## Content

[./generated](./generated/) folder contains pre-generated solidity file based on Cosmos SDK v0.46.15 and ibc-go v6.2.0

## Sample Use

```solidity
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.9;
import "./cosmos/staking/v1beta1/tx.sol";
contract TestEncodeMsgDelegate {
event EncodedMsg(bytes data);
constructor() {
emit EncodedMsg(CosmosStakingV1beta1MsgDelegate.encode(
CosmosStakingV1beta1MsgDelegate.Data({
delegator_address: "cro1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
validator_address: "crocncl1yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy",
amount: CosmosBaseV1beta1Coin.Data({
denom: "basecro",
amount: "100"
})
})
));
}
}
```

## How to Generate

If you need other versions, you can tweak the [./scripts/update-proto-deps.sh](./scripts/update-proto-deps.sh) to download your desired version protobuf definitions.

```bash
./scripts/update-proto-deps.sh
./scripts/install-protobuf-solidity.sh
SOLPB_DIR=./solidity-protobuf ./scripts/generate-protobuf-solidity.sh
```

## License

[Apache 2.0](./LICENSE)
281 changes: 281 additions & 0 deletions generated/GoogleProtobufAny.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.9;
import "./ProtoBufRuntime.sol";

library GoogleProtobufAny {


//struct definition
struct Data {
string type_url;
bytes value;
}

// Decoder section

/**
* @dev The main decoder for memory
* @param bs The bytes array to be decoded
* @return The decoded struct
*/
function decode(bytes memory bs) internal pure returns (Data memory) {
(Data memory x, ) = _decode(32, bs, bs.length);
return x;
}

/**
* @dev The main decoder for storage
* @param self The in-storage struct
* @param bs The bytes array to be decoded
*/
function decode(Data storage self, bytes memory bs) internal {
(Data memory x, ) = _decode(32, bs, bs.length);
store(x, self);
}
// inner decoder

/**
* @dev The decoder for internal usage
* @param p The offset of bytes array to start decode
* @param bs The bytes array to be decoded
* @param sz The number of bytes expected
* @return The decoded struct
* @return The number of bytes decoded
*/
function _decode(uint256 p, bytes memory bs, uint256 sz)
internal
pure
returns (Data memory, uint)
{
Data memory r;
uint[3] memory counters;
uint256 fieldId;
ProtoBufRuntime.WireType wireType;
uint256 bytesRead;
uint256 offset = p;
uint256 pointer = p;
while (pointer < offset + sz) {
(fieldId, wireType, bytesRead) = ProtoBufRuntime._decode_key(pointer, bs);
pointer += bytesRead;
if (fieldId == 1) {
pointer += _read_type_url(pointer, bs, r, counters);
}
else if (fieldId == 2) {
pointer += _read_value(pointer, bs, r, counters);
}

else {
if (wireType == ProtoBufRuntime.WireType.Fixed64) {
uint256 size;
(, size) = ProtoBufRuntime._decode_fixed64(pointer, bs);
pointer += size;
}
if (wireType == ProtoBufRuntime.WireType.Fixed32) {
uint256 size;
(, size) = ProtoBufRuntime._decode_fixed32(pointer, bs);
pointer += size;
}
if (wireType == ProtoBufRuntime.WireType.Varint) {
uint256 size;
(, size) = ProtoBufRuntime._decode_varint(pointer, bs);
pointer += size;
}
if (wireType == ProtoBufRuntime.WireType.LengthDelim) {
uint256 size;
(, size) = ProtoBufRuntime._decode_lendelim(pointer, bs);
pointer += size;
}
}

}
return (r, sz);
}

// field readers

/**
* @dev The decoder for reading a field
* @param p The offset of bytes array to start decode
* @param bs The bytes array to be decoded
* @param r The in-memory struct
* @param counters The counters for repeated fields
* @return The number of bytes decoded
*/
function _read_type_url(
uint256 p,
bytes memory bs,
Data memory r,
uint[3] memory counters
) internal pure returns (uint) {
/**
* if `r` is NULL, then only counting the number of fields.
*/
(string memory x, uint256 sz) = ProtoBufRuntime._decode_string(p, bs);
if (isNil(r)) {
counters[1] += 1;
} else {
r.type_url = x;
if (counters[1] > 0) counters[1] -= 1;
}
return sz;
}

/**
* @dev The decoder for reading a field
* @param p The offset of bytes array to start decode
* @param bs The bytes array to be decoded
* @param r The in-memory struct
* @param counters The counters for repeated fields
* @return The number of bytes decoded
*/
function _read_value(
uint256 p,
bytes memory bs,
Data memory r,
uint[3] memory counters
) internal pure returns (uint) {
/**
* if `r` is NULL, then only counting the number of fields.
*/
(bytes memory x, uint256 sz) = ProtoBufRuntime._decode_bytes(p, bs);
if (isNil(r)) {
counters[2] += 1;
} else {
r.value = x;
if (counters[2] > 0) counters[2] -= 1;
}
return sz;
}


// Encoder section

/**
* @dev The main encoder for memory
* @param r The struct to be encoded
* @return The encoded byte array
*/
function encode(Data memory r) internal pure returns (bytes memory) {
bytes memory bs = new bytes(_estimate(r));
uint256 sz = _encode(r, 32, bs);
assembly {
mstore(bs, sz)
}
return bs;
}
// inner encoder

/**
* @dev The encoder for internal usage
* @param r The struct to be encoded
* @param p The offset of bytes array to start decode
* @param bs The bytes array to be decoded
* @return The number of bytes encoded
*/
function _encode(Data memory r, uint256 p, bytes memory bs)
internal
pure
returns (uint)
{
uint256 offset = p;
uint256 pointer = p;

pointer += ProtoBufRuntime._encode_key(
1,
ProtoBufRuntime.WireType.LengthDelim,
pointer,
bs
);
pointer += ProtoBufRuntime._encode_string(r.type_url, pointer, bs);
pointer += ProtoBufRuntime._encode_key(
2,
ProtoBufRuntime.WireType.LengthDelim,
pointer,
bs
);
pointer += ProtoBufRuntime._encode_bytes(r.value, pointer, bs);
return pointer - offset;
}
// nested encoder

/**
* @dev The encoder for inner struct
* @param r The struct to be encoded
* @param p The offset of bytes array to start decode
* @param bs The bytes array to be decoded
* @return The number of bytes encoded
*/
function _encode_nested(Data memory r, uint256 p, bytes memory bs)
internal
pure
returns (uint)
{
/**
* First encoded `r` into a temporary array, and encode the actual size used.
* Then copy the temporary array into `bs`.
*/
uint256 offset = p;
uint256 pointer = p;
bytes memory tmp = new bytes(_estimate(r));
uint256 tmpAddr = ProtoBufRuntime.getMemoryAddress(tmp);
uint256 bsAddr = ProtoBufRuntime.getMemoryAddress(bs);
uint256 size = _encode(r, 32, tmp);
pointer += ProtoBufRuntime._encode_varint(size, pointer, bs);
ProtoBufRuntime.copyBytes(tmpAddr + 32, bsAddr + pointer, size);
pointer += size;
delete tmp;
return pointer - offset;
}
// estimator

/**
* @dev The estimator for a struct
* @param r The struct to be encoded
* @return The number of bytes encoded in estimation
*/
function _estimate(
Data memory r
) internal pure returns (uint) {
uint256 e;
e += 1 + ProtoBufRuntime._sz_lendelim(bytes(r.type_url).length);
e += 1 + ProtoBufRuntime._sz_lendelim(r.value.length);
return e;
}

//store function
/**
* @dev Store in-memory struct to storage
* @param input The in-memory struct
* @param output The in-storage struct
*/
function store(Data memory input, Data storage output) internal {
output.type_url = input.type_url;
output.value = input.value;

}



//utility functions
/**
* @dev Return an empty struct
* @return r The empty struct
*/
function nil() internal pure returns (Data memory r) {
assembly {
r := 0
}
}

/**
* @dev Test whether a struct is empty
* @param x The struct to be tested
* @return r True if it is empty
*/
function isNil(Data memory x) internal pure returns (bool r) {
assembly {
r := iszero(x)
}
}
}
//library Any
Loading

0 comments on commit 3966d54

Please sign in to comment.