-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
51 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,58 @@ | ||
<a href="https://fivebinaries.com/"><img src="https://img.shields.io/badge/made%20by-Five%20Binaries-darkviolet.svg?style=flat-square" /></a> | ||
|
||
# go-cardano-serialization | ||
Golang library for serialization & deserialization of Cardano data structures. | ||
|
||
<p align="center">Golang library for serialization & deserialization of Cardano data structures.</a> | ||
<br> | ||
|
||
<p align="center"> | ||
<a href="#quickstart">Quickstart</a> • | ||
<a href="#examples">Examples</a> • | ||
<a href="#tests">Testing</a> | ||
</p> | ||
<br> | ||
|
||
## Quickstart | ||
Make sure you are using Go 1.15+, and Go modules support is enabled. | ||
Install or update `go-cardano-serialization` package commands with: | ||
``` | ||
go get github.com/fivebinaries/go-cardano-serialization | ||
``` | ||
|
||
Install or update `go-cardano-serialization` package commands with: | ||
|
||
```console | ||
$ go get github.com/fivebinaries/go-cardano-serialization | ||
``` | ||
|
||
## Examples | ||
|
||
An example of Cardano addresses serialization in Go. | ||
|
||
```go | ||
// load address from Base58 | ||
addr, err := FromBytes(base58.Decode("Ae2tdPwUPEZ4YjgvykNpoFeYUxoyhNj2kg8KfKWN2FizsSpLUPv68MpTVDo")) | ||
if err != nil { | ||
log.Panic(err) | ||
} | ||
// get ByronAddress | ||
byronAddr := addr.ToByronAddress() | ||
// get protocol magic for Byron address | ||
magic := byronAddr.ProtocolMagic() | ||
// get network ID for Byron address | ||
netId := byronAddr.NetworkId() | ||
// create new base address | ||
keyBytes := [crypto.Ed25519KeyHashLen]byte{} | ||
scriptBytes := [crypto.ScriptHashLen]byte{} | ||
for i := range keyBytes { | ||
keyBytes[i] = 23 | ||
} | ||
for i := range scriptBytes { | ||
scriptBytes[i] = 42 | ||
} | ||
addrBase := NewBaseAddress(5, StakeCredetialFromKeyHash(keyBytes[:]), StakeCredetialFromScriptHash(scriptBytes[:])) | ||
addrBytes := addrBase.ToBytes() | ||
``` | ||
|
||
Address API example: | ||
```go | ||
// load address from Base58 | ||
addr, err := FromBytes(base58.Decode("Ae2tdPwUPEZ4YjgvykNpoFeYUxoyhNj2kg8KfKWN2FizsSpLUPv68MpTVDo")) | ||
if err != nil { | ||
log.Panic(err) | ||
} | ||
// get ByronAddress | ||
byronAddr := addr.ToByronAddress() | ||
// get protocol magic for Byron address | ||
magic := byronAddr.ProtocolMagic() | ||
// get network ID for Byron address | ||
netId := byronAddr.NetworkId() | ||
// create new base address | ||
keyBytes := [crypto.Ed25519KeyHashLen]byte{} | ||
scriptBytes := [crypto.ScriptHashLen]byte{} | ||
for i := range keyBytes { | ||
keyBytes[i] = 23 | ||
} | ||
for i := range scriptBytes { | ||
scriptBytes[i] = 42 | ||
} | ||
addrBase := NewBaseAddress(5, StakeCredetialFromKeyHash(keyBytes[:]), StakeCredetialFromScriptHash(scriptBytes[:])) | ||
addrBytes := addrBase.ToBytes() | ||
``` | ||
## Tests | ||
You can run all tests with the command: | ||
```go | ||
go test ./... | ||
``` | ||
|
||
```console | ||
$ go test ./... | ||
``` |