def sign(credential, pvk, method="ethr"):
    """
    @method str
        default is is ethr
        tz (tz2)
    @credential is dict
    return is str
    Both curve secp256k1 and "alg" :"ES256K-R"
    """
    if not method:
        method = 'ethr'
    key = ethereum_to_jwk256kr(pvk)
    did = didkit.keyToDID(method, key)
    vm = didkit.keyToVerificationMethod(method, key)

    credential["issuer"] = did
    credential["issuanceDate"] = datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ')
    didkit_options = {
        "proofPurpose": "assertionMethod",
        "verificationMethod": vm
    }
    return didkit.issueCredential(
        #credential.__str__().replace("'", '"'),
        json.dumps(credential, ensure_ascii=False),
        didkit_options.__str__().replace("'", '"'),
        key)
Exemple #2
0
    def setUp(self):
        tests.key = "{\"kty\":\"OKP\",\"crv\":\"Ed25519\",\"x\":\"PBcY2yJ4h_cLUnQNcYhplu9KQQBNpGxP4sYcMPdlu6I\",\"d\":\"n5WUFIghmRYZi0rEYo2lz-Zg2B9B1KW4MYfJXwOXfyI\"}"
        tests.did = didkit.keyToDID("key", tests.key)
        tests.verificationMethod = didkit.keyToVerificationMethod(
            "key", tests.key)
        tests.presentation = {
            "@context": ["https://www.w3.org/2018/credentials/v1"],
            "id": "http://example.org/presentations/3731",
            "type": ["VerifiablePresentation"],
            "holder": tests.did,
            "verifiableCredential": {
                "@context": "https://www.w3.org/2018/credentials/v1",
                "id": "http://example.org/credentials/3731",
                "type": ["VerifiableCredential"],
                "issuer": "did:example:30e07a529f32d234f6181736bd3",
                "issuanceDate": "2020-08-19T21:41:50Z",
                "credentialSubject": {
                    "id": "did:example:d23dd687a7dc6787646f2eb98d0",
                },
            },
        }

        tests.verificationPurpose = {
            "proofPurpose": "authentication",
            "verificationMethod": tests.verificationMethod,
        }
Exemple #3
0
    def setUp(self):
        tests.key = "{\"kty\":\"OKP\",\"crv\":\"Ed25519\",\"x\":\"PBcY2yJ4h_cLUnQNcYhplu9KQQBNpGxP4sYcMPdlu6I\",\"d\":\"n5WUFIghmRYZi0rEYo2lz-Zg2B9B1KW4MYfJXwOXfyI\"}"
        tests.did = didkit.keyToDID("key", tests.key)
        tests.verificationMethod = didkit.keyToVerificationMethod(
            "key", tests.key)

        tests.verificationPurpose = {
            "proofPurpose": "authentication",
            "verificationMethod": tests.verificationMethod,
            "challenge": uuid.uuid4().__str__()
        }
Exemple #4
0
def issueCredential(request):
    session_id = request.args.get("session_id")
    session_data = stripe.checkout.Session.retrieve(session_id)

    with open('key.jwk', "r") as f:
        key = f.readline()
    f.close()

    did_key = request.form.get('subject_id', didkit.keyToDID("key", key))
    verification_method = didkit.keyToVerificationMethod("key", key)
    issuance_date = datetime.utcnow().replace(microsecond=0)
    expiration_date = issuance_date + timedelta(weeks=4)

    credential = {
        "id":
        "urn:uuid:" + uuid.uuid4().__str__(),
        "@context": [
            "https://www.w3.org/2018/credentials/v1",
            "https://www.w3.org/2018/credentials/examples/v1",
        ],
        "type": ["VerifiableCredential"],
        "issuer":
        did_key,
        "issuanceDate":
        issuance_date.isoformat() + "Z",
        "expirationDate":
        expiration_date.isoformat() + "Z",
        "credentialSubject": {
            "@context": [{
                "email": "https://schema.org/Text"
            }],
            "id": "urn:uuid:" + uuid.uuid4().__str__(),
            "email": session_data["customer_details"]["email"]
        },
    }

    didkit_options = {
        "proofPurpose": "assertionMethod",
        "verificationMethod": verification_method,
    }

    credential = didkit.issueCredential(
        credential.__str__().replace("'", '"'),
        didkit_options.__str__().replace("'", '"'), key)
    return json.loads(credential)
Exemple #5
0
 def testKeyToVerificationMethod(self):
     self.assertEqual(
         didkit.keyToVerificationMethod("key", tests.key),
         "did:key:z6MkiVpwA241guqtKWAkohHpcAry7S94QQb6ukW3GcCsugbK#z6MkiVpwA241guqtKWAkohHpcAry7S94QQb6ukW3GcCsugbK"
     )
    method = "tz"

    pvk = "0x7f1116bdb705f3e51a299a1fe04b619e0e2516258ef187946076b04151ece8a5"
    key = ethereum_to_jwk256kr(pvk)
    did = didkit.keyToDID(method, key)

    credential = json.load(
        open('/home/thierry/Talao/verifiable_credentials/experience.jsonld',
             'r'))

    credential['credentialSubject']["id"] = "2020-08-19T21:41:50Z"
    credential['id'] = "123123123131321:lkjh:mh"
    credential["issuer"] = did
    credential["issuanceDate"] = "2020-08-19T21:41:50Z"

    credential = sign(credential, pvk, method=method)
    print(json.dumps(json.loads(credential), indent=4))

    key = ethereum_to_jwk256kr(pvk)
    verifmethod = didkit.keyToVerificationMethod(method, key)

    didkit_options = {
        "proofPurpose": "assertionMethod",
        "verificationMethod": verifmethod
    }

    print(
        didkit.verifyCredential(credential,
                                didkit_options.__str__().replace("'", '"')))
print('jwk =', key)

d = json.loads(key)["d"]
private_key = "0x" + base64.urlsafe_b64decode(d + '=' * (4 - len(d) % 4)).hex()
print("private key = ", private_key)
priv_key_bytes = decode_hex(private_key)
priv_key = keys.PrivateKey(priv_key_bytes)
pub_key = priv_key.public_key
print("public key ", pub_key.to_hex())
print("address = ", pub_key.to_checksum_address())

did = "did:key:" + pub_key.to_checksum_address()[2:]
did = didkit.keyToDID("ethr", key)
print("did = ", did)

verifmethod = didkit.keyToVerificationMethod("ethr", key)
print("verifimethod = ", verifmethod)

credential = {
    "@context": "https://www.w3.org/2018/credentials/v1",
    "type": ["VerifiableCredential"],
    "issuer": did,
    "issuanceDate": "2020-08-19T21:41:50Z",
    "credentialSubject": {
        "id": "did:example:d23dd687a7dc6787646f2eb98d0",
    }
}

didkit_options = {
    "proofPurpose": "assertionMethod",
    "verificationMethod": verifmethod