FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
bitcore/test/test.basic.js at master · deckar01/bitcore · GitHub
deckar01
bitcore
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
bitcore
/
test
/
test.basic.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
127 lines (104 loc) · 3.28 KB
Breadcrumbs
bitcore
/
test
/
test.basic.js
Copy path
File metadata and controls
127 lines (104 loc) · 3.28 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
'use strict'
;
var
chai
=
chai
||
require
(
'chai'
)
;
var
bitcore
=
bitcore
||
require
(
'../bitcore'
)
;
var
testdata
=
testdata
||
require
(
'./testdata'
)
;
var
should
=
chai
.
should
(
)
;
var
Address
=
bitcore
.
Address
;
var
PrivateKey
=
bitcore
.
PrivateKey
;
var
networks
=
bitcore
.
networks
;
var
Key
=
bitcore
.
Key
;
function
test_encode_priv
(
b58
,
payload
,
isTestnet
,
isCompressed
)
{
var
network
=
isTestnet
?
networks
.
testnet
:
networks
.
livenet
;
var
version
=
network
.
privKeyVersion
;
var
buf_pl
=
new
Buffer
(
payload
,
'hex'
)
;
var
buf
;
if
(
isCompressed
)
{
buf
=
new
Buffer
(
buf_pl
.
length
+
1
)
;
buf_pl
.
copy
(
buf
)
;
buf
[
buf_pl
.
length
]
=
1
;
}
else
buf
=
buf_pl
;
var
key
=
new
Key
(
)
;
key
.
private
=
buf
;
key
.
compressed
=
isCompressed
;
var
privkey
=
new
PrivateKey
(
version
,
buf
)
;
privkey
.
toString
(
)
.
should
.
equal
(
b58
)
;
}
function
test_encode_pub
(
b58
,
payload
,
isTestnet
,
addrType
)
{
var
isScript
=
(
addrType
===
'script'
)
;
var
network
=
isTestnet
?
networks
.
testnet
:
networks
.
livenet
;
var
version
=
isScript
?
network
.
P2SHVersion
:
network
.
addressVersion
;
var
buf
=
new
Buffer
(
payload
,
'hex'
)
;
var
addr
=
new
Address
(
version
,
buf
)
;
addr
.
toString
(
)
.
should
.
equal
(
b58
)
;
}
function
test_decode_priv
(
b58
,
payload
,
isTestnet
,
isCompressed
)
{
var
network
=
isTestnet
?
networks
.
testnet
:
networks
.
livenet
;
var
version
=
network
.
privKeyVersion
;
var
buf_pl
=
new
Buffer
(
payload
,
'hex'
)
;
var
buf
;
if
(
isCompressed
)
{
buf
=
new
Buffer
(
buf_pl
.
length
+
1
)
;
buf_pl
.
copy
(
buf
)
;
buf
[
buf_pl
.
length
]
=
1
;
}
else
buf
=
buf_pl
;
var
privkey
=
new
PrivateKey
(
b58
)
;
version
.
should
.
equal
(
privkey
.
version
(
)
)
;
buf_pl
.
toString
(
)
.
should
.
equal
(
privkey
.
payload
(
)
.
toString
(
)
)
;
}
function
test_decode_pub
(
b58
,
payload
,
isTestnet
,
addrType
)
{
var
isScript
=
(
addrType
===
'script'
)
;
var
network
=
isTestnet
?
networks
.
testnet
:
networks
.
livenet
;
var
version
=
isScript
?
network
.
P2SHVersion
:
network
.
addressVersion
;
var
buf
=
new
Buffer
(
payload
,
'hex'
)
;
var
addr
=
new
Address
(
b58
)
;
version
.
should
.
equal
(
addr
.
version
(
)
)
;
buf
.
toString
(
)
.
should
.
equal
(
addr
.
payload
(
)
.
toString
(
)
)
;
}
function
is_valid
(
datum
)
{
var
b58
=
datum
[
0
]
;
var
payload
=
datum
[
1
]
;
var
obj
=
datum
[
2
]
;
var
isPrivkey
=
obj
.
isPrivkey
;
var
isTestnet
=
obj
.
isTestnet
;
if
(
isPrivkey
)
{
var
isCompressed
=
obj
.
isCompressed
;
test_encode_priv
(
b58
,
payload
,
isTestnet
,
isCompressed
)
;
test_decode_priv
(
b58
,
payload
,
isTestnet
,
isCompressed
)
;
}
else
{
var
addrType
=
obj
.
addrType
;
test_encode_pub
(
b58
,
payload
,
isTestnet
,
addrType
)
;
test_decode_pub
(
b58
,
payload
,
isTestnet
,
addrType
)
;
}
}
function
is_invalid
(
datum
)
{
if
(
datum
.
length
<
1
)
throw
new
Error
(
'Bad test'
)
;
// ignore succeeding elements, as comments
var
b58
=
datum
[
0
]
;
var
privkey
=
new
PrivateKey
(
b58
)
;
var
addr
=
new
Address
(
b58
)
;
var
valid
=
true
;
try
{
privkey
.
validate
(
)
;
addr
.
validate
(
)
;
}
catch
(
e
)
{
valid
=
false
;
}
valid
.
should
.
equal
(
false
)
;
}
describe
(
'Valid base58 keys'
,
function
(
)
{
testdata
.
dataValid
.
forEach
(
function
(
datum
)
{
it
(
'valid '
+
datum
[
0
]
,
function
(
)
{
is_valid
(
datum
)
;
}
)
;
}
)
;
}
)
;
describe
(
'Invalid base58 keys'
,
function
(
)
{
testdata
.
dataInvalid
.
forEach
(
function
(
datum
)
{
it
(
'invalid '
+
datum
,
function
(
)
{
is_invalid
(
datum
)
;
}
)
;
}
)
;
}
)
;
Back
|
FazBrowse Home
|
New Git URL