-
Notifications
You must be signed in to change notification settings - Fork 13
/
signature.go
69 lines (61 loc) · 1.69 KB
/
signature.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
62
63
64
65
66
67
68
69
// SPDX-License-Identifier: ISC
// Copyright (c) 2014-2020 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package account
import (
"encoding/hex"
"fmt"
)
// Signature - the type for a signature
type Signature []byte
// String - convert a binary signature to hex string for use by the fmt package (for %s)
func (signature Signature) String() string {
return hex.EncodeToString(signature)
}
// GoString - convert a binary signature to hex string for use by the fmt package (for %#v)
func (signature Signature) GoString() string {
return "<signature:" + hex.EncodeToString(signature) + ">"
}
// Scan - convert a text representation to a signature for use by the format package scan routines
func (signature *Signature) Scan(state fmt.ScanState, verb rune) error {
token, err := state.Token(true, func(c rune) bool {
if c >= '0' && c <= '9' {
return true
}
if c >= 'A' && c <= 'F' {
return true
}
if c >= 'a' && c <= 'f' {
return true
}
return false
})
if err != nil {
return err
}
sig := make([]byte, hex.DecodedLen(len(token)))
byteCount, err := hex.Decode(sig, token)
if err != nil {
return err
}
*signature = sig[:byteCount]
return nil
}
// MarshalText - convert signature to text
func (signature Signature) MarshalText() ([]byte, error) {
size := hex.EncodedLen(len(signature))
b := make([]byte, size)
hex.Encode(b, signature)
return b, nil
}
// UnmarshalText - convert text into a signature
func (signature *Signature) UnmarshalText(s []byte) error {
sig := make([]byte, hex.DecodedLen(len(s)))
byteCount, err := hex.Decode(sig, s)
if err != nil {
return err
}
*signature = sig[:byteCount]
return nil
}