-
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.
✂️ Split out signers to separate module
- Loading branch information
1 parent
53e17b6
commit 81acfcc
Showing
13 changed files
with
219 additions
and
198 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
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Point, recoverSigner, sign } from "../crypto/secp256k1"; | ||
import { keccak256Uint32 } from "../crypto/sha3"; | ||
import { hex, uint8ArrayBEyeSayıdan } from "../util/çevir"; | ||
import eth from "./eth.d"; | ||
import evm from "./evm"; | ||
|
||
/** | ||
* @param {!Point} Q | ||
* @return {string} address | ||
*/ | ||
const pointToAddress = (Q) => { | ||
/** @const {!Uint8Array} */ | ||
const buff = new Uint8Array(64); | ||
uint8ArrayBEyeSayıdan(buff, 32, Q.x); | ||
uint8ArrayBEyeSayıdan(buff, 64, Q.y); | ||
/** @const {!Uint8Array} */ | ||
const hash = new Uint8Array( | ||
keccak256Uint32(new Uint32Array(buff.buffer)).buffer, 12, 20); | ||
return "0x" + hex(hash); | ||
} | ||
|
||
/** | ||
* Given a digest and a signature, recovers the signer address if the signature | ||
* is valid; outputs an arbitrary value otherwise. | ||
* | ||
* @param {string} digest as a length 64 hex string | ||
* @param {eth.CompactSignature} signature as a length 128 compact signature | ||
* @return {string} 42 characters long EVM address | ||
*/ | ||
const signerAddress = (digest, signature) => { | ||
/** @const {number} */ | ||
const highNibble = parseInt(signature[64], 16); | ||
/** @const {boolean} */ | ||
const yParity = highNibble >= 8; | ||
/** @const {bigint} */ | ||
const r = BigInt("0x" + signature.slice(0, 64)); | ||
/** @const {bigint} */ | ||
const s = BigInt("0x" + (yParity | ||
? (highNibble - 8).toString(16) + signature.slice(65) | ||
: signature.slice(64)) | ||
); | ||
return pointToAddress( | ||
recoverSigner(BigInt("0x" + digest), r, s, yParity)); | ||
} | ||
|
||
/** | ||
* @param {string} digest | ||
* @param {bigint} privateKey | ||
* @return {eth.CompactSignature} | ||
*/ | ||
const signCompact = (digest, privateKey) => { | ||
const { r, s, yParity } = sign(BigInt("0x" + digest), privateKey); | ||
return evm.uint256(r) + evm.uint256(yParity ? s + (1n << 255n) : s); | ||
} | ||
|
||
export { | ||
pointToAddress, | ||
signCompact, | ||
signerAddress | ||
}; |
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.