-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcommon.go
More file actions
82 lines (72 loc) · 2.38 KB
/
common.go
File metadata and controls
82 lines (72 loc) · 2.38 KB
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
package address
import (
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/hashing/sha256"
"math"
)
var baseSeed = []byte(core.OneAddressPrefix + "PublicKey")
var sha256Hasher = sha256.NewSha256()
func enhanceBaseSeed(
sourceAddress []byte,
sourceIdentifier core.AddressIdentifier,
targetIdentifier core.AddressIdentifier,
) []byte {
seed := append(baseSeed, sourceAddress...)
seed = append(seed, sourceIdentifier.Spread()...)
return append(seed, targetIdentifier.Spread()...)
}
func generateOffCurvePublicKey(seed []byte, generator func([]byte) ([]byte, error)) ([]byte, error) {
for bump := math.MaxUint8; bump >= 0; bump-- {
bumpedSeed := append(seed, byte(bump))
publicKey, err := generator(bumpedSeed)
if err != nil {
continue
}
return publicKey, nil
}
return nil, ErrFailedToGenerateOffCurvePublicKey
}
func isSourceInvalid(sourceAddress []byte, sourceIdentifier core.AddressIdentifier) bool {
switch sourceIdentifier {
case core.MVXAddressIdentifier:
return isPseudoMultiversXAddress(sourceAddress)
case core.ETHAddressIdentifier:
return isPseudoEthereumAddress(sourceAddress)
default:
return true
}
}
func generatePseudoAddressForSeed(seed []byte, targetIdentifier core.AddressIdentifier) ([]byte, error) {
switch targetIdentifier {
case core.MVXAddressIdentifier:
return generatePseudoMultiversXAddress(seed)
case core.ETHAddressIdentifier:
return generatePseudoEthereumAddress(seed)
default:
return nil, ErrAddressIdentifierNotHandled
}
}
func GeneratePseudoAddress(
sourceAddress []byte,
sourceIdentifier core.AddressIdentifier,
targetIdentifier core.AddressIdentifier,
) ([]byte, error) {
if sourceIdentifier == targetIdentifier {
return nil, ErrSourceIdentifierMatchesTargetIdentifier
}
if isSourceInvalid(sourceAddress, sourceIdentifier) {
return nil, ErrSourceAddressIsGenerated
}
seed := enhanceBaseSeed(sourceAddress, sourceIdentifier, targetIdentifier)
return generatePseudoAddressForSeed(seed, targetIdentifier)
}
func ConvertAddressToPseudoAddress(sourceAddress []byte, sourceIdentifier core.AddressIdentifier) ([]byte, error) {
switch sourceIdentifier {
case core.MVXAddressIdentifier:
return convertMultiversXAddressToPseudoAddress(sourceAddress)
case core.ETHAddressIdentifier:
return convertEthereumAddressToPseudoAddress(sourceAddress)
default:
return nil, ErrAddressIdentifierNotHandled
}
}