micro-eth-signer
Advanced tools
Comparing version 0.1.1 to 0.1.2
27
index.js
@@ -53,8 +53,33 @@ "use strict"; | ||
} | ||
function normalizeField(field, value) { | ||
if (['nonce', 'gasPrice', 'gasLimit', 'value'].includes(field)) { | ||
if (typeof value === 'string') { | ||
if (value === '0x') | ||
value = ''; | ||
} | ||
else if (typeof value === 'number' || typeof value === 'bigint') { | ||
value = value.toString(16); | ||
} | ||
else { | ||
throw new TypeError('Invalid type'); | ||
} | ||
} | ||
if (field === 'gasLimit' && !value) { | ||
value = '0x5208'; | ||
} | ||
if (['nonce', 'gasPrice', 'value'].includes(field) && !value) { | ||
throw new TypeError('The field must have non-zero value'); | ||
} | ||
if (typeof value !== 'string') | ||
throw new TypeError('Invalid type'); | ||
return value; | ||
} | ||
function rawToSerialized(input) { | ||
let array = Array.isArray(input) ? input : mapToArray(input); | ||
for (let i = 0; i < array.length; i++) { | ||
const field = FIELDS[i]; | ||
const value = array[i]; | ||
const adjusted = normalizeField(field, value); | ||
if (typeof value === 'string') | ||
array[i] = add0x(value); | ||
array[i] = add0x(adjusted); | ||
} | ||
@@ -61,0 +86,0 @@ return add0x(bytesToHex(rlp.encode(array))); |
{ | ||
"name": "micro-eth-signer", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Create, sign and validate Ethereum transactions & addresses with minimum deps", | ||
@@ -22,6 +22,5 @@ "main": "index.js", | ||
"author": "Paul Miller (https://paulmillr.com)", | ||
"homepage": "https://paulmillr.com/posts/noble-secp256k1-fast-ecc/", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/paulmillr/noble-secp256k1.git" | ||
"url": "https://github.com/paulmillr/micro-eth-signer.git" | ||
}, | ||
@@ -28,0 +27,0 @@ "license": "MIT", |
@@ -5,3 +5,3 @@ # micro-eth-signer | ||
Uses three dependencies (SHA-3, RLP & secp256k1); all libraries bundled are 12KB gzipped. | ||
Library's size is 4.7KB minified. Uses three dependencies (SHA-3, RLP & secp256k1), all libraries combined are 12KB gzipped. | ||
@@ -12,21 +12,28 @@ ## Usage | ||
Supports Node.js & all major browsers. If you're looking for a fully-contained single-file version, check out Releases page on GitHub. | ||
```js | ||
import { Address, Transaction } from "micro-eth-signer"; | ||
const { Address, Transaction } = require('micro-eth-signer'); | ||
(async () => { | ||
const privateKey = "6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e"; | ||
const tx = new Transaction({ | ||
"nonce": "0x01", | ||
"gasLimit": "0x5208", | ||
"gasPrice": "0x02540be400", | ||
"to": "0xdf90dea0e0bf5ca6d2a7f0cb86874ba6714f463e", | ||
"value": "0x2386f26fc10000" | ||
to: '0xdf90dea0e0bf5ca6d2a7f0cb86874ba6714f463e', | ||
gasPrice: 100n * 10n ** 9n, // 100 gwei in wei | ||
value: 1n ** 18n, // 1 eth in wei | ||
nonce: 1 | ||
}); | ||
// Another way | ||
// tx = new Transaction('0xeb018502540be40082520894df90dea0e0bf5ca6d2a7f0cb86874ba6714f463e872386f26fc1000080808080'); | ||
// hex string or Uint8Array | ||
const privateKey = '6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e'; | ||
const signedTx = await tx.sign(privateKey); | ||
const {hash, hex} = signedTx; | ||
const addr = Address.fromPrivateKey(privateKey); | ||
const pubKey = signedTx.recoverSenderPublicKey(); | ||
const addr = Address.fromPrivateKey(privateKey); | ||
console.log('verified', Address.verifyChecksum(addr)); | ||
console.log('Verified', Address.verifyChecksum(addr)); | ||
console.log(tx); | ||
console.log("Need wei", tx.upfrontCost); | ||
console.log(signedTx.sender, signedTx.sender == addr) | ||
console.log('Need wei', tx.upfrontCost); | ||
console.log('addr is correct', signedTx.sender, signedTx.sender == addr); | ||
console.log(signedTx); | ||
@@ -64,3 +71,3 @@ })(); | ||
``` | ||
```js | ||
// raw | ||
@@ -67,0 +74,0 @@ { |
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
18429
303
117
2