FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
bitcore/util/EncodedData.js at master · nilnullzip/bitcore · GitHub
nilnullzip
bitcore
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
bitcore
/
util
/
EncodedData.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
152 lines (135 loc) · 3.98 KB
Breadcrumbs
bitcore
/
util
/
EncodedData.js
Copy path
File metadata and controls
152 lines (135 loc) · 3.98 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var
base58
=
require
(
'../lib/Base58'
)
.
base58Check
;
// Constructor. Takes the following forms:
// new EncodedData(<base58_address_string>)
// new EncodedData(<binary_buffer>)
// new EncodedData(<data>, <encoding>)
// new EncodedData(<version>, <20-byte-hash>)
function
EncodedData
(
data
,
encoding
)
{
this
.
data
=
data
;
if
(
!
encoding
&&
(
typeof
data
==
'string'
)
)
{
encoding
=
'base58'
;
this
.
converters
=
this
.
encodings
[
encoding
]
.
converters
;
this
.
_encoding
=
this
.
encodings
[
encoding
]
.
_encoding
;
}
else
{
if
(
typeof
this
.
encodings
[
encoding
]
===
'undefined'
)
encoding
=
'binary'
;
this
.
converters
=
this
.
encodings
[
encoding
]
.
converters
;
this
.
_encoding
=
this
.
encodings
[
encoding
]
.
_encoding
;
}
}
;
// get or set the encoding used (transforms data)
EncodedData
.
prototype
.
encoding
=
function
(
encoding
)
{
if
(
encoding
&&
(
encoding
!=
this
.
_encoding
)
)
{
this
.
data
=
this
.
as
(
encoding
)
;
this
.
converters
=
this
.
encodings
[
encoding
]
.
converters
;
this
.
_encoding
=
this
.
encodings
[
encoding
]
.
_encoding
;
}
return
this
.
_encoding
;
}
;
// answer a new instance having the given encoding
EncodedData
.
prototype
.
withEncoding
=
function
(
encoding
)
{
return
new
EncodedData
(
this
.
as
(
encoding
)
,
encoding
)
;
}
;
// answer the data in the given encoding
EncodedData
.
prototype
.
as
=
function
(
encoding
)
{
if
(
!
encodings
[
encoding
]
)
throw
new
Error
(
'invalid encoding: '
+
encoding
)
;
return
this
.
converters
[
encoding
]
.
call
(
this
)
;
}
;
// validate that we can convert to binary
EncodedData
.
prototype
.
_validate
=
function
(
)
{
this
.
withEncoding
(
'binary'
)
;
}
;
// Boolean protocol for testing if valid
EncodedData
.
prototype
.
isValid
=
function
(
)
{
try
{
this
.
validate
(
)
;
return
true
;
}
catch
(
e
)
{
return
false
;
}
}
;
// subclasses can override to do more stuff
EncodedData
.
prototype
.
validate
=
function
(
)
{
this
.
_validate
(
)
;
}
;
// convert to a string (in base58 form)
EncodedData
.
prototype
.
toString
=
function
(
)
{
return
this
.
as
(
'base58'
)
;
}
;
// utility
EncodedData
.
prototype
.
doAsBinary
=
function
(
callback
)
{
var
oldEncoding
=
this
.
encoding
(
)
;
this
.
encoding
(
'binary'
)
;
callback
.
apply
(
this
)
;
this
.
encoding
(
oldEncoding
)
;
}
;
// Setup support for various address encodings. The object for
// each encoding inherits from the EncodedData prototype. This
// allows any encoding to override any method...changing the encoding
// for an instance will change the encoding it inherits from. Note,
// this will present some problems for anyone wanting to inherit from
// EncodedData (we'll deal with that when needed).
var
encodings
=
{
'binary'
:
{
converters
:
{
'binary'
:
function
(
)
{
var
answer
=
new
Buffer
(
this
.
data
.
length
)
;
this
.
data
.
copy
(
answer
)
;
return
answer
;
}
,
'base58'
:
function
(
)
{
return
base58
.
encode
(
this
.
data
)
;
}
,
'hex'
:
function
(
)
{
return
this
.
data
.
toString
(
'hex'
)
;
}
,
}
,
_validate
:
function
(
)
{
//nothing to do here...we make no assumptions about the data
}
,
}
,
'base58'
:
{
converters
:
{
'binary'
:
function
(
)
{
return
base58
.
decode
(
this
.
data
)
;
}
,
'hex'
:
function
(
)
{
return
this
.
withEncoding
(
'binary'
)
.
as
(
'hex'
)
;
}
,
}
,
}
,
'hex'
:
{
converters
:
{
'binary'
:
function
(
)
{
return
new
Buffer
(
this
.
data
,
'hex'
)
;
}
,
'base58'
:
function
(
)
{
return
this
.
withEncoding
(
'binary'
)
.
as
(
'base58'
)
;
}
,
}
,
}
,
}
;
var
no_conversion
=
function
(
)
{
return
this
.
data
;
}
;
for
(
var
k
in
encodings
)
{
if
(
encodings
.
hasOwnProperty
(
k
)
)
{
if
(
!
encodings
[
k
]
.
converters
[
k
]
)
encodings
[
k
]
.
converters
[
k
]
=
no_conversion
;
encodings
[
k
]
.
_encoding
=
k
;
}
}
EncodedData
.
applyEncodingsTo
=
function
(
aClass
)
{
var
tmp
=
{
}
;
for
(
var
k
in
encodings
)
{
var
enc
=
encodings
[
k
]
;
var
obj
=
Object
.
create
(
aClass
.
prototype
)
;
for
(
var
j
in
enc
)
{
obj
[
j
]
=
enc
[
j
]
;
}
tmp
[
k
]
=
obj
;
}
aClass
.
prototype
.
encodings
=
tmp
;
}
;
EncodedData
.
applyEncodingsTo
(
EncodedData
)
;
module
.
exports
=
EncodedData
;
Back
|
FazBrowse Home
|
New Git URL