This repository has been archived by the owner on May 13, 2024. It is now read-only.
/
memory.go
65 lines (54 loc) · 1.61 KB
/
memory.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
// Copyright (c) 2020-2022 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package signer
import (
"context"
"errors"
"blockwatch.cc/tzgo/codec"
"blockwatch.cc/tzgo/tezos"
)
var ErrAddressMismatch = errors.New("signer: address mismatch")
type MemorySigner struct {
key tezos.PrivateKey
}
func NewFromKey(k tezos.PrivateKey) *MemorySigner {
return &MemorySigner{
key: k,
}
}
func (s MemorySigner) ListAddresses(_ context.Context) ([]tezos.Address, error) {
return []tezos.Address{s.key.Address()}, nil
}
func (s MemorySigner) GetKey(_ context.Context, addr tezos.Address) (tezos.Key, error) {
pk := s.key.Public()
if !pk.Address().Equal(addr) {
return tezos.InvalidKey, ErrAddressMismatch
}
return pk, nil
}
func (s MemorySigner) SignMessage(_ context.Context, addr tezos.Address, msg string) (tezos.Signature, error) {
if !s.key.Address().Equal(addr) {
return tezos.InvalidSignature, ErrAddressMismatch
}
op := codec.NewOp().
WithBranch(tezos.ZeroBlockHash).
WithContents(&codec.FailingNoop{
Arbitrary: msg,
})
digest := tezos.Digest(op.Bytes())
return s.key.Sign(digest[:])
}
func (s MemorySigner) SignOperation(_ context.Context, addr tezos.Address, op *codec.Op) (tezos.Signature, error) {
if !s.key.Address().Equal(addr) {
return tezos.InvalidSignature, ErrAddressMismatch
}
err := op.Sign(s.key)
return op.Signature, err
}
func (s MemorySigner) SignBlock(_ context.Context, addr tezos.Address, head *codec.BlockHeader) (tezos.Signature, error) {
if !s.key.Address().Equal(addr) {
return tezos.InvalidSignature, ErrAddressMismatch
}
err := head.Sign(s.key)
return head.Signature, err
}