forked from hashgraph/hedera-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.go
881 lines (703 loc) · 19.3 KB
/
crypto.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
package hedera
/*-
*
* Hedera Go SDK
*
* Copyright (C) 2020 - 2022 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import (
"crypto/hmac"
"crypto/sha512"
"encoding/binary"
"encoding/hex"
"io"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/crypto"
"github.com/hashgraph/hedera-protobufs-go/services"
"github.com/pkg/errors"
"golang.org/x/crypto/pbkdf2"
)
const ed25519PrivateKeyPrefix = "302e020100300506032b657004220420"
const ed25519PubKeyPrefix = "302a300506032b6570032100"
const _ECDSAPrivateKeyPrefix = "3030020100300706052b8104000a04220420"
const _ECDSAPubKeyPrefix = "302f300706052b8104000a0324000421"
type Key interface {
_ToProtoKey() *services.Key
String() string
}
func _KeyFromProtobuf(pbKey *services.Key) (Key, error) {
if pbKey == nil {
return PublicKey{}, errParameterNull
}
switch key := pbKey.GetKey().(type) {
case *services.Key_Ed25519:
return PublicKeyFromBytesEd25519(key.Ed25519)
case *services.Key_ThresholdKey:
threshold := int(key.ThresholdKey.GetThreshold())
keys, err := _KeyListFromProtobuf(key.ThresholdKey.GetKeys())
if err != nil {
return nil, err
}
keys.threshold = threshold
return &keys, nil
case *services.Key_KeyList:
keys, err := _KeyListFromProtobuf(key.KeyList)
if err != nil {
return nil, err
}
return &keys, nil
case *services.Key_ContractID:
return _ContractIDFromProtobuf(key.ContractID), nil
case *services.Key_ECDSASecp256K1:
return PublicKeyFromBytesECDSA(key.ECDSASecp256K1)
case *services.Key_DelegatableContractId:
return _DelegatableContractIDFromProtobuf(key.DelegatableContractId), nil
default:
return nil, _NewErrBadKeyf("key type not implemented: %v", key)
}
}
type PrivateKey struct {
ecdsaPrivateKey *_ECDSAPrivateKey
ed25519PrivateKey *_Ed25519PrivateKey
}
type PublicKey struct {
ecdsaPublicKey *_ECDSAPublicKey
ed25519PublicKey *_Ed25519PublicKey
}
// PrivateKeyGenerateEcdsa Generates a new ECDSASecp256K1 key
func PrivateKeyGenerateEcdsa() (PrivateKey, error) {
key, err := _GenerateECDSAPrivateKey()
if err != nil {
return PrivateKey{}, err
}
return PrivateKey{
ecdsaPrivateKey: key,
}, nil
}
// Deprecated: use `PrivateKeyGenerateEd25519()` instead
func PrivateKeyGenerate() (PrivateKey, error) {
return PrivateKeyGenerateEd25519()
}
// PrivateKeyGenerateEd25519 Generates a new Ed25519 key
func PrivateKeyGenerateEd25519() (PrivateKey, error) {
key, err := _GenerateEd25519PrivateKey()
if err != nil {
return PrivateKey{}, err
}
return PrivateKey{
ed25519PrivateKey: key,
}, nil
}
// Deprecated the use of raw bytes for a Ed25519 private key is deprecated; use PrivateKeyFromBytesEd25519() instead.
func PrivateKeyFromBytes(bytes []byte) (PrivateKey, error) {
key, err := _Ed25519PrivateKeyFromBytes(bytes)
if err != nil {
key2, err2 := _ECDSAPrivateKeyFromBytes(bytes)
if err2 != nil {
return PrivateKey{}, err2
}
return PrivateKey{
ecdsaPrivateKey: key2,
}, nil
}
return PrivateKey{
ed25519PrivateKey: key,
}, nil
}
func PrivateKeyFromBytesDer(bytes []byte) (PrivateKey, error) {
key, err := _Ed25519PrivateKeyFromBytes(bytes)
if err != nil {
key2, err2 := _ECDSAPrivateKeyFromBytes(bytes)
if err2 != nil {
return PrivateKey{}, err2
}
return PrivateKey{
ecdsaPrivateKey: key2,
}, nil
}
return PrivateKey{
ed25519PrivateKey: key,
}, nil
}
func PrivateKeyFromBytesEd25519(bytes []byte) (PrivateKey, error) {
key, err := _Ed25519PrivateKeyFromBytes(bytes)
if err != nil {
return PrivateKey{}, err
}
return PrivateKey{
ed25519PrivateKey: key,
}, nil
}
func PrivateKeyFromBytesECDSA(bytes []byte) (PrivateKey, error) {
key, err := _ECDSAPrivateKeyFromBytes(bytes)
if err != nil {
return PrivateKey{}, err
}
return PrivateKey{
ecdsaPrivateKey: key,
}, nil
}
func PublicKeyFromBytesEd25519(bytes []byte) (PublicKey, error) {
key, err := _Ed25519PublicKeyFromBytes(bytes)
if err != nil {
return PublicKey{}, err
}
return PublicKey{
ed25519PublicKey: key,
}, nil
}
func PublicKeyFromBytesECDSA(bytes []byte) (PublicKey, error) {
key, err := _ECDSAPublicKeyFromBytes(bytes)
if err != nil {
return PublicKey{}, err
}
return PublicKey{
ecdsaPublicKey: key,
}, nil
}
// Deprecated the use of raw bytes for a Ed25519 private key is deprecated; use PublicKeyFromBytesEd25519() instead.
func PublicKeyFromBytes(bytes []byte) (PublicKey, error) {
key, err := _Ed25519PublicKeyFromBytes(bytes)
if err != nil {
key2, err2 := _ECDSAPublicKeyFromBytes(bytes)
if err2 != nil {
return PublicKey{}, err2
}
return PublicKey{
ecdsaPublicKey: key2,
}, nil
}
return PublicKey{
ed25519PublicKey: key,
}, nil
}
func PublicKeyFromBytesDer(bytes []byte) (PublicKey, error) {
key, err := _Ed25519PublicKeyFromBytes(bytes)
if err != nil {
key2, err2 := _ECDSAPublicKeyFromBytes(bytes)
if err2 != nil {
return PublicKey{}, err2
}
return PublicKey{
ecdsaPublicKey: key2,
}, nil
}
return PublicKey{
ed25519PublicKey: key,
}, nil
}
// Deprecated
// PrivateKeyFromMnemonic recovers an _Ed25519PrivateKey from a valid 24 word length mnemonic phrase and a
// passphrase.
//
// An empty string can be passed for passPhrase If the mnemonic phrase wasn't generated with a passphrase. This is
// required to recover a private key from a mnemonic generated by the Android and iOS wallets.
func PrivateKeyFromMnemonic(mnemonic Mnemonic, passPhrase string) (PrivateKey, error) {
key, err := _Ed25519PrivateKeyFromMnemonic(mnemonic, passPhrase)
if err != nil {
return PrivateKey{}, err
}
return PrivateKey{
ed25519PrivateKey: key,
}, nil
}
// The use of raw bytes for a Ed25519 private key is deprecated; use PrivateKeyFromStringEd25519() instead.
func PrivateKeyFromString(s string) (PrivateKey, error) {
byt, err := hex.DecodeString(s)
if err != nil {
return PrivateKey{}, err
}
return PrivateKeyFromBytes(byt)
}
// PrivateKeyFromStringDer Creates PrivateKey from hex string with a der prefix
func PrivateKeyFromStringDer(s string) (PrivateKey, error) {
if strings.Contains(s, _ECDSAPrivateKeyPrefix) {
key, err := _ECDSAPrivateKeyFromString(s)
if err != nil {
return PrivateKey{}, err
}
return PrivateKey{
ecdsaPrivateKey: key,
}, nil
} else if strings.Contains(s, ed25519PrivateKeyPrefix) {
key, err := _Ed25519PrivateKeyFromString(s)
if err != nil {
return PrivateKey{}, err
}
return PrivateKey{
ed25519PrivateKey: key,
}, nil
}
return PrivateKey{}, nil
}
func PrivateKeyFromStringEd25519(s string) (PrivateKey, error) {
key, err := _Ed25519PrivateKeyFromString(s)
if err != nil {
return PrivateKey{}, err
}
return PrivateKey{
ed25519PrivateKey: key,
}, nil
}
// Deprecated: use PrivateKeyFromStringECDSA() instead
func PrivateKeyFromStringECSDA(s string) (PrivateKey, error) {
return PrivateKeyFromStringECDSA(s)
}
func PrivateKeyFromStringECDSA(s string) (PrivateKey, error) {
key, err := _ECDSAPrivateKeyFromString(s)
if err != nil {
return PrivateKey{}, err
}
return PrivateKey{
ecdsaPrivateKey: key,
}, nil
}
func PrivateKeyFromSeedEd25519(seed []byte) (PrivateKey, error) {
key, err := _Ed25519PrivateKeyFromSeed(seed)
if err != nil {
return PrivateKey{}, err
}
return PrivateKey{
ed25519PrivateKey: key,
}, nil
}
func PrivateKeyFromSeedECDSAsecp256k1(seed []byte) (PrivateKey, error) {
key, err := _ECDSAPrivateKeyFromSeed(seed)
if err != nil {
return PrivateKey{}, err
}
return PrivateKey{
ecdsaPrivateKey: key,
}, nil
}
func PrivateKeyFromKeystore(ks []byte, passphrase string) (PrivateKey, error) {
key, err := _Ed25519PrivateKeyFromKeystore(ks, passphrase)
if err != nil {
return PrivateKey{}, err
}
return PrivateKey{
ed25519PrivateKey: key,
}, nil
}
// PrivateKeyReadKeystore recovers an _Ed25519PrivateKey from an encrypted _Keystore file.
func PrivateKeyReadKeystore(source io.Reader, passphrase string) (PrivateKey, error) {
key, err := _Ed25519PrivateKeyReadKeystore(source, passphrase)
if err != nil {
return PrivateKey{}, err
}
return PrivateKey{
ed25519PrivateKey: key,
}, nil
}
func PrivateKeyFromPem(bytes []byte, passphrase string) (PrivateKey, error) {
key, err := _Ed25519PrivateKeyFromPem(bytes, passphrase)
if err != nil {
return PrivateKey{}, err
}
return PrivateKey{
ed25519PrivateKey: key,
}, nil
}
func PrivateKeyReadPem(source io.Reader, passphrase string) (PrivateKey, error) {
// note: Passphrases are currently not supported, but included in the function definition to avoid breaking
// changes in the future.
key, err := _Ed25519PrivateKeyReadPem(source, passphrase)
if err != nil {
return PrivateKey{}, err
}
return PrivateKey{
ed25519PrivateKey: key,
}, nil
}
// The use of raw bytes for a Ed25519 public key is deprecated; use PublicKeyFromStringEd25519/ECDSA() instead.
func PublicKeyFromString(s string) (PublicKey, error) {
byt, err := hex.DecodeString(s)
if err != nil {
return PublicKey{}, err
}
return PublicKeyFromBytes(byt)
}
func PublicKeyFromStringECDSA(s string) (PublicKey, error) {
key, err := _ECDSAPublicKeyFromString(s)
if err != nil {
return PublicKey{}, err
}
return PublicKey{
ecdsaPublicKey: key,
}, nil
}
func PublicKeyFromStringEd25519(s string) (PublicKey, error) {
key, err := _Ed25519PublicKeyFromString(s)
if err != nil {
return PublicKey{}, err
}
return PublicKey{
ed25519PublicKey: key,
}, nil
}
func _DeriveEd25519ChildKey(parentKey []byte, chainCode []byte, index uint32) ([]byte, []byte, error) {
if IsHardenedIndex(index) {
return nil, nil, errors.New("the index should not be pre-hardened")
}
h := hmac.New(sha512.New, chainCode)
input := make([]byte, 37)
// 0x00 + parentKey + _Index(BE)
input[0] = 0
copy(input[1:37], parentKey)
binary.BigEndian.PutUint32(input[33:37], index)
// harden the input
input[33] |= 128
if _, err := h.Write(input); err != nil {
return nil, nil, err
}
digest := h.Sum(nil)
return digest[0:32], digest[32:], nil
}
func _DeriveECDSAChildKey(parentKey []byte, chainCode []byte, index uint32) ([]byte, []byte, error) {
h := hmac.New(sha512.New, chainCode)
isHardened := IsHardenedIndex(index)
input := make([]byte, 37)
key, err := crypto.ToECDSA(parentKey)
if err != nil {
return nil, nil, err
}
if isHardened {
offset := 33 - len(parentKey)
copy(input[offset:], parentKey)
} else {
pubKey := crypto.CompressPubkey(&key.PublicKey)
copy(input, pubKey)
}
binary.BigEndian.PutUint32(input[33:37], index)
if _, err := h.Write(input); err != nil {
return nil, nil, err
}
i := h.Sum(nil)
il := new(big.Int)
il.SetBytes(i[0:32])
ir := i[32:]
ki := new(big.Int)
ki.Add(key.D, il)
ki.Mod(ki, key.Curve.Params().N)
return ki.Bytes(), ir, nil
}
func _DeriveLegacyChildKey(parentKey []byte, index int64) ([]byte, error) {
in := make([]uint8, 8)
switch switchIndex := index; {
case switchIndex == int64(0xffffffffff):
in = []uint8{0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff}
case switchIndex > 0xffffffff:
return nil, errors.New("derive index is out of range")
default:
if switchIndex < 0 {
for i := 0; i < 4; i++ {
in[i] = uint8(0xff)
}
}
for i := 4; i < len(in); i++ {
in[i] = uint8(switchIndex)
}
}
password := make([]uint8, len(parentKey))
copy(password, parentKey)
password = append(password, in...)
salt := []byte{0xFF}
return pbkdf2.Key(password, salt, 2048, 32, sha512.New), nil
}
func (sk PrivateKey) PublicKey() PublicKey {
if sk.ecdsaPrivateKey != nil {
return PublicKey{
ecdsaPublicKey: sk.ecdsaPrivateKey._PublicKey(),
}
}
if sk.ed25519PrivateKey != nil {
return PublicKey{
ed25519PublicKey: sk.ed25519PrivateKey._PublicKey(),
}
}
return PublicKey{}
}
func (sk PrivateKey) ToAccountID(shard uint64, realm uint64) *AccountID {
return sk.PublicKey().ToAccountID(shard, realm)
}
func (pk PublicKey) ToAccountID(shard uint64, realm uint64) *AccountID {
temp := pk
return &AccountID{
Shard: shard,
Realm: realm,
Account: 0,
AliasKey: &temp,
checksum: nil,
}
}
// String returns the text-encoded representation of the PrivateKey.
func (sk PrivateKey) String() string {
if sk.ecdsaPrivateKey != nil {
return sk.ecdsaPrivateKey._StringDer()
}
if sk.ed25519PrivateKey != nil {
return sk.ed25519PrivateKey._StringDer()
}
return ""
}
func (sk PrivateKey) StringRaw() string {
if sk.ecdsaPrivateKey != nil {
return sk.ecdsaPrivateKey._StringRaw()
}
if sk.ed25519PrivateKey != nil {
return sk.ed25519PrivateKey._StringRaw()
}
return ""
}
func (sk PrivateKey) StringDer() string {
if sk.ecdsaPrivateKey != nil {
return sk.ecdsaPrivateKey._StringDer()
}
if sk.ed25519PrivateKey != nil {
return sk.ed25519PrivateKey._StringDer()
}
return ""
}
func (pk PublicKey) String() string {
if pk.ecdsaPublicKey != nil {
return pk.ecdsaPublicKey._StringDer()
}
if pk.ed25519PublicKey != nil {
return pk.ed25519PublicKey._StringDer()
}
return ""
}
func (pk PublicKey) StringDer() string {
if pk.ecdsaPublicKey != nil {
return pk.ecdsaPublicKey._StringDer()
}
if pk.ed25519PublicKey != nil {
return pk.ed25519PublicKey._StringDer()
}
return ""
}
func (pk PublicKey) StringRaw() string {
if pk.ecdsaPublicKey != nil {
return pk.ecdsaPublicKey._StringRaw()
}
if pk.ed25519PublicKey != nil {
return pk.ed25519PublicKey._StringRaw()
}
return ""
}
// `Deprecated: Use ToEvmAddress instead`
func (pk PublicKey) ToEthereumAddress() string {
if pk.ecdsaPublicKey != nil {
return pk.ecdsaPublicKey._ToEthereumAddress()
}
panic("unsupported operation on Ed25519PublicKey")
}
func (pk PublicKey) ToEvmAddress() string {
if pk.ecdsaPublicKey != nil {
return pk.ecdsaPublicKey._ToEthereumAddress()
}
panic("unsupported operation on Ed25519PublicKey")
}
/*
* For `Ed25519` the result of this method call is identical to `toBytesRaw()` while for `ECDSA`
* this method is identical to `toBytesDer()`.
*
* We strongly recommend using `toBytesRaw()` or `toBytesDer()` instead.
*/
func (sk PrivateKey) Bytes() []byte {
if sk.ecdsaPrivateKey != nil {
return sk.ecdsaPrivateKey._BytesDer()
}
if sk.ed25519PrivateKey != nil {
return sk.ed25519PrivateKey._BytesRaw()
}
return []byte{}
}
func (sk PrivateKey) BytesDer() []byte {
if sk.ecdsaPrivateKey != nil {
return sk.ecdsaPrivateKey._BytesDer()
}
if sk.ed25519PrivateKey != nil {
return sk.ed25519PrivateKey._BytesDer()
}
return []byte{}
}
func (sk PrivateKey) BytesRaw() []byte {
if sk.ecdsaPrivateKey != nil {
return sk.ecdsaPrivateKey._BytesRaw()
}
if sk.ed25519PrivateKey != nil {
return sk.ed25519PrivateKey._BytesRaw()
}
return []byte{}
}
/*
* For `Ed25519` the result of this method call is identical to `toBytesRaw()` while for `ECDSA`
* this method is identical to `toBytesDer()`.
*
* We strongly recommend using `toBytesRaw()` or `toBytesDer()` instead.
*/
func (pk PublicKey) Bytes() []byte {
if pk.ecdsaPublicKey != nil {
return pk.ecdsaPublicKey._BytesDer()
}
if pk.ed25519PublicKey != nil {
return pk.ed25519PublicKey._BytesRaw()
}
return []byte{}
}
func (pk PublicKey) BytesRaw() []byte {
if pk.ecdsaPublicKey != nil {
return pk.ecdsaPublicKey._BytesRaw()
}
if pk.ed25519PublicKey != nil {
return pk.ed25519PublicKey._BytesRaw()
}
return []byte{}
}
func (pk PublicKey) BytesDer() []byte {
if pk.ecdsaPublicKey != nil {
return pk.ecdsaPublicKey._BytesDer()
}
if pk.ed25519PublicKey != nil {
return pk.ed25519PublicKey._BytesDer()
}
return []byte{}
}
func (sk PrivateKey) Keystore(passphrase string) ([]byte, error) {
if sk.ed25519PrivateKey != nil {
return sk.ed25519PrivateKey._Keystore(passphrase)
}
return []byte{}, errors.New("only ed25519 keystore is supported right now")
}
func (sk PrivateKey) WriteKeystore(destination io.Writer, passphrase string) error {
if sk.ed25519PrivateKey != nil {
return sk.ed25519PrivateKey._WriteKeystore(destination, passphrase)
}
return errors.New("only writing ed25519 keystore is supported right now")
}
// Sign signs the provided message with the Ed25519PrivateKey.
func (sk PrivateKey) Sign(message []byte) []byte {
if sk.ed25519PrivateKey != nil {
return sk.ed25519PrivateKey._Sign(message)
}
if sk.ecdsaPrivateKey != nil {
return sk.ecdsaPrivateKey._Sign(message)
}
return []byte{}
}
func (sk PrivateKey) SupportsDerivation() bool {
if sk.ed25519PrivateKey != nil {
return sk.ed25519PrivateKey._SupportsDerivation()
}
return false
}
func (sk PrivateKey) Derive(index uint32) (PrivateKey, error) {
if sk.ed25519PrivateKey != nil {
key, err := sk.ed25519PrivateKey._Derive(index)
if err != nil {
return PrivateKey{}, err
}
return PrivateKey{
ed25519PrivateKey: key,
}, nil
}
if sk.ecdsaPrivateKey != nil {
key, err := sk.ecdsaPrivateKey._Derive(index)
if err != nil {
return PrivateKey{}, err
}
return PrivateKey{
ecdsaPrivateKey: key,
}, nil
}
return PrivateKey{}, nil
}
func (sk PrivateKey) LegacyDerive(index int64) (PrivateKey, error) {
if sk.ed25519PrivateKey != nil {
key, err := sk.ed25519PrivateKey._LegacyDerive(index)
if err != nil {
return PrivateKey{}, err
}
return PrivateKey{
ed25519PrivateKey: key,
}, nil
}
return PrivateKey{}, errors.New("only ed25519 legacy derivation is supported")
}
func (sk PrivateKey) _ToProtoKey() *services.Key {
if sk.ecdsaPrivateKey != nil {
return sk.ecdsaPrivateKey._ToProtoKey()
}
if sk.ed25519PrivateKey != nil {
return sk.ed25519PrivateKey._ToProtoKey()
}
return &services.Key{}
}
func (pk PublicKey) _ToProtoKey() *services.Key {
if pk.ecdsaPublicKey != nil {
return pk.ecdsaPublicKey._ToProtoKey()
}
if pk.ed25519PublicKey != nil {
return pk.ed25519PublicKey._ToProtoKey()
}
return &services.Key{}
}
func (pk PublicKey) _ToSignaturePairProtobuf(signature []byte) *services.SignaturePair {
if pk.ecdsaPublicKey != nil {
return pk.ecdsaPublicKey._ToSignaturePairProtobuf(signature)
}
if pk.ed25519PublicKey != nil {
return pk.ed25519PublicKey._ToSignaturePairProtobuf(signature)
}
return &services.SignaturePair{}
}
func (sk PrivateKey) SignTransaction(transaction *Transaction) ([]byte, error) {
if sk.ecdsaPrivateKey != nil {
b, err := sk.ecdsaPrivateKey._SignTransaction(transaction)
if err != nil {
return []byte{}, err
}
return b, nil
}
if sk.ed25519PrivateKey != nil {
b, err := sk.ed25519PrivateKey._SignTransaction(transaction)
if err != nil {
return []byte{}, err
}
return b, nil
}
return []byte{}, errors.New("key type not supported, only ed25519 and ECDSASecp256K1 are supported right now")
}
func (pk PublicKey) Verify(message []byte, signature []byte) bool {
if pk.ecdsaPublicKey != nil {
return pk.ecdsaPublicKey._Verify(message, signature)
}
if pk.ed25519PublicKey != nil {
return pk.ed25519PublicKey._Verify(message, signature)
}
return false
}
func (pk PublicKey) VerifyTransaction(transaction Transaction) bool {
if pk.ecdsaPublicKey != nil {
return pk.ecdsaPublicKey._VerifyTransaction(transaction)
}
if pk.ed25519PublicKey != nil {
return pk.ed25519PublicKey._VerifyTransaction(transaction)
}
return false
}