web3-eth-accounts
Advanced tools
Comparing version 4.1.4-dev.463d070.0 to 4.1.4-dev.4f8e8cc.0
@@ -43,2 +43,9 @@ import { Address, Bytes, CipherOptions, HexString, KeyStore } from 'web3-types'; | ||
/** | ||
* Takes a hash of a message and a private key, signs the message using the SECP256k1 elliptic curve algorithm, and returns the signature components. | ||
* @param hash - The hash of the message to be signed, represented as a hexadecimal string. | ||
* @param privateKey - The private key used to sign the message, represented as a byte array. | ||
* @returns - The signature Object containing the message, messageHash, signature r, s, v | ||
*/ | ||
export declare const signMessageWithPrivateKey: (hash: HexString, privateKey: Bytes) => SignResult; | ||
/** | ||
* Signs arbitrary data with a given private key. | ||
@@ -45,0 +52,0 @@ * :::info |
@@ -28,3 +28,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.decrypt = exports.create = exports.privateKeyToAccount = exports.encrypt = exports.privateKeyToPublicKey = exports.privateKeyToAddress = exports.recover = exports.recoverTransaction = exports.signTransaction = exports.sign = exports.hashMessage = exports.parseAndValidatePrivateKey = void 0; | ||
exports.decrypt = exports.create = exports.privateKeyToAccount = exports.encrypt = exports.privateKeyToPublicKey = exports.privateKeyToAddress = exports.recover = exports.recoverTransaction = exports.signTransaction = exports.sign = exports.signMessageWithPrivateKey = exports.hashMessage = exports.parseAndValidatePrivateKey = void 0; | ||
/** | ||
@@ -129,2 +129,24 @@ * The web3 accounts package contains functions to generate Ethereum accounts and sign transactions & data. | ||
/** | ||
* Takes a hash of a message and a private key, signs the message using the SECP256k1 elliptic curve algorithm, and returns the signature components. | ||
* @param hash - The hash of the message to be signed, represented as a hexadecimal string. | ||
* @param privateKey - The private key used to sign the message, represented as a byte array. | ||
* @returns - The signature Object containing the message, messageHash, signature r, s, v | ||
*/ | ||
const signMessageWithPrivateKey = (hash, privateKey) => { | ||
const privateKeyUint8Array = (0, exports.parseAndValidatePrivateKey)(privateKey); | ||
const signature = constants_js_1.secp256k1.sign(hash.substring(2), privateKeyUint8Array); | ||
const signatureBytes = signature.toCompactRawBytes(); | ||
const r = signature.r.toString(16).padStart(64, '0'); | ||
const s = signature.s.toString(16).padStart(64, '0'); | ||
const v = signature.recovery + 27; | ||
return { | ||
messageHash: hash, | ||
v: (0, web3_utils_1.numberToHex)(v), | ||
r: `0x${r}`, | ||
s: `0x${s}`, | ||
signature: `${(0, web3_utils_1.bytesToHex)(signatureBytes)}${v.toString(16)}`, | ||
}; | ||
}; | ||
exports.signMessageWithPrivateKey = signMessageWithPrivateKey; | ||
/** | ||
* Signs arbitrary data with a given private key. | ||
@@ -152,16 +174,11 @@ * :::info | ||
const sign = (data, privateKey) => { | ||
const privateKeyUint8Array = (0, exports.parseAndValidatePrivateKey)(privateKey); | ||
const hash = (0, exports.hashMessage)(data); | ||
const signature = constants_js_1.secp256k1.sign(hash.substring(2), privateKeyUint8Array); | ||
const signatureBytes = signature.toCompactRawBytes(); | ||
const r = signature.r.toString(16).padStart(64, '0'); | ||
const s = signature.s.toString(16).padStart(64, '0'); | ||
const v = signature.recovery + 27; | ||
const { messageHash, v, r, s, signature } = (0, exports.signMessageWithPrivateKey)(hash, privateKey); | ||
return { | ||
message: data, | ||
messageHash: hash, | ||
v: (0, web3_utils_1.numberToHex)(v), | ||
r: `0x${r}`, | ||
s: `0x${s}`, | ||
signature: `${(0, web3_utils_1.bytesToHex)(signatureBytes)}${v.toString(16)}`, | ||
messageHash, | ||
v, | ||
r, | ||
s, | ||
signature, | ||
}; | ||
@@ -168,0 +185,0 @@ }; |
@@ -123,2 +123,23 @@ /* | ||
/** | ||
* Takes a hash of a message and a private key, signs the message using the SECP256k1 elliptic curve algorithm, and returns the signature components. | ||
* @param hash - The hash of the message to be signed, represented as a hexadecimal string. | ||
* @param privateKey - The private key used to sign the message, represented as a byte array. | ||
* @returns - The signature Object containing the message, messageHash, signature r, s, v | ||
*/ | ||
export const signMessageWithPrivateKey = (hash, privateKey) => { | ||
const privateKeyUint8Array = parseAndValidatePrivateKey(privateKey); | ||
const signature = secp256k1.sign(hash.substring(2), privateKeyUint8Array); | ||
const signatureBytes = signature.toCompactRawBytes(); | ||
const r = signature.r.toString(16).padStart(64, '0'); | ||
const s = signature.s.toString(16).padStart(64, '0'); | ||
const v = signature.recovery + 27; | ||
return { | ||
messageHash: hash, | ||
v: numberToHex(v), | ||
r: `0x${r}`, | ||
s: `0x${s}`, | ||
signature: `${bytesToHex(signatureBytes)}${v.toString(16)}`, | ||
}; | ||
}; | ||
/** | ||
* Signs arbitrary data with a given private key. | ||
@@ -146,16 +167,11 @@ * :::info | ||
export const sign = (data, privateKey) => { | ||
const privateKeyUint8Array = parseAndValidatePrivateKey(privateKey); | ||
const hash = hashMessage(data); | ||
const signature = secp256k1.sign(hash.substring(2), privateKeyUint8Array); | ||
const signatureBytes = signature.toCompactRawBytes(); | ||
const r = signature.r.toString(16).padStart(64, '0'); | ||
const s = signature.s.toString(16).padStart(64, '0'); | ||
const v = signature.recovery + 27; | ||
const { messageHash, v, r, s, signature } = signMessageWithPrivateKey(hash, privateKey); | ||
return { | ||
message: data, | ||
messageHash: hash, | ||
v: numberToHex(v), | ||
r: `0x${r}`, | ||
s: `0x${s}`, | ||
signature: `${bytesToHex(signatureBytes)}${v.toString(16)}`, | ||
messageHash, | ||
v, | ||
r, | ||
s, | ||
signature, | ||
}; | ||
@@ -162,0 +178,0 @@ }; |
@@ -43,2 +43,9 @@ import { Address, Bytes, CipherOptions, HexString, KeyStore } from 'web3-types'; | ||
/** | ||
* Takes a hash of a message and a private key, signs the message using the SECP256k1 elliptic curve algorithm, and returns the signature components. | ||
* @param hash - The hash of the message to be signed, represented as a hexadecimal string. | ||
* @param privateKey - The private key used to sign the message, represented as a byte array. | ||
* @returns - The signature Object containing the message, messageHash, signature r, s, v | ||
*/ | ||
export declare const signMessageWithPrivateKey: (hash: HexString, privateKey: Bytes) => SignResult; | ||
/** | ||
* Signs arbitrary data with a given private key. | ||
@@ -45,0 +52,0 @@ * :::info |
{ | ||
"name": "web3-eth-accounts", | ||
"version": "4.1.4-dev.463d070.0+463d070", | ||
"version": "4.1.4-dev.4f8e8cc.0+4f8e8cc", | ||
"description": "Package for managing Ethereum accounts and signing", | ||
@@ -58,3 +58,3 @@ "main": "./lib/commonjs/index.js", | ||
"typescript": "^4.7.4", | ||
"web3-providers-ipc": "4.0.8-dev.463d070.0+463d070" | ||
"web3-providers-ipc": "4.0.8-dev.4f8e8cc.0+4f8e8cc" | ||
}, | ||
@@ -65,8 +65,8 @@ "dependencies": { | ||
"ethereum-cryptography": "^2.0.0", | ||
"web3-errors": "1.2.1-dev.463d070.0+463d070", | ||
"web3-types": "1.7.1-dev.463d070.0+463d070", | ||
"web3-utils": "4.3.2-dev.463d070.0+463d070", | ||
"web3-validator": "2.0.7-dev.463d070.0+463d070" | ||
"web3-errors": "1.2.2-dev.4f8e8cc.0+4f8e8cc", | ||
"web3-types": "1.7.1-dev.4f8e8cc.0+4f8e8cc", | ||
"web3-utils": "4.3.2-dev.4f8e8cc.0+4f8e8cc", | ||
"web3-validator": "2.0.7-dev.4f8e8cc.0+4f8e8cc" | ||
}, | ||
"gitHead": "463d070a574a6e09e8c3319bd54fadfd8518d847" | ||
"gitHead": "4f8e8ccfb50fc7d76a9712e67a7454004379cbbe" | ||
} |
@@ -174,2 +174,25 @@ /* | ||
/** | ||
* Takes a hash of a message and a private key, signs the message using the SECP256k1 elliptic curve algorithm, and returns the signature components. | ||
* @param hash - The hash of the message to be signed, represented as a hexadecimal string. | ||
* @param privateKey - The private key used to sign the message, represented as a byte array. | ||
* @returns - The signature Object containing the message, messageHash, signature r, s, v | ||
*/ | ||
export const signMessageWithPrivateKey = (hash: HexString, privateKey: Bytes): SignResult => { | ||
const privateKeyUint8Array = parseAndValidatePrivateKey(privateKey); | ||
const signature = secp256k1.sign(hash.substring(2), privateKeyUint8Array); | ||
const signatureBytes = signature.toCompactRawBytes(); | ||
const r = signature.r.toString(16).padStart(64, '0'); | ||
const s = signature.s.toString(16).padStart(64, '0'); | ||
const v = signature.recovery! + 27; | ||
return { | ||
messageHash: hash, | ||
v: numberToHex(v), | ||
r: `0x${r}`, | ||
s: `0x${s}`, | ||
signature: `${bytesToHex(signatureBytes)}${v.toString(16)}`, | ||
}; | ||
}; | ||
/** | ||
* Signs arbitrary data with a given private key. | ||
@@ -197,19 +220,13 @@ * :::info | ||
export const sign = (data: string, privateKey: Bytes): SignResult => { | ||
const privateKeyUint8Array = parseAndValidatePrivateKey(privateKey); | ||
const hash = hashMessage(data); | ||
const signature = secp256k1.sign(hash.substring(2), privateKeyUint8Array); | ||
const signatureBytes = signature.toCompactRawBytes(); | ||
const r = signature.r.toString(16).padStart(64, '0'); | ||
const s = signature.s.toString(16).padStart(64, '0'); | ||
const v = signature.recovery! + 27; | ||
const { messageHash, v, r, s, signature } = signMessageWithPrivateKey(hash, privateKey); | ||
return { | ||
message: data, | ||
messageHash: hash, | ||
v: numberToHex(v), | ||
r: `0x${r}`, | ||
s: `0x${s}`, | ||
signature: `${bytesToHex(signatureBytes)}${v.toString(16)}`, | ||
messageHash, | ||
v, | ||
r, | ||
s, | ||
signature, | ||
}; | ||
@@ -216,0 +233,0 @@ }; |
Sorry, the diff of this file is not supported yet
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
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
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
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
1356499
29257