https://github.com/docknetwork/scale-codec-go
Go SCALE-Codec Library
https://github.com/docknetwork/scale-codec-go
scale-codec
Last synced: 5 months ago
JSON representation
Go SCALE-Codec Library
- Host: GitHub
- URL: https://github.com/docknetwork/scale-codec-go
- Owner: docknetwork
- License: apache-2.0
- Created: 2019-10-10T16:24:30.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-10-16T20:08:47.000Z (over 6 years ago)
- Last Synced: 2024-06-20T16:39:22.561Z (almost 2 years ago)
- Topics: scale-codec
- Language: Go
- Homepage:
- Size: 1.18 MB
- Stars: 4
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Golang SCALE Codec
=
This is an implementation of Scale-codec in go.
The following implementations were used as a reference:
* [Python](https://github.com/polkascan/py-scale-codec)
* [Rust](https://github.com/paritytech/parity-scale-codec)
To know more about the role of this library check this [link](https://medium.com/polkadot-network/polkascan-development-update-1-8451c4fcfc2e).
And more info about CODEC types is [here](https://polkadot.js.org/api/types/#codec-types).
Installation
-
Do
```shell script
go get github.com/docknetwork/scale-codec-go/codec
```
or from cloned repo
```shell script
cd codec && go install
```
Examples
-
Parsing primitive types:
```go
package main
import (
"fmt"
"scale/codec"
)
func main() {
offsetBytes, err := codec.NewBytes("0x02093d00")
value, err := offsetBytes.ToCompactUInt32()
fmt.Println(value, err)
// 1000000
}
```
Parsing bytes to existing structure:
```go
package main
import (
"fmt"
"scale/codec"
)
func main() {
offsetBytes, err := codec.NewBytes("0x0c00")
prefs, err := offsetBytes.ToValidatorPrefsLegacy()
fmt.Println(prefs.UnstakeThreshold, err)
// 3
}
```
Creating your own structure:
```go
package main
import (
"fmt"
"scale/codec"
)
type ValidatorPrefsLegacy struct {
UnstakeThreshold codec.U32
ValidatorPayment codec.Balance
}
func ToValidatorPrefsLegacy(sb *codec.OffsetBytes) (res ValidatorPrefsLegacy, err error) {
unstakeThreshold, err := sb.ToCompactUInt32()
if err != nil {
return
}
validatorPayment, err := sb.ToCompactBalance()
if err != nil {
return
}
res.UnstakeThreshold = unstakeThreshold
res.ValidatorPayment = validatorPayment
return
}
func main() {
offsetBytes, err := codec.NewBytes("0x0c00")
prefs, err := ToValidatorPrefsLegacy(&offsetBytes)
fmt.Println(prefs.UnstakeThreshold, err)
// 3
}
```