Skip to content
This repository was archived by the owner on Mar 7, 2023. It is now read-only.

Commit d73d1cf

Browse files
committed
Fix IllegalFormat problem in deploy pyscore/bmc
- remove umsgpack, wrap - add pure base64 implement - using '*' for iconservice import
1 parent 060c883 commit d73d1cf

File tree

14 files changed

+190
-1319
lines changed

14 files changed

+190
-1319
lines changed

pyscore/bmc/link.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from iconservice import Address, IconScoreDatabase
15+
from iconservice import *
1616

1717
from .relay import BTPRelays, BTPRelay
1818
from ..lib import BTPAddress, BTPException

pyscore/bmc/relay.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from iconservice import IconScoreDatabase, Address
15+
from iconservice import *
1616

1717
from ..lib.icon import PropertiesDB, IterableDictDB
1818

pyscore/bmc/relayer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
99
# See the License for the specific language governing permissions and
1010
# limitations under the License.
11-
from iconservice import IconScoreDatabase, Address
11+
from iconservice import *
1212

1313
from ..lib import BTPException, BTPAddress
1414
from ..lib.icon import IterableDictDB, PropertiesDB

pyscore/bmv/icon/message.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@
2323
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2424
# See the License for the specific language governing permissions and
2525
# limitations under the License.
26-
from iconservice import Address
26+
from iconservice import *
2727

2828
from ..exception import *
2929
from ...lib import BMVException
3030
from ...lib.icon import rlp, base64, Serializable
3131
from ...lib.icon.mta import MerkleTreeAccumulator, MTAException, InvalidWitnessOldException
3232
from ...lib.icon.mpt import MerklePatriciaTree, MPTException
33-
from ...lib.icon.wrap import get_hash, recover_public_key, address_by_public_key
3433

3534

3635
# ================================================
@@ -39,7 +38,7 @@
3938
class BlockHeader(object):
4039
def __init__(self, serialized: bytes) -> None:
4140
self.__bytes = serialized
42-
self.__hash = get_hash(serialized)
41+
self.__hash = sha3_256(serialized)
4342

4443
unpacked = rlp.rlp_decode(self.__bytes,
4544
[int, int, int, bytes, bytes, bytes, bytes, bytes, bytes, bytes, bytes])
@@ -96,7 +95,7 @@ def __init__(self, serialized: bytes) -> None:
9695
self.__bytes = serialized
9796
self.__addresses = []
9897
if serialized is not None:
99-
self.__hash = get_hash(serialized)
98+
self.__hash = sha3_256(serialized)
10099
unpacked = rlp.rlp_decode(self.__bytes, {list: bytes})
101100
for b in unpacked:
102101
address = Address.from_bytes(b)
@@ -195,9 +194,9 @@ def verify(self, height: int, block_id: bytes, validators: Validators) -> None:
195194
for vote_item in self.__vote_items:
196195
vote_msg.append(vote_item.timestamp)
197196
serialized_vote_msg = rlp.rlp_encode(vote_msg)
198-
msg_hash = get_hash(serialized_vote_msg)
199-
public_key = recover_public_key(msg_hash, vote_item.signature)
200-
addr = address_by_public_key(public_key)
197+
msg_hash = sha3_256(serialized_vote_msg)
198+
public_key = recover_key(msg_hash, vote_item.signature)
199+
addr = create_address_with_key(public_key)
201200
if not validators.contains(addr):
202201
raise InvalidVotesException("invalid signature")
203202
if addr not in contained_validators:

pyscore/bmv/icon/tests/test_unit_iconee.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from ..icon import *
33
from ....lib.icon import rlp, base64
44
from ....lib.icon.mta import MerkleTreeAccumulator
5-
from iconservice import Address, AddressPrefix
5+
from iconservice import *
66
from tbears.libs.scoretest.score_test_case import ScoreTestCase
77
from tbears.libs.icon_integrate_test import IconIntegrateTestBase
88
from typing import List
@@ -14,7 +14,7 @@
1414
class Key(object):
1515
def __init__(self, secret=None) -> None:
1616
self.private_key = PrivateKey(secret)
17-
self.addr = address_by_public_key(self.private_key.public_key.format(False))
17+
self.addr = create_address_with_key(self.private_key.public_key.format(False))
1818

1919
def sign(self, _hash: bytes) -> bytes:
2020
return self.private_key.sign_recoverable(_hash, None)

pyscore/lib/btp_abstract.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from iconservice import ABC, abstractmethod
15+
from iconservice import *
1616

1717

1818
class BTPServiceHandler(ABC):

pyscore/lib/btp_exception.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from iconservice import IconScoreException
15+
from iconservice import *
1616
from .const import Const
1717

1818

pyscore/lib/btp_interface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from iconservice import InterfaceScore, interface, Address
15+
from iconservice import *
1616

1717

1818
class BMCInterfaceForBSH(InterfaceScore):

pyscore/lib/icon/base64.py

Lines changed: 171 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,175 @@
1-
import base64
2-
3-
41
def urlsafe_b64encode(bs: bytes) -> str:
5-
v = base64.urlsafe_b64encode(bs)
6-
if isinstance(v, bytes):
7-
return v.decode('utf-8')
8-
return v
2+
return encode(bs, True).decode('utf-8')
93

104

115
def urlsafe_b64decode(s: str) -> bytes:
12-
return base64.urlsafe_b64decode(s)
6+
return base64url_decode(s)
7+
8+
9+
10+
_to_base64 = [
11+
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
12+
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
13+
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
14+
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
15+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
16+
]
17+
18+
_to_base64_url = [
19+
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
20+
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
21+
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
22+
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
23+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'
24+
]
25+
26+
27+
def _from_base64(base64: list) -> list:
28+
result = [-1] * 256
29+
for i, v in enumerate(base64):
30+
result[ord(v)] = i
31+
result[ord('=')] = -2
32+
return result
33+
34+
35+
def encode(s, is_url: bool = False) -> bytes:
36+
base64 = _to_base64_url if is_url else _to_base64
37+
if isinstance(s, bytes):
38+
src = s
39+
elif isinstance(s, str):
40+
src = bytes(s, 'utf-8')
41+
42+
dst = bytearray(_encode_length(len(src)))
43+
44+
start = 0
45+
end = len(src)
46+
slen = int(end / 3) * 3
47+
48+
dp = 0
49+
sl0 = slen
50+
while start < slen:
51+
sp0 = start
52+
dp0 = dp
53+
while sp0 < sl0:
54+
bits = (src[sp0] & 0xff) << 16 | (src[sp0+1] & 0xff) << 8 | (src[sp0+2] & 0xff)
55+
sp0 = sp0 + 3
56+
dst[dp0] = ord(base64[(bits >> 18) & 0x3f])
57+
dst[dp0+1] = ord(base64[(bits >> 12) & 0x3f])
58+
dst[dp0+2] = ord(base64[(bits >> 6) & 0x3f])
59+
dst[dp0+3] = ord(base64[bits & 0x3f])
60+
dp0 = dp0 + 4
61+
62+
dlen = int((sl0 - start) / 3) * 4
63+
dp += dlen
64+
start = sl0
65+
66+
if start < end:
67+
b0 = src[start] & 0xff
68+
start = start + 1
69+
dst[dp] = ord(base64[b0 >> 2])
70+
dp = dp + 1
71+
if start == end:
72+
dst[dp] = ord(base64[b0 << 4 & 0x3f])
73+
dst[dp+1] = ord('=')
74+
dst[dp+2] = ord('=')
75+
else:
76+
b1 = src[start] & 0xff
77+
dst[dp] = ord(base64[b0 << 4 & 0x3f | (b1 >> 4)])
78+
dst[dp+1] = ord(base64[b1 << 2 & 0x3f])
79+
dst[dp+2] = ord('=')
80+
81+
return bytes(dst)
82+
83+
84+
def decode(src, is_url: bool = False) -> bytes:
85+
if isinstance(src, str):
86+
src = bytes(src, 'utf-8')
87+
88+
dst = bytearray(_decode_length(src))
89+
90+
sp = 0
91+
sl = len(src)
92+
93+
dp = 0
94+
bits = 0
95+
shiftto = 18
96+
97+
while sp < sl:
98+
b = src[sp] & 0xff
99+
sp = sp + 1
100+
base64 = _to_base64_url if is_url else _to_base64
101+
b = _from_base64(base64)[b]
102+
if b < 0:
103+
if b == -2:
104+
if shiftto == 6 and (sp == sl or src[sp] != ord('=')) or shiftto == 18:
105+
raise Exception('Input byte array has wrong 4-byte ending unit')
106+
sp = sp + 1
107+
break
108+
109+
raise Exception(f'Illegal base64 character {chr(src[sp - 1])}')
110+
111+
bits = bits | (b << shiftto)
112+
shiftto -= 6
113+
if shiftto < 0:
114+
dst[dp] = (bits >> 16 & 0xff)
115+
dst[dp+1] = (bits >> 8 & 0xff)
116+
dst[dp+2] = bits & 0xff
117+
dp = dp + 3
118+
shiftto = 18
119+
bits = 0
120+
121+
if shiftto == 6:
122+
dst[dp] = (bits >> 16 & 0xff)
123+
elif shiftto == 0:
124+
dst[dp] = (bits >> 16 & 0xff)
125+
dst[dp+1] = (bits >> 8 & 0xff)
126+
elif shiftto == 12:
127+
raise Exception('Last unit does not have enough valid bits')
128+
129+
if sp < sl:
130+
raise Exception(f'Input byte array has incorrect ending byte at {sp}')
131+
132+
return bytes(dst)
133+
134+
135+
def urlsafe_encode(s: str) -> bytes:
136+
return encode(s, True)
137+
138+
139+
def urlsafe_decode(s) -> bytes:
140+
return decode(s, True)
141+
142+
143+
def _encode_length(l: int) -> int:
144+
return 4 * int((l + 2) / 3)
145+
146+
147+
def _decode_length(s: bytes) -> int:
148+
paddings = 0
149+
start = 0
150+
sl = len(s)
151+
l = sl - start
152+
if l == 0:
153+
return 0
154+
if l < 2:
155+
raise Exception('Input byte[] should at least have 2 bytes for base64 bytes')
156+
157+
if s[sl - 1] == ord('='):
158+
paddings = paddings + 1
159+
if s[sl - 2] == ord('='):
160+
paddings = paddings + 1
161+
162+
if paddings == 0 and l & 0x3 != 0:
163+
paddings = 4 - (l & 0x3)
164+
return 3 * (int((l + 3) / 4)) - paddings
165+
166+
167+
def base64url_decode(s) -> bytes:
168+
if isinstance(s, str):
169+
s = s.encode('ascii')
170+
171+
rem = len(s) % 4
172+
if rem > 0:
173+
s += b'=' * (4 - rem)
174+
return urlsafe_decode(s)
175+

pyscore/lib/icon/db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from iconservice import IconScoreDatabase, DictDB, VarDB, ABC, abstractmethod, Address, ArrayDB
15+
from iconservice import *
1616

1717
from .. import Const, BTPAddress
1818

0 commit comments

Comments
 (0)