Skip to content
This repository has been archived by the owner on Jun 17, 2021. It is now read-only.

Introduce Address type #186

Merged
merged 5 commits into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ assert.equal(new BN('dead', 16).add(new BN('101010', 2)), 57047)

- [account](docs/modules/_account_.md)
- Private/public key and address-related functionality (creation, validation, conversion)
- [address](docs/modules/_address_.md)
- Address class and type
- [bytes](docs/modules/_bytes_.md)
- Byte-related helper and conversion functions
- [constants](docs/modules/_constants_.md)
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Modules

* ["account"](modules/_account_.md)
* ["address"](modules/_address_.md)
* ["bytes"](modules/_bytes_.md)
* ["constants"](modules/_constants_.md)
* ["externals"](modules/_externals_.md)
Expand Down
181 changes: 181 additions & 0 deletions docs/classes/_address_.address.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
[ethereumjs-util](../README.md) › ["address"](../modules/_address_.md) › [Address](_address_.address.md)

# Class: Address

## Hierarchy

* **Address**

## Index

### Constructors

* [constructor](_address_.address.md#constructor)

### Properties

* [buf](_address_.address.md#buf)

### Methods

* [isZero](_address_.address.md#iszero)
* [toString](_address_.address.md#tostring)
* [fromPrivateKey](_address_.address.md#static-fromprivatekey)
* [fromPublicKey](_address_.address.md#static-frompublickey)
* [fromString](_address_.address.md#static-fromstring)
* [generate](_address_.address.md#static-generate)
* [generate2](_address_.address.md#static-generate2)
* [zero](_address_.address.md#static-zero)

## Constructors

### constructor

\+ **new Address**(`buf`: Buffer): *[Address](_address_.address.md)*

*Defined in [address.ts:13](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/address.ts#L13)*

**Parameters:**

Name | Type |
------ | ------ |
`buf` | Buffer |

**Returns:** *[Address](_address_.address.md)*

## Properties

### buf

• **buf**: *Buffer*

*Defined in [address.ts:13](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/address.ts#L13)*

## Methods

### isZero

▸ **isZero**(): *boolean*

*Defined in [address.ts:81](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/address.ts#L81)*

Is address zero.

**Returns:** *boolean*

___

### toString

▸ **toString**(): *string*

*Defined in [address.ts:88](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/address.ts#L88)*

Returns hex encoding of address.

**Returns:** *string*

___

### `Static` fromPrivateKey

▸ **fromPrivateKey**(`privateKey`: Buffer): *[Address](_address_.address.md)*

*Defined in [address.ts:50](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/address.ts#L50)*

Returns an address for a given private key.

**Parameters:**

Name | Type | Description |
------ | ------ | ------ |
`privateKey` | Buffer | A private key must be 256 bits wide |

**Returns:** *[Address](_address_.address.md)*

___

### `Static` fromPublicKey

▸ **fromPublicKey**(`pubKey`: Buffer): *[Address](_address_.address.md)*

*Defined in [address.ts:40](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/address.ts#L40)*

Returns an address for a given public key.

**Parameters:**

Name | Type | Description |
------ | ------ | ------ |
`pubKey` | Buffer | The two points of an uncompressed key |

**Returns:** *[Address](_address_.address.md)*

___

### `Static` fromString

▸ **fromString**(`str`: string): *[Address](_address_.address.md)*

*Defined in [address.ts:31](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/address.ts#L31)*

Returns an Address object from a hex-encoded string.

**Parameters:**

Name | Type | Description |
------ | ------ | ------ |
`str` | string | Hex-encoded address |

**Returns:** *[Address](_address_.address.md)*

___

### `Static` generate

▸ **generate**(`from`: [Address](_address_.address.md), `nonce`: BN): *[Address](_address_.address.md)*

*Defined in [address.ts:61](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/address.ts#L61)*

Generates an address for a newly created contract.

**Parameters:**

Name | Type | Description |
------ | ------ | ------ |
`from` | [Address](_address_.address.md) | The address which is creating this new address |
`nonce` | BN | The nonce of the from account |

**Returns:** *[Address](_address_.address.md)*

___

### `Static` generate2

▸ **generate2**(`from`: [Address](_address_.address.md), `salt`: Buffer, `initCode`: Buffer): *[Address](_address_.address.md)*

*Defined in [address.ts:72](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/address.ts#L72)*

Generates an address for a contract created using CREATE2.

**Parameters:**

Name | Type | Description |
------ | ------ | ------ |
`from` | [Address](_address_.address.md) | The address which is creating this new address |
`salt` | Buffer | A salt |
`initCode` | Buffer | The init code of the contract being created |

**Returns:** *[Address](_address_.address.md)*

___

### `Static` zero

▸ **zero**(): *[Address](_address_.address.md)*

*Defined in [address.ts:23](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/address.ts#L23)*

Returns the zero address.

**Returns:** *[Address](_address_.address.md)*
9 changes: 9 additions & 0 deletions docs/modules/_address_.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[ethereumjs-util](../README.md) › ["address"](_address_.md)

# Module: "address"

## Index

### Classes

* [Address](../classes/_address_.address.md)
91 changes: 91 additions & 0 deletions src/address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const assert = require('assert')
import BN = require('bn.js')
import { toBuffer, zeros } from './bytes'
import {
isValidAddress,
pubToAddress,
privateToAddress,
generateAddress,
generateAddress2,
} from './account'

export class Address {
public readonly buf: Buffer

constructor(buf: Buffer) {
assert(buf.length === 20, 'Invalid address length')
this.buf = buf
}

/**
* Returns the zero address.
*/
static zero(): Address {
return new Address(zeros(20))
}

/**
* Returns an Address object from a hex-encoded string.
* @param str - Hex-encoded address
*/
static fromString(str: string): Address {
assert(isValidAddress(str), 'Invalid address')
return new Address(toBuffer(str))
}

/**
* Returns an address for a given public key.
* @param pubKey The two points of an uncompressed key
*/
static fromPublicKey(pubKey: Buffer): Address {
assert(Buffer.isBuffer(pubKey), 'Public key should be Buffer')
const buf = pubToAddress(pubKey)
return new Address(buf)
}

/**
* Returns an address for a given private key.
* @param privateKey A private key must be 256 bits wide
*/
static fromPrivateKey(privateKey: Buffer): Address {
assert(Buffer.isBuffer(privateKey), 'Private key should be Buffer')
const buf = privateToAddress(privateKey)
return new Address(buf)
}

/**
* Generates an address for a newly created contract.
* @param from The address which is creating this new address
* @param nonce The nonce of the from account
*/
static generate(from: Address, nonce: BN): Address {
assert(BN.isBN(nonce))
return new Address(generateAddress(from.buf, nonce.toArrayLike(Buffer)))
}

/**
* Generates an address for a contract created using CREATE2.
* @param from The address which is creating this new address
* @param salt A salt
* @param initCode The init code of the contract being created
*/
static generate2(from: Address, salt: Buffer, initCode: Buffer): Address {
assert(Buffer.isBuffer(salt))
assert(Buffer.isBuffer(initCode))
return new Address(generateAddress2(from.buf, salt, initCode))
}

/**
* Is address zero.
*/
isZero(): boolean {
return this.buf.equals(Address.zero().buf)
}

/**
* Returns hex encoding of address.
*/
toString(): string {
return '0x' + this.buf.toString('hex')
}
}
2 changes: 1 addition & 1 deletion src/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const stripZeros = function(a: any): Buffer | number[] | string {
export const toBuffer = function(v: any): Buffer {
if (!Buffer.isBuffer(v)) {
if (Array.isArray(v) || v instanceof Uint8Array) {
v = Buffer.from(v)
v = Buffer.from(v as Uint8Array)
} else if (typeof v === 'string') {
if (ethjsUtil.isHexString(v)) {
v = Buffer.from(ethjsUtil.padToEven(ethjsUtil.stripHexPrefix(v)), 'hex')
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export * from './constants'
*/
export * from './account'

/**
* Address type
*/
export * from './address'

/**
* Hash functions
*/
Expand Down
Loading