-
Notifications
You must be signed in to change notification settings - Fork 13
/
payid.go
60 lines (52 loc) · 1.42 KB
/
payid.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
// Copyright (c) 2014-2017 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package pay
import (
"encoding/hex"
"github.com/bitmark-inc/bitmarkd/fault"
"golang.org/x/crypto/sha3"
)
// type to represent a payment identifier
// Note: no reversal is required for this
type PayId [48]byte
// create a payment identifier from a set of transactions
func NewPayId(packed [][]byte) PayId {
digest := sha3.New384()
for _, data := range packed {
digest.Write(data)
}
hash := digest.Sum([]byte{})
var payId PayId
copy(payId[:], hash)
return payId
}
// convert a binary pay id to hex string for use by the fmt package (for %s)
func (payid PayId) String() string {
return hex.EncodeToString(payid[:])
}
// convert a binary pay id to hex string for use by the fmt package (for %#v)
func (payid PayId) GoString() string {
return "<payid:" + hex.EncodeToString(payid[:]) + ">"
}
// convert pay id to hex text
func (payid PayId) MarshalText() ([]byte, error) {
size := hex.EncodedLen(len(payid))
buffer := make([]byte, size)
hex.Encode(buffer, payid[:])
return buffer, nil
}
// convert hex text into a pay id
func (payid *PayId) UnmarshalText(s []byte) error {
if len(*payid) != hex.DecodedLen(len(s)) {
return fault.ErrNotAPayId
}
byteCount, err := hex.Decode(payid[:], s)
if nil != err {
return err
}
if len(payid) != byteCount {
return fault.ErrNotAPayId
}
return nil
}