delegation,invocation: proper String() function
This commit is contained in:
@@ -102,6 +102,9 @@ func (d DID) PubKey() (crypto.PubKey, error) {
|
|||||||
|
|
||||||
// String formats the decentralized identity document (DID) as a string.
|
// String formats the decentralized identity document (DID) as a string.
|
||||||
func (d DID) String() string {
|
func (d DID) String() string {
|
||||||
|
if d == Undef {
|
||||||
|
return "(undefined)"
|
||||||
|
}
|
||||||
key, _ := mbase.Encode(mbase.Base58BTC, []byte(d.bytes))
|
key, _ := mbase.Encode(mbase.Base58BTC, []byte(d.bytes))
|
||||||
return "did:key:" + key
|
return "did:key:" + key
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,8 +10,10 @@ package delegation
|
|||||||
// TODO: change the "delegation" link above when the specification is merged
|
// TODO: change the "delegation" link above when the specification is merged
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ucan-wg/go-ucan/did"
|
"github.com/ucan-wg/go-ucan/did"
|
||||||
@@ -170,6 +172,32 @@ func (t *Token) IsValidAt(ti time.Time) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Token) String() string {
|
||||||
|
var res strings.Builder
|
||||||
|
|
||||||
|
var kind string
|
||||||
|
switch {
|
||||||
|
case t.issuer == t.subject:
|
||||||
|
kind = " (root delegation)"
|
||||||
|
case t.subject == did.Undef:
|
||||||
|
kind = " (powerline delegation)"
|
||||||
|
default:
|
||||||
|
kind = " (normal delegation)"
|
||||||
|
}
|
||||||
|
|
||||||
|
res.WriteString(fmt.Sprintf("Issuer: %s\n", t.Issuer()))
|
||||||
|
res.WriteString(fmt.Sprintf("Audience: %s\n", t.Audience()))
|
||||||
|
res.WriteString(fmt.Sprintf("Subject: %s%s\n", t.Subject(), kind))
|
||||||
|
res.WriteString(fmt.Sprintf("Command: %s\n", t.Command()))
|
||||||
|
res.WriteString(fmt.Sprintf("Policy: %s\n", t.Policy()))
|
||||||
|
res.WriteString(fmt.Sprintf("Nonce: %s\n", base64.StdEncoding.EncodeToString(t.Nonce())))
|
||||||
|
res.WriteString(fmt.Sprintf("Meta: %s\n", t.Meta()))
|
||||||
|
res.WriteString(fmt.Sprintf("NotBefore: %v\n", t.NotBefore()))
|
||||||
|
res.WriteString(fmt.Sprintf("Expiration: %v", t.Expiration()))
|
||||||
|
|
||||||
|
return res.String()
|
||||||
|
}
|
||||||
|
|
||||||
func (t *Token) validate() error {
|
func (t *Token) validate() error {
|
||||||
var errs error
|
var errs error
|
||||||
|
|
||||||
|
|||||||
@@ -8,8 +8,10 @@
|
|||||||
package invocation
|
package invocation
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ipfs/go-cid"
|
"github.com/ipfs/go-cid"
|
||||||
@@ -214,6 +216,24 @@ func (t *Token) IsValidAt(ti time.Time) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Token) String() string {
|
||||||
|
var res strings.Builder
|
||||||
|
|
||||||
|
res.WriteString(fmt.Sprintf("Issuer: %s\n", t.Issuer()))
|
||||||
|
res.WriteString(fmt.Sprintf("Audience: %s\n", t.Audience()))
|
||||||
|
res.WriteString(fmt.Sprintf("Subject: %v\n", t.Subject()))
|
||||||
|
res.WriteString(fmt.Sprintf("Command: %s\n", t.Command()))
|
||||||
|
res.WriteString(fmt.Sprintf("Args: %s\n", t.Arguments()))
|
||||||
|
res.WriteString(fmt.Sprintf("Proof: %v\n", t.Proof()))
|
||||||
|
res.WriteString(fmt.Sprintf("Nonce: %s\n", base64.StdEncoding.EncodeToString(t.Nonce())))
|
||||||
|
res.WriteString(fmt.Sprintf("Meta: %s\n", t.Meta()))
|
||||||
|
res.WriteString(fmt.Sprintf("Expiration: %v\n", t.Expiration()))
|
||||||
|
res.WriteString(fmt.Sprintf("Invoked At: %v\n", t.InvokedAt()))
|
||||||
|
res.WriteString(fmt.Sprintf("Cause: %v", t.Cause()))
|
||||||
|
|
||||||
|
return res.String()
|
||||||
|
}
|
||||||
|
|
||||||
func (t *Token) validate() error {
|
func (t *Token) validate() error {
|
||||||
var errs error
|
var errs error
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user