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

Commit 42909e7

Browse files
authored
Merge pull request #186 from ethereumjs/types
Introduce Address type
2 parents ae6515d + 37a74c1 commit 42909e7

File tree

8 files changed

+369
-1
lines changed

8 files changed

+369
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ assert.equal(new BN('dead', 16).add(new BN('101010', 2)), 57047)
3333

3434
- [account](docs/modules/_account_.md)
3535
- Private/public key and address-related functionality (creation, validation, conversion)
36+
- [address](docs/modules/_address_.md)
37+
- Address class and type
3638
- [bytes](docs/modules/_bytes_.md)
3739
- Byte-related helper and conversion functions
3840
- [constants](docs/modules/_constants_.md)

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### Modules
88

99
* ["account"](modules/_account_.md)
10+
* ["address"](modules/_address_.md)
1011
* ["bytes"](modules/_bytes_.md)
1112
* ["constants"](modules/_constants_.md)
1213
* ["externals"](modules/_externals_.md)

docs/classes/_address_.address.md

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
[ethereumjs-util](../README.md)["address"](../modules/_address_.md)[Address](_address_.address.md)
2+
3+
# Class: Address
4+
5+
## Hierarchy
6+
7+
* **Address**
8+
9+
## Index
10+
11+
### Constructors
12+
13+
* [constructor](_address_.address.md#constructor)
14+
15+
### Properties
16+
17+
* [buf](_address_.address.md#buf)
18+
19+
### Methods
20+
21+
* [isZero](_address_.address.md#iszero)
22+
* [toString](_address_.address.md#tostring)
23+
* [fromPrivateKey](_address_.address.md#static-fromprivatekey)
24+
* [fromPublicKey](_address_.address.md#static-frompublickey)
25+
* [fromString](_address_.address.md#static-fromstring)
26+
* [generate](_address_.address.md#static-generate)
27+
* [generate2](_address_.address.md#static-generate2)
28+
* [zero](_address_.address.md#static-zero)
29+
30+
## Constructors
31+
32+
### constructor
33+
34+
\+ **new Address**(`buf`: Buffer): *[Address](_address_.address.md)*
35+
36+
*Defined in [address.ts:13](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/address.ts#L13)*
37+
38+
**Parameters:**
39+
40+
Name | Type |
41+
------ | ------ |
42+
`buf` | Buffer |
43+
44+
**Returns:** *[Address](_address_.address.md)*
45+
46+
## Properties
47+
48+
### buf
49+
50+
**buf**: *Buffer*
51+
52+
*Defined in [address.ts:13](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/address.ts#L13)*
53+
54+
## Methods
55+
56+
### isZero
57+
58+
**isZero**(): *boolean*
59+
60+
*Defined in [address.ts:81](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/address.ts#L81)*
61+
62+
Is address zero.
63+
64+
**Returns:** *boolean*
65+
66+
___
67+
68+
### toString
69+
70+
**toString**(): *string*
71+
72+
*Defined in [address.ts:88](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/address.ts#L88)*
73+
74+
Returns hex encoding of address.
75+
76+
**Returns:** *string*
77+
78+
___
79+
80+
### `Static` fromPrivateKey
81+
82+
**fromPrivateKey**(`privateKey`: Buffer): *[Address](_address_.address.md)*
83+
84+
*Defined in [address.ts:50](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/address.ts#L50)*
85+
86+
Returns an address for a given private key.
87+
88+
**Parameters:**
89+
90+
Name | Type | Description |
91+
------ | ------ | ------ |
92+
`privateKey` | Buffer | A private key must be 256 bits wide |
93+
94+
**Returns:** *[Address](_address_.address.md)*
95+
96+
___
97+
98+
### `Static` fromPublicKey
99+
100+
**fromPublicKey**(`pubKey`: Buffer): *[Address](_address_.address.md)*
101+
102+
*Defined in [address.ts:40](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/address.ts#L40)*
103+
104+
Returns an address for a given public key.
105+
106+
**Parameters:**
107+
108+
Name | Type | Description |
109+
------ | ------ | ------ |
110+
`pubKey` | Buffer | The two points of an uncompressed key |
111+
112+
**Returns:** *[Address](_address_.address.md)*
113+
114+
___
115+
116+
### `Static` fromString
117+
118+
**fromString**(`str`: string): *[Address](_address_.address.md)*
119+
120+
*Defined in [address.ts:31](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/address.ts#L31)*
121+
122+
Returns an Address object from a hex-encoded string.
123+
124+
**Parameters:**
125+
126+
Name | Type | Description |
127+
------ | ------ | ------ |
128+
`str` | string | Hex-encoded address |
129+
130+
**Returns:** *[Address](_address_.address.md)*
131+
132+
___
133+
134+
### `Static` generate
135+
136+
**generate**(`from`: [Address](_address_.address.md), `nonce`: BN): *[Address](_address_.address.md)*
137+
138+
*Defined in [address.ts:61](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/address.ts#L61)*
139+
140+
Generates an address for a newly created contract.
141+
142+
**Parameters:**
143+
144+
Name | Type | Description |
145+
------ | ------ | ------ |
146+
`from` | [Address](_address_.address.md) | The address which is creating this new address |
147+
`nonce` | BN | The nonce of the from account |
148+
149+
**Returns:** *[Address](_address_.address.md)*
150+
151+
___
152+
153+
### `Static` generate2
154+
155+
**generate2**(`from`: [Address](_address_.address.md), `salt`: Buffer, `initCode`: Buffer): *[Address](_address_.address.md)*
156+
157+
*Defined in [address.ts:72](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/address.ts#L72)*
158+
159+
Generates an address for a contract created using CREATE2.
160+
161+
**Parameters:**
162+
163+
Name | Type | Description |
164+
------ | ------ | ------ |
165+
`from` | [Address](_address_.address.md) | The address which is creating this new address |
166+
`salt` | Buffer | A salt |
167+
`initCode` | Buffer | The init code of the contract being created |
168+
169+
**Returns:** *[Address](_address_.address.md)*
170+
171+
___
172+
173+
### `Static` zero
174+
175+
**zero**(): *[Address](_address_.address.md)*
176+
177+
*Defined in [address.ts:23](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/address.ts#L23)*
178+
179+
Returns the zero address.
180+
181+
**Returns:** *[Address](_address_.address.md)*

docs/modules/_address_.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[ethereumjs-util](../README.md)["address"](_address_.md)
2+
3+
# Module: "address"
4+
5+
## Index
6+
7+
### Classes
8+
9+
* [Address](../classes/_address_.address.md)

src/address.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
const assert = require('assert')
2+
import BN = require('bn.js')
3+
import { toBuffer, zeros } from './bytes'
4+
import {
5+
isValidAddress,
6+
pubToAddress,
7+
privateToAddress,
8+
generateAddress,
9+
generateAddress2,
10+
} from './account'
11+
12+
export class Address {
13+
public readonly buf: Buffer
14+
15+
constructor(buf: Buffer) {
16+
assert(buf.length === 20, 'Invalid address length')
17+
this.buf = buf
18+
}
19+
20+
/**
21+
* Returns the zero address.
22+
*/
23+
static zero(): Address {
24+
return new Address(zeros(20))
25+
}
26+
27+
/**
28+
* Returns an Address object from a hex-encoded string.
29+
* @param str - Hex-encoded address
30+
*/
31+
static fromString(str: string): Address {
32+
assert(isValidAddress(str), 'Invalid address')
33+
return new Address(toBuffer(str))
34+
}
35+
36+
/**
37+
* Returns an address for a given public key.
38+
* @param pubKey The two points of an uncompressed key
39+
*/
40+
static fromPublicKey(pubKey: Buffer): Address {
41+
assert(Buffer.isBuffer(pubKey), 'Public key should be Buffer')
42+
const buf = pubToAddress(pubKey)
43+
return new Address(buf)
44+
}
45+
46+
/**
47+
* Returns an address for a given private key.
48+
* @param privateKey A private key must be 256 bits wide
49+
*/
50+
static fromPrivateKey(privateKey: Buffer): Address {
51+
assert(Buffer.isBuffer(privateKey), 'Private key should be Buffer')
52+
const buf = privateToAddress(privateKey)
53+
return new Address(buf)
54+
}
55+
56+
/**
57+
* Generates an address for a newly created contract.
58+
* @param from The address which is creating this new address
59+
* @param nonce The nonce of the from account
60+
*/
61+
static generate(from: Address, nonce: BN): Address {
62+
assert(BN.isBN(nonce))
63+
return new Address(generateAddress(from.buf, nonce.toArrayLike(Buffer)))
64+
}
65+
66+
/**
67+
* Generates an address for a contract created using CREATE2.
68+
* @param from The address which is creating this new address
69+
* @param salt A salt
70+
* @param initCode The init code of the contract being created
71+
*/
72+
static generate2(from: Address, salt: Buffer, initCode: Buffer): Address {
73+
assert(Buffer.isBuffer(salt))
74+
assert(Buffer.isBuffer(initCode))
75+
return new Address(generateAddress2(from.buf, salt, initCode))
76+
}
77+
78+
/**
79+
* Is address zero.
80+
*/
81+
isZero(): boolean {
82+
return this.buf.equals(Address.zero().buf)
83+
}
84+
85+
/**
86+
* Returns hex encoding of address.
87+
*/
88+
toString(): string {
89+
return '0x' + this.buf.toString('hex')
90+
}
91+
}

src/bytes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ const stripZeros = function(a: any): Buffer | number[] | string {
111111
export const toBuffer = function(v: any): Buffer {
112112
if (!Buffer.isBuffer(v)) {
113113
if (Array.isArray(v) || v instanceof Uint8Array) {
114-
v = Buffer.from(v)
114+
v = Buffer.from(v as Uint8Array)
115115
} else if (typeof v === 'string') {
116116
if (ethjsUtil.isHexString(v)) {
117117
v = Buffer.from(ethjsUtil.padToEven(ethjsUtil.stripHexPrefix(v)), 'hex')

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ export * from './constants'
99
*/
1010
export * from './account'
1111

12+
/**
13+
* Address type
14+
*/
15+
export * from './address'
16+
1217
/**
1318
* Hash functions
1419
*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy