-
Notifications
You must be signed in to change notification settings - Fork 13
/
template.go
61 lines (48 loc) · 1.2 KB
/
template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
61
package main
import (
"bytes"
_ "embed"
"encoding/base64"
"text/template"
"github.com/svarogg/kaspaper/model"
)
//go:embed template.html
var templateString string
type walletTemplate struct {
Mnemonic *model.MnemonicString
Address string
AddressQR string
}
func renderWallet(wallet model.KaspaperWallet) (string, error) {
walletTemplate, err := walletToWalletTempalte(wallet)
if err != nil {
return "", err
}
funcMap := template.FuncMap{
"sub": func(str string, i, j int) string { return str[i:j] },
}
tmpl := template.Must(template.New("kaspaper").Funcs(funcMap).Parse(templateString))
buf := &bytes.Buffer{}
err = tmpl.Execute(buf, walletTemplate)
if err != nil {
return "", err
}
return buf.String(), nil
}
func walletToWalletTempalte(wallet model.KaspaperWallet) (*walletTemplate, error) {
const addressIndex = 0
address, err := wallet.Address(addressIndex)
if err != nil {
return nil, err
}
addressQRbytes, err := wallet.AddressQR(addressIndex)
if err != nil {
return nil, err
}
addressQRBase64 := base64.StdEncoding.EncodeToString(addressQRbytes)
return &walletTemplate{
Mnemonic: wallet.Mnemonic(),
Address: address,
AddressQR: addressQRBase64,
}, nil
}