@okxweb3/coin-ethereum
Version:
An Ethereum SDK for building Web3 wallets and applications.
43 lines • 1.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Address = void 0;
const assert = require('assert');
const bytes_1 = require("./bytes");
const account_1 = require("./account");
class Address {
constructor(buf) {
assert(buf.length === 20, 'Invalid address length');
this.buf = buf;
}
static zero() {
return new Address((0, bytes_1.zeros)(20));
}
static fromString(str) {
assert((0, account_1.isValidAddress)(str), 'Invalid address');
return new Address((0, bytes_1.toBuffer)(str));
}
static fromPublicKey(pubKey) {
assert(Buffer.isBuffer(pubKey), 'Public key should be Buffer');
const buf = (0, account_1.pubToAddress)(pubKey);
return new Address(buf);
}
static fromPrivateKey(privateKey) {
assert(Buffer.isBuffer(privateKey), 'Private key should be Buffer');
const buf = (0, account_1.privateToAddress)(privateKey);
return new Address(buf);
}
equals(address) {
return this.buf.equals(address.buf);
}
isZero() {
return this.equals(Address.zero());
}
toString() {
return '0x' + this.buf.toString('hex');
}
toBuffer() {
return Buffer.from(this.buf);
}
}
exports.Address = Address;
//# sourceMappingURL=address.js.map