-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: stargate: Implement DID and Token features (#45)
- Loading branch information
Youngjoon Lee
authored
Jul 19, 2021
1 parent
de582ec
commit c36b9b8
Showing
8 changed files
with
353 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import ecc from "secp256k1"; | ||
import { randomBytes } from "crypto"; | ||
|
||
export class Secp256k1 { | ||
static generatePrivateKey(): Uint8Array { | ||
let privKey | ||
do { | ||
privKey = randomBytes(32) | ||
} while (!ecc.privateKeyVerify(privKey)); | ||
return privKey; | ||
} | ||
|
||
static getPublicKeyCompressed(privKey: Uint8Array): Uint8Array { | ||
return ecc.publicKeyCreate(privKey); | ||
} | ||
|
||
static sign(data32: Uint8Array, privKey: Uint8Array): Uint8Array { | ||
return ecc.ecdsaSign(data32, privKey).signature; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { sha256 } from "@cosmjs/crypto"; | ||
import { DIDDocument, DataWithSeq } from "../proto/panacea/did/v2/did"; | ||
import Long from "long"; | ||
import { Secp256k1 } from "../crypto/secp256k1"; | ||
|
||
const bs58 = require('bs58'); | ||
|
||
export class DidUtil { | ||
static getDid(pubKeyCompressed: Uint8Array): string { | ||
return `did:panacea:${bs58.encode(sha256(pubKeyCompressed))}`; | ||
} | ||
|
||
static getPublicKeyBase58(pubKeyCompressed: Uint8Array): string { | ||
return bs58.encode(pubKeyCompressed); | ||
} | ||
|
||
static signDidDocument(privKey: Uint8Array, didDocument: DIDDocument, sequence: Long = Long.fromInt(0)): Uint8Array { | ||
const dataWithSeq: DataWithSeq = { | ||
data: DIDDocument.encode(didDocument).finish(), | ||
sequence: sequence, | ||
}; | ||
return Secp256k1.sign(sha256(DataWithSeq.encode(dataWithSeq).finish()), privKey); | ||
} | ||
|
||
static signDid(privKey: Uint8Array, did: string, sequence: Long = Long.fromInt(0)): Uint8Array { | ||
const didDocument: DIDDocument = { | ||
contexts: undefined, | ||
id: did, | ||
controller: undefined, | ||
verificationMethods: [], | ||
authentications: [], | ||
assertionMethods: [], | ||
keyAgreements: [], | ||
capabilityInvocations: [], | ||
capabilityDelegations: [], | ||
services: [], | ||
} | ||
return this.signDidDocument(privKey, didDocument, sequence); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.