-
Notifications
You must be signed in to change notification settings - Fork 14
/
signer.go
66 lines (54 loc) · 1.69 KB
/
signer.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
package user
import (
"crypto/ecdsa"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
neofsecdsa "github.com/nspcc-dev/neofs-sdk-go/crypto/ecdsa"
)
// Signer represents a NeoFS user authorized by a digital signature.
type Signer interface {
// Signer signs data on behalf of the user.
neofscrypto.Signer
// UserID returns ID of the associated user.
UserID() ID
}
type signer struct {
neofscrypto.Signer
usr ID
}
func (s signer) UserID() ID {
return s.usr
}
// NewSigner combines provided [neofscrypto.Signer] and [ID] into [Signer].
//
// See also [NewAutoIDSigner].
func NewSigner(s neofscrypto.Signer, usr ID) Signer {
return signer{
Signer: s,
usr: usr,
}
}
func newAutoResolvedSigner(s neofscrypto.Signer, pubKey ecdsa.PublicKey) Signer {
var id ID
id.SetScriptHash((*keys.PublicKey)(&pubKey).GetScriptHash())
return NewSigner(s, id)
}
// NewAutoIDSigner returns [Signer] with neofscrypto.ECDSA_SHA512
// signature scheme and user [ID] automatically resolved from the ECDSA public
// key.
//
// See also [NewAutoIDSignerRFC6979].
func NewAutoIDSigner(key ecdsa.PrivateKey) Signer {
return newAutoResolvedSigner(neofsecdsa.Signer(key), key.PublicKey)
}
// NewAutoIDSignerRFC6979 is an analogue of [NewAutoIDSigner] but with
// [neofscrypto.ECDSA_DETERMINISTIC_SHA256] signature scheme.
func NewAutoIDSignerRFC6979(key ecdsa.PrivateKey) Signer {
return newAutoResolvedSigner(neofsecdsa.SignerRFC6979(key), key.PublicKey)
}
// ResolveFromECDSAPublicKey resolves [ID] from the given [ecdsa.PublicKey].
func ResolveFromECDSAPublicKey(pk ecdsa.PublicKey) ID {
var id ID
id.SetScriptHash((*keys.PublicKey)(&pk).GetScriptHash())
return id
}