irmameta.go 1.96 KB
Newer Older
Sietse Ringers's avatar
Sietse Ringers committed
1
// irmameta parses and prints info about the specified metadata attribute.
2
3
4
5
6
package main

import (
	"encoding/base64"
	"fmt"
7

8
9
10
11
12
	"os"
	"time"

	"encoding/json"

13
14
	"github.com/privacybydesign/gabi"
	"github.com/privacybydesign/gabi/big"
15
	"github.com/privacybydesign/irmago"
16
17
18
19
)

func main() {
	if len(os.Args) != 3 {
Sietse Ringers's avatar
Sietse Ringers committed
20
		fmt.Println("Usage: irmago path_to_irma_configuration metadata_attribute_in_decimal")
21
22
	}

23
24
	metaint := new(big.Int)
	_, ok := metaint.SetString(os.Args[2], 10)
25
	if !ok {
26
27
28
29
30
31
		bts, err := base64.StdEncoding.DecodeString(os.Args[2])
		if err != nil {
			fmt.Println("Could not parse argument as decimal or base64 integer: ", err.Error())
			os.Exit(1)
		}
		metaint.SetBytes(bts)
32
33
	}

34
	confpath := os.Args[1]
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
	conf, err := irma.NewConfiguration(confpath, "")
	if err != nil {
		fmt.Println("Failed to parse irma_configuration:", err)
		os.Exit(1)
	}
	err = conf.ParseFolder()
	if err != nil {
		fmt.Println("Failed to parse irma_configuration:", err)
		os.Exit(1)
	}

	meta := irma.MetadataFromInt(metaint, conf)
	typ := meta.CredentialType()
	var key *gabi.PublicKey

	if typ == nil {
		fmt.Println("Unknown credential type, hash:", base64.StdEncoding.EncodeToString(meta.CredentialTypeHash()))
	} else {
		fmt.Println("Identifier      :", typ.Identifier())
		key, err = meta.PublicKey()
		if err != nil {
			fmt.Println("Failed to parse public key", err)
		}
	}
	fmt.Println("Signed          :", meta.SigningDate().String())
	fmt.Println("Expires         :", meta.Expiry().String())
61
62
	fmt.Println("IsValid         :", meta.IsValid())
	fmt.Println("Version         :", meta.Version())
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
	fmt.Println("KeyCounter      :", meta.KeyCounter())
	if key != nil {
		fmt.Println("KeyExpires      :", time.Unix(key.ExpiryDate, 0))
		fmt.Println("KeyModulusBitlen:", key.N.BitLen())
	}

	fmt.Println()
	fmt.Println("CredentialType  :", prettyprint(typ))
}

func prettyprint(ob interface{}) string {
	b, err := json.MarshalIndent(ob, "", "  ")
	if err != nil {
		fmt.Println("error:", err)
	}
	return string(b)
}