generated from well-known-components/template-server
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc0d31b
commit 7222f3c
Showing
11 changed files
with
55 additions
and
847 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,30 +1,38 @@ | ||
import EthCrypto from 'eth-crypto' | ||
import { IBaseComponent } from '@well-known-components/interfaces' | ||
import { randomBytes } from 'crypto' | ||
import secp256k1 from 'secp256k1' | ||
import { sha3 } from 'eth-connect' | ||
|
||
export type HashAndSignResult = { | ||
hash: string | ||
signedHash: string | ||
} | ||
|
||
export type IdentityComponent = IBaseComponent & { | ||
getPublicKey(): string | ||
getAddress(): string | ||
sign(message: string): string | ||
hashAndSign(message: string): HashAndSignResult | ||
} | ||
|
||
export function createIdentityComponent(): IdentityComponent { | ||
const identity = EthCrypto.createIdentity() | ||
let privKey: Uint8Array | ||
do { | ||
privKey = randomBytes(32) | ||
} while (!secp256k1.privateKeyVerify(privKey)) | ||
|
||
function getAddress(): string { | ||
return identity.address | ||
} | ||
const pubKey = secp256k1.publicKeyCreate(privKey) | ||
|
||
function getPublicKey(): string { | ||
return identity.publicKey | ||
return Buffer.from(pubKey).toString('hex') | ||
} | ||
|
||
function sign(message: string): string { | ||
return EthCrypto.sign(identity.privateKey, EthCrypto.hash.keccak256(message)) | ||
function hashAndSign(message: string): HashAndSignResult { | ||
const hash = sha3(message) | ||
const signedHash = Buffer.from(secp256k1.ecdsaSign(Buffer.from(hash, 'hex'), privKey).signature).toString('hex') | ||
return { hash, signedHash } | ||
} | ||
|
||
return { | ||
getAddress, | ||
getPublicKey, | ||
sign | ||
hashAndSign | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { createIdentity } from 'eth-crypto' | ||
import { computeAddress } from '@dcl/crypto/dist/crypto' | ||
import { createIdentityComponent } from '../src/adapters/identity' | ||
|
||
export function generateRandomAddress(): string { | ||
const identity = createIdentity() | ||
return identity.address | ||
const pubKey = createIdentityComponent().getPublicKey() | ||
return computeAddress(Buffer.from(pubKey, 'hex')) | ||
} |
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.