Skip to content

Commit 99b516b

Browse files
Added ability to get private key for WalletAddress and updated yarn.lock file (#213)
* Added ability to get private key for WalletAddress and updated yarn.lock file * Add semicolon * update * Refactored tests * lint * address feedback * Changes
1 parent f68ea83 commit 99b516b

File tree

4 files changed

+103
-108
lines changed

4 files changed

+103
-108
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
### Added
66

77
- Add support for list address transactions.
8+
- Add support for exporting the private key of a `WalletAddress`
89

910
## [0.2.0]
1011

1112
### Added
1213

1314
- USDC Faucet support on Base Sepolia
14-
- Improved error mesasges for `InternalError`
15+
- Improved error messages for `InternalError`
1516

1617
## [0.1.1] - 2024-08-27
1718

src/coinbase/address/wallet_address.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ export class WalletAddress extends Address {
7474
this.key = key;
7575
}
7676

77+
/**
78+
* Exports the Address's private key to a hex string.
79+
*
80+
* @returns The Address's private key as a hex string.
81+
*/
82+
public export() {
83+
if (this.key === undefined) {
84+
throw new Error("Private key is not set");
85+
}
86+
87+
return this.key.privateKey;
88+
}
89+
7790
/**
7891
* Returns whether the Address has a private key backing it to sign transactions.
7992
*

src/tests/wallet_address_test.ts

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,38 @@ describe("WalletAddress", () => {
260260
);
261261
});
262262

263+
describe("#setKey", () => {
264+
it("should set the key successfully", () => {
265+
key = ethers.Wallet.createRandom();
266+
const newAddress = new WalletAddress(VALID_ADDRESS_MODEL, undefined);
267+
expect(() => {
268+
newAddress.setKey(key);
269+
}).not.toThrow(Error);
270+
});
271+
it("should not set the key successfully", () => {
272+
key = ethers.Wallet.createRandom();
273+
const newAddress = new WalletAddress(VALID_ADDRESS_MODEL, key);
274+
expect(() => {
275+
newAddress.setKey(key);
276+
}).toThrow(Error);
277+
});
278+
});
279+
280+
describe("#export", () => {
281+
it("should get the private key if it is set", () => {
282+
key = ethers.Wallet.createRandom();
283+
const newAddress = new WalletAddress(VALID_ADDRESS_MODEL, key);
284+
expect(newAddress.export()).toEqual(key.privateKey);
285+
});
286+
287+
it("should not get the private key if not set", () => {
288+
const newAddress = new WalletAddress(VALID_ADDRESS_MODEL, undefined);
289+
expect(() => {
290+
newAddress.export();
291+
}).toThrow(Error);
292+
});
293+
});
294+
263295
describe("#stakingOperation", () => {
264296
key = ethers.Wallet.createRandom();
265297
const newAddress = newAddressModel("", randomUUID(), Coinbase.networks.EthereumHolesky);
@@ -1071,23 +1103,6 @@ describe("WalletAddress", () => {
10711103
).rejects.toThrow(Error);
10721104
});
10731105
});
1074-
1075-
describe("#setKey", () => {
1076-
it("should set the key successfully", () => {
1077-
key = ethers.Wallet.createRandom();
1078-
const newAddress = new WalletAddress(VALID_ADDRESS_MODEL, undefined);
1079-
expect(() => {
1080-
newAddress.setKey(key);
1081-
}).not.toThrow(Error);
1082-
});
1083-
it("should not set the key successfully", () => {
1084-
key = ethers.Wallet.createRandom();
1085-
const newAddress = new WalletAddress(VALID_ADDRESS_MODEL, key);
1086-
expect(() => {
1087-
newAddress.setKey(key);
1088-
}).toThrow(Error);
1089-
});
1090-
});
10911106
});
10921107

10931108
describe("#createPayloadSignature", () => {

0 commit comments

Comments
 (0)