Skip to content

Commit 338f900

Browse files
committed
Update Widentity ECDSA Sign
1 parent 2973242 commit 338f900

File tree

7 files changed

+186
-28
lines changed

7 files changed

+186
-28
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
__pycache__
1+
__pycache__
2+
.idea/

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,39 @@
11
# Weidentity-Python-SDK
22
Weidentity-Python-SDK
3+
## 支持版本
4+
Python3.6+
5+
6+
## How to use
7+
8+
1. 下载weidentity python sdk
9+
```bash
10+
pip install pyweidentity
11+
```
12+
13+
2. 托管模式示例
14+
```python
15+
from pyweidentity.weidentityService import weidentityService
16+
17+
URL = "http://192.168.80.144:6001"
18+
# WeIdentity RestService URL
19+
20+
weid = weidentityService(URL)
21+
create_weid = weid.create_weidentity_did()
22+
print(create_weid)
23+
```
24+
25+
26+
3. 轻客户端模式示例
27+
```python
28+
from pyweidentity.weidentityClient import weidentityClient
29+
import random
30+
31+
URL = "http://192.168.80.144:6001"
32+
# WeIdentity RestService URL
33+
34+
weid = weidentityClient(URL)
35+
privKey = "0xc4a116fb87ae9b8b87842b0f46e1bbf71c37fdae1104fd6d3fd2041e23c8c68e"
36+
nonce = str(random.randint(1, 999999999999999999999999999999))
37+
create_weid = weid.create_weidentity_did(privKey, nonce)
38+
print(create_weid)
39+
```

pyweidentity/Base.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import base64
55
import sha3
66
from ecdsa import SigningKey, SECP256k1
7+
from pyweidentity.localweid import generate_addr
8+
from Crypto.Hash import keccak
9+
from eth_account import Account
710

811
LOG = logging.getLogger(__name__)
912
class Base(object):
@@ -40,7 +43,50 @@ def post(self, url, data):
4043
return None
4144
return response.json()
4245

46+
# def priv_to_public_hex(self, privkey):
47+
# if privkey[:2] == "0x":
48+
# account = generate_addr(priv=privkey[2:])
49+
# else:
50+
# account = generate_addr(priv=hex(int(privkey))[2:])
51+
#
52+
# publickey = account["payload"]["pubv"]
53+
# return publickey
54+
55+
def priv_to_public(self, privkey):
56+
if privkey[:2] == "0x":
57+
account = generate_addr(priv=privkey[2:])
58+
else:
59+
account = generate_addr(priv=hex(int(privkey))[2:])
60+
61+
publickey = str(int(account["payload"]["pubv"], 16))
62+
return publickey
63+
64+
65+
def weid_ecdsa_sign(self, privKey, encode_transaction):
66+
# 轻客户端模式的二次签名 signType is 1.
67+
account = Account.privateKeyToAccount(privKey)
68+
raw_tx_bytes = base64.b64decode(encode_transaction)
69+
70+
msg = keccak.new(digest_bits=256)
71+
msg.update(raw_tx_bytes)
72+
73+
sig = account.signHash(msg.digest())
74+
if (len(hex(sig.r)[2:]) % 2) == 0:
75+
hexed_r = hex(sig.r)[2:]
76+
else:
77+
hexed_r = "0" + hex(sig.r)[2:]
78+
if (len(hex(sig.s)[2:]) % 2) == 0:
79+
hexed_s = hex(sig.s)[2:]
80+
else:
81+
hexed_s = "0" + hex(sig.s)[2:]
82+
b_sig = bytes(bytearray.fromhex(hex(sig.v)[2:] + hexed_r + hexed_s))
83+
b_64_sig = base64.b64encode(b_sig).decode()
84+
# print("sig: {sig}".format(sig=b_64_sig))
85+
return b_64_sig
86+
4387
def ecdsa_sign(self, encode_transaction, privkey, hashfunc=hashlib.sha256):
88+
# 生成证书时需要用到的ecdsa签名
89+
4490
signning_key = SigningKey.from_string(bytes.fromhex(privkey), curve=SECP256k1)
4591
# encode_transaction = respBody['respBody']['encodedTransaction']
4692
# base64解密

pyweidentity/localweid.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,13 @@ def create_random_weid():
2929
}
3030
return data
3131

32-
def create_watting_weid(privkey):
33-
# 通过get的方式传送一个privkey data。
34-
account = generate_addr(priv=privkey)
35-
addr = account["payload"]["addr"]
36-
# 拼接weid,这里CHAIN_ID是留给上链用的。
37-
weid = "did:weid:{addr}".format(addr=addr)
38-
data = {
39-
"privateKeyHex": account["payload"]["priv"],
40-
"publicKeyHex": account["payload"]["pubv"],
41-
"privateKeyInt": str(int(account["payload"]["priv"], 16)),
42-
"publicKeyInt": str(int(account["payload"]["pubv"], 16)),
43-
"weId": weid,
44-
}
45-
return data
32+
def priv_to_public(privkey):
33+
if privkey[:2] == "0x":
34+
account = generate_addr(priv=privkey[2:])
35+
else:
36+
account = generate_addr(priv=hex(int(privkey))[2:])
4637

38+
return account["payload"]["pubv"]
4739

4840
def create_weid_by_privkey(privkey, chain_id):
4941
if privkey[:2] == "0x":

pyweidentity/weidentityClient.py

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import requests
21
import logging
32
from .Base import Base
43
import time
@@ -23,23 +22,35 @@ def create_weidentity_did_first(self, publicKey, nonce):
2322
}
2423
return self.post("/weid/api/encode", data=data_dict)
2524

26-
def create_weidentity_did_second(self, nonce, data, signedMessage):
25+
def create_weidentity_did_second(self, nonce, data, signedMessage, blockLimit, signType="1"):
2726
# 创建WeIdentity DID
2827
data_dict = {
2928
"functionArg": {},
3029
"transactionArg": {
3130
"nonce": nonce,
3231
"data": data,
32+
"blockLimit": blockLimit,
33+
"signType": signType,
3334
"signedMessage": signedMessage
3435
},
3536
"functionName": "createWeId",
3637
"v": self.version
3738
}
3839
return self.post("/weid/api/transact", data=data_dict)
3940

40-
def create_weidentity_did(self, publicKey, nonce, data, signedMessage):
41-
self.create_weidentity_did_first(publicKey, nonce)
42-
return self.create_weidentity_did_second(nonce, data, signedMessage)
41+
def create_weidentity_did(self, privKey, nonce, signType="1"):
42+
publicKey = self.priv_to_public(privKey)
43+
respBody = self.create_weidentity_did_first(publicKey, nonce)
44+
encode_transaction = respBody['respBody']['encodedTransaction']
45+
46+
blockLimit = respBody['respBody']['blockLimit']
47+
48+
raw_result = self.weid_ecdsa_sign(privKey, encode_transaction)
49+
50+
weid_second = self.create_weidentity_did_second(nonce, data=respBody['respBody']['data'], blockLimit=blockLimit,
51+
signType=signType, signedMessage=raw_result)
52+
53+
return weid_second
4354

4455
def register_authority_issuer_first(self, name, weId, nonce):
4556
# 注册Authority Issuer
@@ -56,23 +67,37 @@ def register_authority_issuer_first(self, name, weId, nonce):
5667
}
5768
return self.post("/weid/api/encode", data=data_dict)
5869

59-
def register_authority_issuer_second(self, nonce, data, signedMessage):
70+
def register_authority_issuer_second(self, nonce, data, signedMessage, blockLimit, signType="1"):
6071
# 注册Authority Issuer
6172
data_dict = {
6273
"functionArg": {},
6374
"transactionArg": {
6475
"nonce": nonce,
6576
"data": data,
77+
"blockLimit": blockLimit,
78+
"signType": signType,
6679
"signedMessage": signedMessage
6780
},
6881
"functionName": "registerAuthorityIssuer",
6982
"v": self.version
7083
}
7184
return self.post("/weid/api/transact", data=data_dict)
7285

73-
def register_authority_issuer(self, name, weId, nonce, data, signedMessage):
74-
self.register_authority_issuer_first(name, weId, nonce)
75-
return self.register_authority_issuer_second(nonce, data, signedMessage)
86+
def register_authority_issuer(self, privKey, name, weId, nonce, signType="1"):
87+
88+
respBody = self.register_authority_issuer_first(name, weId, nonce)
89+
90+
encode_transaction = respBody['respBody']['encodedTransaction']
91+
92+
blockLimit = respBody['respBody']['blockLimit']
93+
94+
raw_result = self.weid_ecdsa_sign(privKey, encode_transaction)
95+
96+
authority_issuer_second = self.register_authority_issuer_second(nonce, data=respBody['respBody']['data'], blockLimit=blockLimit,
97+
signType=signType, signedMessage=raw_result)
98+
99+
return authority_issuer_second
100+
76101

77102
def create_cpt_first(self, weId, cptJsonSchema, cptSignature, nonce):
78103
# 创建CPT
@@ -90,23 +115,37 @@ def create_cpt_first(self, weId, cptJsonSchema, cptSignature, nonce):
90115
}
91116
return self.post("/weid/api/encode", data=data_dict)
92117

93-
def create_cpt_second(self, nonce, data, signedMessage):
118+
def create_cpt_second(self, nonce, data, signedMessage, blockLimit, signType="1"):
94119
# 创建CPT
95120
data_dict = {
96121
"functionArg": {},
97122
"transactionArg": {
98123
"nonce": nonce,
99124
"data": data,
125+
"blockLimit": blockLimit,
126+
"signType": signType,
100127
"signedMessage": signedMessage
101128
},
102129
"functionName": "registerCpt",
103130
"v": self.version
104131
}
105132
return self.post("/weid/api/transact", data=data_dict)
106133

107-
def create_cpt(self, name, weId, nonce, data, signedMessage):
108-
self.register_authority_issuer_first(name, weId, nonce)
109-
return self.register_authority_issuer_second(nonce, data, signedMessage)
134+
def create_cpt(self, privKey, weId, cptJsonSchema, cptSignature, nonce, signType="1"):
135+
136+
respBody = self.create_cpt_first(weId, cptJsonSchema, cptSignature, nonce)
137+
138+
encode_transaction = respBody['respBody']['encodedTransaction']
139+
140+
blockLimit = respBody['respBody']['blockLimit']
141+
142+
raw_result = self.weid_ecdsa_sign(privKey, encode_transaction)
143+
144+
cpt_second = self.create_cpt_second(nonce, data=respBody['respBody']['data'], blockLimit=blockLimit,
145+
signType=signType, signedMessage=raw_result)
146+
147+
return cpt_second
148+
110149

111150
def create_credential_pojo(self, cptId, issuer_weid, expirationDate, claim):
112151
# 创建CredentialPojo

setup.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from setuptools import find_packages, setup
2+
3+
with open("README.md", "r", encoding="utf-8") as fh:
4+
long_description = fh.read()
5+
6+
setup(
7+
name='pyweidentity',
8+
version='1.0',
9+
author="99Kies",
10+
author_email='[email protected]',
11+
url="https://github.com/SUIBE-Blockchain/Weidentity-Python-SDK",
12+
packages=find_packages(),
13+
long_description=long_description,
14+
long_description_content_type="text/markdown",
15+
classifiers=[
16+
"Programming Language :: Python :: 3",
17+
"License :: OSI Approved :: Apache Software License",
18+
"Operating System :: OS Independent",
19+
],
20+
)

tests/test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# from pyweidentity.weidentityClient import weidentityClient
2+
# import random
3+
#
4+
# URL = "http://192.168.80.144:6001"
5+
# # WeIdentity RestService URL
6+
#
7+
# weid = weidentityClient(URL)
8+
# privKey = "0xc4a116fb87ae9b8b87842b0f46e1bbf71c37fdae1104fd6d3fd2041e23c6c68e"
9+
# nonce = str(random.randint(1, 999999999999999999999999999999))
10+
# create_weid = weid.create_weidentity_did(privKey, nonce)
11+
# print(create_weid)
12+
13+
14+
# from pyweidentity.weidentityService import weidentityService
15+
#
16+
# URL = "http://192.168.80.144:6001"
17+
# # WeIdentity RestService URL
18+
#
19+
# weid = weidentityService(URL)
20+
# create_weid = weid.create_weidentity_did()
21+
# print(create_weid)
22+
23+
# {'respBody': 'did:weid:1:0x3a08be5b858bbd765a7a914a7d50be31558dd00f', 'loopback': None, 'errorCode': 0, 'errorMessage': 'success'}

0 commit comments

Comments
 (0)