forked from ebellocchia/bip_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bip44Coins.SOLANA.py
executable file
·75 lines (50 loc) · 2.25 KB
/
Bip44Coins.SOLANA.py
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
#!/usr/bin/python
import sys
import binascii
import base64
from bip_utils import (
Bip39WordsNum, Bip39MnemonicGenerator, Bip39SeedGenerator, Bip44Changes, Bip44Coins, Bip44
)
num_args = len(sys.argv)
args = str(sys.argv)
# print('Number of arguments:', num_args, 'arguments.' )
# print('Argument List:', args )
if num_args < 4 :
#print('Parameters are user_id, index')
exit()
account_id = int(sys.argv[1])
index = int(sys.argv[2])
base64_message = sys.argv[3]
# print('Account ID: ', account_id)
# print('Account Index: ', index)
# Generate random mnemonic
#mnemonic = Bip39MnemonicGenerator().FromWordsNumber(Bip39WordsNum.WORDS_NUM_24)
#print("Mnemonic string: %s" % mnemonic)
#mnemonic = VAULT.get_solana_seed( KEY )
#message = "Python is fun"
# message = "gallery hospital reflect tray strike pyramid scrap two proud cute trend sunny bulk almost surface trap license drastic fiber tumble rare purity dentist dice"
# message_bytes = message.encode('ascii')
# base64_bytes = base64.b64encode(message_bytes)
# base64_message = base64_bytes.decode('ascii')
# print(base64_message)
base64_bytes = base64_message.encode('ascii')
message_bytes = base64.b64decode(base64_bytes)
message = message_bytes.decode('ascii')
#print(message)
mnemonic = message
# Generate seed from mnemonic
seed_bytes = Bip39SeedGenerator(mnemonic).Generate()
# Construct from seed
bip44_mst_ctx = Bip44.FromSeed(seed_bytes, Bip44Coins.SOLANA)
# Print master key
# print("Master key (bytes): %s" % bip44_mst_ctx.PrivateKey().Raw().ToHex())
# print("Master key (extended): %s" % bip44_mst_ctx.PrivateKey().ToExtended())
# print("Master key (WIF): %s" % bip44_mst_ctx.PrivateKey().ToWif())
bip44_acc_ctx = bip44_mst_ctx.Purpose().Coin().Account( account_id )
bip44_chg_ctx = bip44_acc_ctx.Change(Bip44Changes.CHAIN_EXT)
bip44_addr_ctx = bip44_chg_ctx.AddressIndex( index )
#print("%d. Address: %s %s" % (account_id, bip44_addr_ctx.PublicKey().ToAddress(), bip44_addr_ctx.PrivateKey().ToExtended()) )
priv_key_bytes = bip44_addr_ctx.PrivateKey().Raw().ToBytes()
pub_key_bytes = bip44_addr_ctx.PublicKey().RawCompressed().ToBytes()[1:]
key_pair = priv_key_bytes + pub_key_bytes
print("{\"address\": \"%s\", \"private_key\": \"%s\"}" % (bip44_addr_ctx.PublicKey().ToAddress(), key_pair.hex() ) )