-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitcoin.lua
More file actions
60 lines (49 loc) · 1.56 KB
/
bitcoin.lua
File metadata and controls
60 lines (49 loc) · 1.56 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
local BTC = {}
local ossl = require("openssl")
local dgst = require("openssl").digest
local b58 = require("base58")
local bn = ossl.bn
local sha256 = function(d) return dgst.digest("sha256", d) end
local ripemd160 = function(d) return dgst.digest("ripemd160", d) end
--Based on https://en.bitcoin.it/wiki/Wallet_import_format
function BTC.genWIF(privKey)
--print("==>>Private Key:", bn.tohex(privKey), "size:", bn.tohex(privKey):len())
local s = "80"..bn.tohex(privKey)
local bVal = s
--print("Step2:", s, "size:", s:len())
s= sha256(ossl.hex(s, false))
--print("Step3:", s, "size:", s:len())
s= sha256(ossl.hex(s, false))
--print("Step4:", s, "size:", s:len())
s = s:sub(1,8)
--print("Step5:", s, "size:", s:len())
s = ossl.hex(bVal .. s, false)
--print("Step6:", s, "size:", s:len())
s = b58.enc(s)
--print("Step7:", s, "size:", s:len())
return s
end
-- Based on https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses
function BTC.genPubAddr(pubKey)
--print("==>>Pub. Key:", bn.tohex(pubKey))
local s = "04"..bn.tohex(pubKey)
s = sha256(ossl.hex(s,false))
--print("Step2-sha256",s)
s = ripemd160(ossl.hex(s,false))
--print("Step3-RIPEMD160",s)
s = "00"..s
local RipVer = s
--print("Step4-Ver",s)
s = sha256(ossl.hex(s,false))
--print("Step5-SHA256",s)
s = sha256(ossl.hex(s,false))
--print("Step6-SHA256",s)
crc = s:sub(1,8)
--print("Step7-CRC",crc)
s = RipVer..crc
--print("Step8-Final",s)
s = b58.enc(ossl.hex(s,false))
--print("BTC Address",s)
return s
end
return BTC