Skip to content

Commit ca0538f

Browse files
committed
feat(doc): add some basic examples
1 parent 8c8da51 commit ca0538f

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,43 @@ Include the `go-varsig` library by running the following command:
1212
go get github.com/ucan-wg/go-varsig@latest
1313
```
1414

15+
## Quickstart
16+
17+
```go
18+
func ExampleDecode() {
19+
example, err := base64.RawStdEncoding.DecodeString("NAHtAe0BE3E")
20+
handleErr(err)
21+
22+
vs, err := varsig.Decode(example)
23+
handleErr(err)
24+
25+
fmt.Printf("%T\n", vs)
26+
fmt.Printf("Discriminator: %d\n", vs.Discriminator())
27+
fmt.Printf("Hash: %d\n", vs.Hash())
28+
fmt.Printf("PayloadEncoding: %d\n", vs.PayloadEncoding())
29+
30+
// Output:
31+
// varsig.EdDSAVarsig
32+
// Discriminator: 237
33+
// Hash: 19
34+
// PayloadEncoding: 3
35+
}
36+
37+
func ExampleEncode() {
38+
edDSAVarsig := varsig.NewEdDSAVarsig(
39+
varsig.CurveEd25519,
40+
varsig.HashSha2_512,
41+
varsig.PayloadEncodingDAGCBOR,
42+
)
43+
44+
b64 := base64.RawStdEncoding.EncodeToString(edDSAVarsig.Encode())
45+
fmt.Print(b64)
46+
47+
// Output:
48+
// NAHtAe0BE3E
49+
}
50+
```
51+
1552
## Documentation
1653

1754
Documentation for this library is provided as Go docs at

varsig_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package varsig_test
22

33
import (
4+
"encoding/base64"
45
"encoding/hex"
6+
"fmt"
57
"io"
68
"testing"
79

@@ -11,6 +13,39 @@ import (
1113
"github.com/ucan-wg/go-varsig"
1214
)
1315

16+
func ExampleDecode() {
17+
example, err := base64.RawStdEncoding.DecodeString("NAHtAe0BE3E")
18+
handleErr(err)
19+
20+
vs, err := varsig.Decode(example)
21+
handleErr(err)
22+
23+
fmt.Printf("%T\n", vs)
24+
fmt.Printf("Discriminator: %d\n", vs.Discriminator())
25+
fmt.Printf("Hash: %d\n", vs.Hash())
26+
fmt.Printf("PayloadEncoding: %d\n", vs.PayloadEncoding())
27+
28+
// Output:
29+
// varsig.EdDSAVarsig
30+
// Discriminator: 237
31+
// Hash: 19
32+
// PayloadEncoding: 3
33+
}
34+
35+
func ExampleEncode() {
36+
edDSAVarsig := varsig.NewEdDSAVarsig(
37+
varsig.CurveEd25519,
38+
varsig.HashSha2_512,
39+
varsig.PayloadEncodingDAGCBOR,
40+
)
41+
42+
b64 := base64.RawStdEncoding.EncodeToString(edDSAVarsig.Encode())
43+
fmt.Print(b64)
44+
45+
// Output:
46+
// NAHtAe0BE3E
47+
}
48+
1449
func TestDecode(t *testing.T) {
1550
t.Parallel()
1651

@@ -112,3 +147,9 @@ func TestDecode(t *testing.T) {
112147
assert.Nil(t, vs)
113148
})
114149
}
150+
151+
func handleErr(err error) {
152+
if err != nil {
153+
panic(err)
154+
}
155+
}

0 commit comments

Comments
 (0)