@keplr-wallet/crypto
Advanced tools
Comparing version 0.12.132 to 0.12.133-rc.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Hash = void 0; | ||
const sha_js_1 = require("sha.js"); | ||
const keccak256_1 = require("@ethersproject/keccak256"); | ||
const buffer_1 = require("buffer/"); | ||
const sha2_1 = require("@noble/hashes/sha2"); | ||
const sha3_1 = require("@noble/hashes/sha3"); | ||
class Hash { | ||
static sha256(data) { | ||
return new Uint8Array(new sha_js_1.sha256().update(data).digest()); | ||
return (0, sha2_1.sha256)(data); | ||
} | ||
static keccak256(data) { | ||
return buffer_1.Buffer.from((0, keccak256_1.keccak256)(data).replace("0x", ""), "hex"); | ||
return (0, sha3_1.keccak_256)(data); | ||
} | ||
@@ -14,0 +13,0 @@ static truncHashPortion(str, firstCharCount = str.length, endCharCount = 0) { |
@@ -1,2 +0,1 @@ | ||
import { ec } from "elliptic"; | ||
export declare class PrivKeySecp256k1 { | ||
@@ -24,4 +23,10 @@ protected readonly privKey: Uint8Array; | ||
getEthAddress(): Uint8Array; | ||
toKeyPair(): ec.KeyPair; | ||
getStarknetAddress(salt: Uint8Array, classHash: Uint8Array): Uint8Array; | ||
getStarknetAddressParams(): { | ||
readonly xLow: Uint8Array; | ||
readonly xHigh: Uint8Array; | ||
readonly yLow: Uint8Array; | ||
readonly yHigh: Uint8Array; | ||
}; | ||
verifyDigest32(digest: Uint8Array, signature: Uint8Array): boolean; | ||
} |
102
build/key.js
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.PubKeySecp256k1 = exports.PrivKeySecp256k1 = void 0; | ||
const elliptic_1 = require("elliptic"); | ||
const crypto_js_1 = __importDefault(require("crypto-js")); | ||
const secp256k1_1 = require("@noble/curves/secp256k1"); | ||
const utils = __importStar(require("@noble/curves/abstract/utils")); | ||
const sha2_1 = require("@noble/hashes/sha2"); | ||
const ripemd160_1 = require("@noble/hashes/ripemd160"); | ||
const buffer_1 = require("buffer/"); | ||
const hash_1 = require("./hash"); | ||
const starknet_1 = require("starknet"); | ||
class PrivKeySecp256k1 { | ||
static generateRandomKey() { | ||
const secp256k1 = new elliptic_1.ec("secp256k1"); | ||
return new PrivKeySecp256k1(buffer_1.Buffer.from(secp256k1.genKeyPair().getPrivate().toArray())); | ||
return new PrivKeySecp256k1(secp256k1_1.secp256k1.utils.randomPrivateKey()); | ||
} | ||
@@ -23,5 +45,3 @@ constructor(privKey) { | ||
getPubKey() { | ||
const secp256k1 = new elliptic_1.ec("secp256k1"); | ||
const key = secp256k1.keyFromPrivate(this.privKey); | ||
return new PubKeySecp256k1(new Uint8Array(key.getPublic().encodeCompressed("array"))); | ||
return new PubKeySecp256k1(secp256k1_1.secp256k1.getPublicKey(this.privKey, true)); | ||
} | ||
@@ -32,11 +52,9 @@ signDigest32(digest) { | ||
} | ||
const secp256k1 = new elliptic_1.ec("secp256k1"); | ||
const key = secp256k1.keyFromPrivate(this.privKey); | ||
const signature = key.sign(digest, { | ||
canonical: true, | ||
const signature = secp256k1_1.secp256k1.sign(digest, this.privKey, { | ||
lowS: true, | ||
}); | ||
return { | ||
r: new Uint8Array(signature.r.toArray("be", 32)), | ||
s: new Uint8Array(signature.s.toArray("be", 32)), | ||
v: signature.recoveryParam, | ||
r: utils.numberToBytesBE(signature.r, 32), | ||
s: utils.numberToBytesBE(signature.s, 32), | ||
v: signature.recovery, | ||
}; | ||
@@ -60,8 +78,7 @@ } | ||
} | ||
const keyPair = this.toKeyPair(); | ||
if (uncompressed) { | ||
return new Uint8Array(buffer_1.Buffer.from(keyPair.getPublic().encode("hex", false), "hex")); | ||
return secp256k1_1.secp256k1.ProjectivePoint.fromHex(buffer_1.Buffer.from(this.pubKey).toString("hex")).toRawBytes(false); | ||
} | ||
else { | ||
return new Uint8Array(buffer_1.Buffer.from(keyPair.getPublic().encodeCompressed("hex"), "hex")); | ||
return secp256k1_1.secp256k1.ProjectivePoint.fromHex(buffer_1.Buffer.from(this.pubKey).toString("hex")).toRawBytes(true); | ||
} | ||
@@ -76,5 +93,3 @@ } | ||
getCosmosAddress() { | ||
let hash = crypto_js_1.default.SHA256(crypto_js_1.default.lib.WordArray.create(this.toBytes(false))).toString(); | ||
hash = crypto_js_1.default.RIPEMD160(crypto_js_1.default.enc.Hex.parse(hash)).toString(); | ||
return new Uint8Array(buffer_1.Buffer.from(hash, "hex")); | ||
return (0, ripemd160_1.ripemd160)((0, sha2_1.sha256)(this.toBytes(false))); | ||
} | ||
@@ -88,6 +103,34 @@ getEthAddress() { | ||
} | ||
toKeyPair() { | ||
const secp256k1 = new elliptic_1.ec("secp256k1"); | ||
return secp256k1.keyFromPublic(buffer_1.Buffer.from(this.pubKey).toString("hex"), "hex"); | ||
getStarknetAddress(salt, classHash) { | ||
const pubBytes = this.toBytes(true).slice(1); | ||
const xLow = pubBytes.slice(16, 32); | ||
const xHigh = pubBytes.slice(0, 16); | ||
const yLow = pubBytes.slice(48, 64); | ||
const yHigh = pubBytes.slice(32, 48); | ||
let calculated = starknet_1.hash | ||
.calculateContractAddressFromHash("0x" + buffer_1.Buffer.from(salt).toString("hex"), "0x" + buffer_1.Buffer.from(classHash).toString("hex"), [ | ||
"0x" + buffer_1.Buffer.from(xLow).toString("hex"), | ||
"0x" + buffer_1.Buffer.from(xHigh).toString("hex"), | ||
"0x" + buffer_1.Buffer.from(yLow).toString("hex"), | ||
"0x" + buffer_1.Buffer.from(yHigh).toString("hex"), | ||
], "0x00") | ||
.replace("0x", ""); | ||
const padZero = 64 - calculated.length; | ||
if (padZero > 0) { | ||
calculated = "0".repeat(padZero) + calculated; | ||
} | ||
else if (padZero < 0) { | ||
throw new Error("Invalid length of calculated address"); | ||
} | ||
return new Uint8Array(buffer_1.Buffer.from(calculated, "hex")); | ||
} | ||
getStarknetAddressParams() { | ||
const pubBytes = this.toBytes(true).slice(1); | ||
return { | ||
xLow: pubBytes.slice(16, 32), | ||
xHigh: pubBytes.slice(0, 16), | ||
yLow: pubBytes.slice(48, 64), | ||
yHigh: pubBytes.slice(32, 48), | ||
}; | ||
} | ||
verifyDigest32(digest, signature) { | ||
@@ -100,9 +143,8 @@ if (digest.length !== 32) { | ||
} | ||
const secp256k1 = new elliptic_1.ec("secp256k1"); | ||
const r = signature.slice(0, 32); | ||
const s = signature.slice(32); | ||
return secp256k1.verify(digest, { | ||
r: buffer_1.Buffer.from(r).toString("hex"), | ||
s: buffer_1.Buffer.from(s).toString("hex"), | ||
}, this.toKeyPair()); | ||
return secp256k1_1.secp256k1.verify({ | ||
r: utils.bytesToNumberBE(r), | ||
s: utils.bytesToNumberBE(s), | ||
}, digest, this.pubKey); | ||
} | ||
@@ -109,0 +151,0 @@ } |
{ | ||
"name": "@keplr-wallet/crypto", | ||
"version": "0.12.132", | ||
"version": "0.12.133-rc.0", | ||
"main": "build/index.js", | ||
@@ -18,18 +18,14 @@ "author": "chainapsis", | ||
}, | ||
"devDependencies": { | ||
"@types/crypto-js": "^4.0.1", | ||
"@types/elliptic": "^6.4.12", | ||
"@types/sha.js": "^2.4.0" | ||
}, | ||
"dependencies": { | ||
"@ethersproject/keccak256": "^5.5.0", | ||
"@noble/curves": "^1.4.2", | ||
"@noble/hashes": "^1.4.0", | ||
"bip32": "^2.0.6", | ||
"bip39": "^3.0.3", | ||
"bs58check": "^2.1.2", | ||
"buffer": "^6.0.3", | ||
"crypto-js": "^4.0.0", | ||
"elliptic": "^6.5.3", | ||
"sha.js": "^2.4.11" | ||
"buffer": "^6.0.3" | ||
}, | ||
"gitHead": "c2d43ec60a80e023faee97ded00f582b7605307c" | ||
"peerDependencies": { | ||
"starknet": "^6" | ||
}, | ||
"gitHead": "6496cf28fc619e718b4e2e9e347755730292745b" | ||
} |
@@ -1,12 +0,11 @@ | ||
import { sha256 } from "sha.js"; | ||
import { keccak256 } from "@ethersproject/keccak256"; | ||
import { Buffer } from "buffer/"; | ||
import { sha256 } from "@noble/hashes/sha2"; | ||
import { keccak_256 } from "@noble/hashes/sha3"; | ||
export class Hash { | ||
static sha256(data: Uint8Array): Uint8Array { | ||
return new Uint8Array(new sha256().update(data).digest()); | ||
return sha256(data); | ||
} | ||
static keccak256(data: Uint8Array): Uint8Array { | ||
return Buffer.from(keccak256(data).replace("0x", ""), "hex"); | ||
return keccak_256(data); | ||
} | ||
@@ -13,0 +12,0 @@ |
114
src/key.ts
@@ -1,14 +0,12 @@ | ||
import { ec } from "elliptic"; | ||
import CryptoJS from "crypto-js"; | ||
import { secp256k1 } from "@noble/curves/secp256k1"; | ||
import * as utils from "@noble/curves/abstract/utils"; | ||
import { sha256 } from "@noble/hashes/sha2"; | ||
import { ripemd160 } from "@noble/hashes/ripemd160"; | ||
import { Buffer } from "buffer/"; | ||
import { Hash } from "./hash"; | ||
import { hash as starknetHash } from "starknet"; | ||
export class PrivKeySecp256k1 { | ||
static generateRandomKey(): PrivKeySecp256k1 { | ||
const secp256k1 = new ec("secp256k1"); | ||
return new PrivKeySecp256k1( | ||
Buffer.from(secp256k1.genKeyPair().getPrivate().toArray()) | ||
); | ||
return new PrivKeySecp256k1(secp256k1.utils.randomPrivateKey()); | ||
} | ||
@@ -23,9 +21,3 @@ | ||
getPubKey(): PubKeySecp256k1 { | ||
const secp256k1 = new ec("secp256k1"); | ||
const key = secp256k1.keyFromPrivate(this.privKey); | ||
return new PubKeySecp256k1( | ||
new Uint8Array(key.getPublic().encodeCompressed("array")) | ||
); | ||
return new PubKeySecp256k1(secp256k1.getPublicKey(this.privKey, true)); | ||
} | ||
@@ -42,13 +34,10 @@ | ||
const secp256k1 = new ec("secp256k1"); | ||
const key = secp256k1.keyFromPrivate(this.privKey); | ||
const signature = key.sign(digest, { | ||
canonical: true, | ||
const signature = secp256k1.sign(digest, this.privKey, { | ||
lowS: true, | ||
}); | ||
return { | ||
r: new Uint8Array(signature.r.toArray("be", 32)), | ||
s: new Uint8Array(signature.s.toArray("be", 32)), | ||
v: signature.recoveryParam, | ||
r: utils.numberToBytesBE(signature.r, 32), | ||
s: utils.numberToBytesBE(signature.s, 32), | ||
v: signature.recovery, | ||
}; | ||
@@ -73,11 +62,10 @@ } | ||
const keyPair = this.toKeyPair(); | ||
if (uncompressed) { | ||
return new Uint8Array( | ||
Buffer.from(keyPair.getPublic().encode("hex", false), "hex") | ||
); | ||
return secp256k1.ProjectivePoint.fromHex( | ||
Buffer.from(this.pubKey).toString("hex") | ||
).toRawBytes(false); | ||
} else { | ||
return new Uint8Array( | ||
Buffer.from(keyPair.getPublic().encodeCompressed("hex"), "hex") | ||
); | ||
return secp256k1.ProjectivePoint.fromHex( | ||
Buffer.from(this.pubKey).toString("hex") | ||
).toRawBytes(true); | ||
} | ||
@@ -94,8 +82,3 @@ } | ||
getCosmosAddress(): Uint8Array { | ||
let hash = CryptoJS.SHA256( | ||
CryptoJS.lib.WordArray.create(this.toBytes(false) as any) | ||
).toString(); | ||
hash = CryptoJS.RIPEMD160(CryptoJS.enc.Hex.parse(hash)).toString(); | ||
return new Uint8Array(Buffer.from(hash, "hex")); | ||
return ripemd160(sha256(this.toBytes(false))); | ||
} | ||
@@ -111,11 +94,48 @@ | ||
toKeyPair(): ec.KeyPair { | ||
const secp256k1 = new ec("secp256k1"); | ||
getStarknetAddress(salt: Uint8Array, classHash: Uint8Array): Uint8Array { | ||
const pubBytes = this.toBytes(true).slice(1); | ||
const xLow = pubBytes.slice(16, 32); | ||
const xHigh = pubBytes.slice(0, 16); | ||
const yLow = pubBytes.slice(48, 64); | ||
const yHigh = pubBytes.slice(32, 48); | ||
return secp256k1.keyFromPublic( | ||
Buffer.from(this.pubKey).toString("hex"), | ||
"hex" | ||
); | ||
let calculated = starknetHash | ||
.calculateContractAddressFromHash( | ||
"0x" + Buffer.from(salt).toString("hex"), | ||
"0x" + Buffer.from(classHash).toString("hex"), | ||
[ | ||
"0x" + Buffer.from(xLow).toString("hex"), | ||
"0x" + Buffer.from(xHigh).toString("hex"), | ||
"0x" + Buffer.from(yLow).toString("hex"), | ||
"0x" + Buffer.from(yHigh).toString("hex"), | ||
], | ||
"0x00" | ||
) | ||
.replace("0x", ""); | ||
const padZero = 64 - calculated.length; | ||
if (padZero > 0) { | ||
calculated = "0".repeat(padZero) + calculated; | ||
} else if (padZero < 0) { | ||
throw new Error("Invalid length of calculated address"); | ||
} | ||
return new Uint8Array(Buffer.from(calculated, "hex")); | ||
} | ||
getStarknetAddressParams(): { | ||
readonly xLow: Uint8Array; | ||
readonly xHigh: Uint8Array; | ||
readonly yLow: Uint8Array; | ||
readonly yHigh: Uint8Array; | ||
} { | ||
const pubBytes = this.toBytes(true).slice(1); | ||
return { | ||
xLow: pubBytes.slice(16, 32), | ||
xHigh: pubBytes.slice(0, 16), | ||
yLow: pubBytes.slice(48, 64), | ||
yHigh: pubBytes.slice(32, 48), | ||
}; | ||
} | ||
verifyDigest32(digest: Uint8Array, signature: Uint8Array): boolean { | ||
@@ -130,4 +150,2 @@ if (digest.length !== 32) { | ||
const secp256k1 = new ec("secp256k1"); | ||
const r = signature.slice(0, 32); | ||
@@ -137,10 +155,10 @@ const s = signature.slice(32); | ||
return secp256k1.verify( | ||
digest, | ||
{ | ||
r: Buffer.from(r).toString("hex"), | ||
s: Buffer.from(s).toString("hex"), | ||
r: utils.bytesToNumberBE(r), | ||
s: utils.bytesToNumberBE(s), | ||
}, | ||
this.toKeyPair() | ||
digest, | ||
this.pubKey | ||
); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58341
7
0
830
+ Added@noble/curves@^1.4.2
+ Added@noble/hashes@^1.4.0
+ Added@noble/curves@1.3.01.6.0(transitive)
+ Added@noble/hashes@1.3.3(transitive)
+ Added@scure/base@1.1.9(transitive)
+ Added@scure/starknet@1.0.0(transitive)
+ Added@starknet-io/types-js@0.7.7(transitive)
+ Addedabi-wan-kanabi@2.2.3(transitive)
+ Addedansi-regex@5.0.1(transitive)
+ Addedansi-styles@4.3.0(transitive)
+ Addedansicolors@0.3.2(transitive)
+ Addedcardinal@2.1.1(transitive)
+ Addedcliui@8.0.1(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedemoji-regex@8.0.0(transitive)
+ Addedescalade@3.2.0(transitive)
+ Addedesprima@4.0.1(transitive)
+ Addedfetch-cookie@3.0.1(transitive)
+ Addedfs-extra@10.1.0(transitive)
+ Addedget-caller-file@2.0.5(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedis-fullwidth-code-point@3.0.0(transitive)
+ Addedisomorphic-fetch@3.0.0(transitive)
+ Addedjsonfile@6.1.0(transitive)
+ Addedlossless-json@4.0.2(transitive)
+ Addednode-fetch@2.7.0(transitive)
+ Addedpako@2.1.0(transitive)
+ Addedpsl@1.9.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedquerystringify@2.2.0(transitive)
+ Addedredeyed@2.1.1(transitive)
+ Addedrequire-directory@2.1.1(transitive)
+ Addedrequires-port@1.0.0(transitive)
+ Addedset-cookie-parser@2.7.0(transitive)
+ Addedstarknet@6.15.0(transitive)
+ Addedstring-width@4.2.3(transitive)
+ Addedstrip-ansi@6.0.1(transitive)
+ Addedtough-cookie@4.1.4(transitive)
+ Addedtr46@0.0.3(transitive)
+ Addedts-mixer@6.0.4(transitive)
+ Addeduniversalify@0.2.02.0.1(transitive)
+ Addedurl-parse@1.5.10(transitive)
+ Addedwebidl-conversions@3.0.1(transitive)
+ Addedwhatwg-fetch@3.6.20(transitive)
+ Addedwhatwg-url@5.0.0(transitive)
+ Addedwrap-ansi@7.0.0(transitive)
+ Addedy18n@5.0.8(transitive)
+ Addedyargs@17.7.2(transitive)
+ Addedyargs-parser@21.1.1(transitive)
- Removed@ethersproject/keccak256@^5.5.0
- Removedcrypto-js@^4.0.0
- Removedelliptic@^6.5.3
- Removedsha.js@^2.4.11
- Removed@ethersproject/bytes@5.7.0(transitive)
- Removed@ethersproject/keccak256@5.7.0(transitive)
- Removed@ethersproject/logger@5.7.0(transitive)
- Removedcrypto-js@4.2.0(transitive)
- Removedjs-sha3@0.8.0(transitive)