FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
bitcore/Key.js at master · ecto/bitcore · GitHub
ecto
/
bitcore
Public
forked from
bitpay/bitcore
Notifications
You must be signed in to change notification settings
Fork
2
Star
2
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
bitcore
/
Key.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
106 lines (85 loc) · 2.54 KB
Breadcrumbs
bitcore
/
Key.js
Copy path
File metadata and controls
106 lines (85 loc) · 2.54 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
if
(
process
.
versions
)
{
// c++ native version
var
KeyModule
=
require
(
'bindings'
)
(
'KeyModule'
)
;
var
Key
=
KeyModule
.
Key
;
module
.
exports
=
Key
;
}
else
{
// pure js version
var
ECKey
=
require
(
'./browser/vendor-bundle.js'
)
.
ECKey
;
var
buffertools
=
require
(
'buffertools'
)
;
var
bufferToArray
=
function
(
buffer
)
{
var
ret
=
[
]
;
var
l
=
buffer
.
length
;
for
(
var
i
=
0
;
i
<
l
;
i
++
)
{
ret
.
push
(
buffer
.
readUInt8
(
i
)
)
;
}
return
ret
;
}
var
kSpec
=
function
(
)
{
this
.
_pub
=
null
;
this
.
compressed
=
true
;
// default
}
;
Object
.
defineProperty
(
kSpec
.
prototype
,
'public'
,
{
set
:
function
(
p
)
{
if
(
!
Buffer
.
isBuffer
(
p
)
)
{
throw
new
Error
(
'Arg should be a buffer'
)
;
}
var
type
=
p
[
0
]
;
this
.
compressed
=
type
!==
4
;
this
.
_pub
=
p
;
}
,
get
:
function
(
)
{
return
this
.
_pub
;
}
}
)
;
kSpec
.
generateSync
=
function
(
)
{
var
eck
=
new
ECKey
(
)
;
eck
.
setCompressed
(
true
)
;
var
pub
=
eck
.
getPub
(
)
;
var
ret
=
new
kSpec
(
)
;
ret
.
private
=
new
Buffer
(
eck
.
priv
.
toByteArrayUnsigned
(
)
)
;
ret
.
public
=
new
Buffer
(
pub
)
;
return
ret
;
}
;
kSpec
.
prototype
.
regenerateSync
=
function
(
)
{
if
(
!
this
.
private
)
{
throw
new
Error
(
'Key does not have a private key set'
)
;
}
var
eck
=
new
ECKey
(
buffertools
.
toHex
(
this
.
private
)
)
;
eck
.
setCompressed
(
this
.
compressed
)
;
this
.
public
=
new
Buffer
(
eck
.
getPub
(
)
)
;
return
this
;
}
;
kSpec
.
prototype
.
signSync
=
function
(
hash
)
{
if
(
!
this
.
private
)
{
throw
new
Error
(
'Key does not have a private key set'
)
;
}
if
(
!
Buffer
.
isBuffer
(
hash
)
||
hash
.
length
!==
32
)
{
throw
new
Error
(
'Arg should be a 32 bytes hash buffer'
)
;
}
var
eck
=
new
ECKey
(
buffertools
.
toHex
(
this
.
private
)
)
;
eck
.
setCompressed
(
this
.
compressed
)
;
var
signature
=
eck
.
sign
(
bufferToArray
(
hash
)
)
;
// return it as a buffer to keep c++ compatibility
return
new
Buffer
(
signature
)
;
}
;
kSpec
.
prototype
.
verifySignatureSync
=
function
(
hash
,
sig
)
{
var
self
=
this
;
if
(
!
Buffer
.
isBuffer
(
hash
)
||
hash
.
length
!==
32
)
{
throw
new
Error
(
'Arg 1 should be a 32 bytes hash buffer'
)
;
}
if
(
!
Buffer
.
isBuffer
(
sig
)
)
{
throw
new
Error
(
'Arg 2 should be a buffer'
)
;
}
if
(
!
self
.
public
)
{
throw
new
Error
(
'Key does not have a public key set'
)
;
}
var
eck
=
new
ECKey
(
)
;
eck
.
setPub
(
bufferToArray
(
self
.
public
)
)
;
eck
.
setCompressed
(
self
.
compressed
)
;
var
sigA
=
bufferToArray
(
sig
)
;
var
ret
=
eck
.
verify
(
bufferToArray
(
hash
)
,
sigA
)
;
return
ret
;
}
;
module
.
exports
=
kSpec
;
}
Back
|
FazBrowse Home
|
New Git URL