-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtc-identity.go
57 lines (41 loc) · 1.14 KB
/
btc-identity.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
package bitcoin-identity
import "fmt"
import "encoding/hex"
import "crypto/sha256"
import "github.com/btcsuite/btcd/btcec"
import "golang.org/x/crypto/ripemd160"
import "github.com/btcsuite/btcutil/base58"
type Secret struct {
PrivKey, PubKey []byte
SIN string
}
func GenerateKey() Secret {
keys, _ := btcec.NewPrivateKey(btcec.S256())
pub := keys.PubKey().SerializeCompressed()
priv := keys.Serialize()
sin := GenerateSin(pub)
return Secret{pub, priv, sin}
}
func GenerateSin(pubkey []byte) string {
// FIRST STEP - COMPLETE
sha_256 := sha256.New()
sha_256.Write(pubkey)
// SECOND STEP - COMPLETE
rip := ripemd160.New()
rip.Write(sha_256.Sum(nil))
// THIRD STEP - COMPLETE
sin_version, _ := hex.DecodeString("0f02")
pubPrefixed := append(sin_version, rip.Sum(nil)...)
// FOURTH STEP - COMPLETE
sha2nd := sha256.New()
sha2nd.Write([]byte(pubPrefixed))
sha3rd := sha256.New()
sha3rd.Write([]byte(sha2nd.Sum(nil)))
// // FIFTH STEP - COMPLETE
checksum := sha3rd.Sum(nil)[0:4]
// SIXTH STEP - COMPLETE
pubWithChecksum := append(pubPrefixed, checksum...)
// SIN
sin := base58.Encode(pubWithChecksum)
return sin
}