Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create a wallet without a provider #1566

Merged
Show file tree
Hide file tree
Changes from 52 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
75e1036
test: added test for setting Account.provider field
petertonysmith94 Dec 19, 2023
0acd706
test: added distinction to test, with a provider
petertonysmith94 Dec 19, 2023
c901a66
test: added Account without a provider tests
petertonysmith94 Dec 19, 2023
36bb89c
feat: made provider optional on the Account class
petertonysmith94 Dec 19, 2023
067317b
feat: removed redundant provider variable on BaseWalletUnlocked (expo…
petertonysmith94 Dec 19, 2023
4a3c6ed
feat: made provider variable protected
petertonysmith94 Dec 19, 2023
8bbd0a1
chore: added missing typedocs for `fromEncryptedJson` method
petertonysmith94 Dec 19, 2023
0d0419d
feat: made BaseWalletUnlocked provider argument optional
petertonysmith94 Dec 19, 2023
8f3c09e
chore: fixed the order of the typedocs (Wallet.fromSeed)
petertonysmith94 Dec 19, 2023
348b045
chore: fixed the order of the typedocs (Wallet.fromMnemonic)
petertonysmith94 Dec 19, 2023
c97879b
test: removed duplicate test
petertonysmith94 Dec 19, 2023
6a683b4
test: added clearer test intentions
petertonysmith94 Dec 19, 2023
49a6ae0
feat: make provider argument optional on Wallet.fromAddress
petertonysmith94 Dec 19, 2023
28d740a
feat: make provider argument optional on Wallet.fromPrivateKey
petertonysmith94 Dec 19, 2023
bdcf761
chore: updated typedocs on Account class - provider optional
petertonysmith94 Dec 19, 2023
1d006d7
feat: make arguments optional on Wallet.generate
petertonysmith94 Dec 19, 2023
ce42fa0
test: tiding up the wallet tests
petertonysmith94 Jan 1, 2024
1fa96c9
test: missing Wallet instantiate from generate
petertonysmith94 Jan 1, 2024
38aa397
fix: failing test - incorrect instance
petertonysmith94 Jan 4, 2024
4fa4b13
test: organising tests by function name
petertonysmith94 Jan 4, 2024
3970303
chore: added todo for question
petertonysmith94 Jan 4, 2024
1fd7411
test: added describe block around tests
petertonysmith94 Jan 4, 2024
28fe1d7
test: fromSeed without provider
petertonysmith94 Jan 4, 2024
8923259
feat: fromMnemonic optional provider
petertonysmith94 Jan 4, 2024
8e22bd2
test: fromExtendedKey provider optional
petertonysmith94 Jan 4, 2024
ccdd425
test: fixing incorrect instance type
petertonysmith94 Jan 4, 2024
67c7cf1
feat: made fromEncryptedJson provider argument optional
petertonysmith94 Jan 4, 2024
e076212
test: fixed name of test
petertonysmith94 Jan 4, 2024
291a4e1
test: added expects for provider error
petertonysmith94 Jan 4, 2024
922b762
test: describe the underlying class
petertonysmith94 Jan 4, 2024
dc9ae02
test: make test a little more organised
petertonysmith94 Jan 4, 2024
01b0e6f
Merge branch 'master' into ps/feat/allow-wallet-creation-without-prov…
petertonysmith94 Jan 4, 2024
79417e3
chore: added changeset
petertonysmith94 Jan 4, 2024
2058680
fix: pass correct provider reference
petertonysmith94 Jan 4, 2024
3e2d016
fix: removed @todo
petertonysmith94 Jan 4, 2024
2fa228b
test: added missing provider argument
petertonysmith94 Jan 4, 2024
81890e6
Merge branch 'master' into ps/feat/allow-wallet-creation-without-prov…
danielbate Jan 5, 2024
5823bd0
Merge branch 'master' into ps/feat/allow-wallet-creation-without-prov…
Torres-ssf Jan 9, 2024
7ca4b73
fix: testing a fix for pipeline adding github token
petertonysmith94 Jan 10, 2024
70448a6
Merge branch 'rc/salamander' into ps/feat/allow-wallet-creation-witho…
arboleya Jan 11, 2024
f1d2b04
Merge branch 'rc/salamander' into ps/feat/allow-wallet-creation-witho…
arboleya Jan 12, 2024
82d56d6
Revert " fix: testing a fix for pipeline adding github token"
petertonysmith94 Jan 12, 2024
09d7cb5
Merge branch 'rc/salamander' into ps/feat/allow-wallet-creation-witho…
danielbate Jan 12, 2024
d7b4f83
chore: added changeset
petertonysmith94 Jan 12, 2024
ee12bca
Merge branch 'rc/salamander' into ps/feat/allow-wallet-creation-witho…
arboleya Jan 15, 2024
dc980a2
Merge branch 'rc/salamander' into ps/feat/allow-wallet-creation-witho…
Torres-ssf Jan 17, 2024
6b80ac4
Merge branch 'rc/salamander' into ps/feat/allow-wallet-creation-witho…
Torres-ssf Jan 17, 2024
25e4178
Merge branch 'rc/salamander' into ps/feat/allow-wallet-creation-witho…
Torres-ssf Jan 17, 2024
3fa4c9d
Merge branch 'rc/salamander' into ps/feat/allow-wallet-creation-witho…
Torres-ssf Jan 17, 2024
e6519ce
Merge branch 'rc/salamander' into ps/feat/allow-wallet-creation-witho…
arboleya Jan 17, 2024
ede3baa
Merge branch 'rc/salamander' into ps/feat/allow-wallet-creation-witho…
Torres-ssf Jan 18, 2024
b72afbc
Merge branch 'rc/salamander' into ps/feat/allow-wallet-creation-witho…
danielbate Jan 18, 2024
10fd09f
docs: added optional provider documentation
petertonysmith94 Jan 18, 2024
4b806d9
chore: removed unnecessary changeset
petertonysmith94 Jan 18, 2024
f1f6b54
chore: removed unnecessary import
petertonysmith94 Jan 18, 2024
32dd3b6
docs: thinking about context of docs
petertonysmith94 Jan 18, 2024
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
5 changes: 5 additions & 0 deletions .changeset/happy-days-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/wallet": minor
---

Made provider argument optional for wallet instantiation
2 changes: 2 additions & 0 deletions .changeset/witty-beds-vanish.md
petertonysmith94 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
petertonysmith94 marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion packages/program/src/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('Contract', () => {
// but without reference to the BaseWalletLocked class
const BaseWalletLockedCustom = Object.assign(Account);
expect(BaseWalletLockedCustom).not.toBeInstanceOf(Account);
const wallet = new BaseWalletLockedCustom(generatedWallet.address);
const wallet = new BaseWalletLockedCustom(generatedWallet.address, generatedWallet.provider);
const contract = new Contract(CONTRACT_ID, ABI, wallet);
expect(contract.provider).toBe(wallet.provider);
expect(contract.account).toBe(wallet);
Expand Down
33 changes: 32 additions & 1 deletion packages/wallet/src/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Account', () => {
'0x0000000000000000000000000000000000000000000000000000000000000000',
];

it('Create wallet using a address', () => {
it('should create account using an address, with a provider', () => {
const account = new Account(
'0x09c0b2d1a486c439a87bcba6b46a7a1a23f3897cc83a94521a96da5c23bc58db',
provider
Expand All @@ -54,6 +54,22 @@ describe('Account', () => {
);
});

it('should create account using an address, without a provider', () => {
const account = new Account(
'0x09c0b2d1a486c439a87bcba6b46a7a1a23f3897cc83a94521a96da5c23bc58db'
);
expect(account.address.toB256()).toEqual(
'0x09c0b2d1a486c439a87bcba6b46a7a1a23f3897cc83a94521a96da5c23bc58db'
);
});

it('should throw an error when using a provider dependent method, without a provider', async () => {
const account = new Account(
'0x09c0b2d1a486c439a87bcba6b46a7a1a23f3897cc83a94521a96da5c23bc58db'
);
await expect(() => account.getBalance()).rejects.toThrow(/Provider not set/);
});

it('should get coins just fine', async () => {
const account = new Account(
'0x09c0b2d1a486c439a87bcba6b46a7a1a23f3897cc83a94521a96da5c23bc58db',
Expand Down Expand Up @@ -223,6 +239,21 @@ describe('Account', () => {
expect(account.provider).not.toBe(provider);
});

it('should be able to set a provider', async () => {
const account = new Account(
'0x09c0b2d1a486c439a87bcba6b46a7a1a23f3897cc83a94521a96da5c23bc58db',
provider
);
const newProviderInstance = await Provider.create(FUEL_NETWORK_URL);

expect(account.provider).not.toBe(newProviderInstance);

account.provider = newProviderInstance;

expect(account.provider).toBe(newProviderInstance);
expect(account.provider).not.toBe(provider);
});

it('should execute fund just as fine', async () => {
const quantities: CoinQuantity[] = [
{
Expand Down
34 changes: 29 additions & 5 deletions packages/wallet/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,52 @@ export class Account extends AbstractAccount {
/**
* The provider used to interact with the network.
*/
provider: Provider;
protected _provider?: Provider;

/**
* Creates a new Account instance.
*
* @param address - The address of the account.
* @param provider - A Provider instance.
* @param provider - A Provider instance (optional).
*/
constructor(address: string | AbstractAddress, provider: Provider) {
constructor(address: string | AbstractAddress, provider?: Provider) {
super();
this.provider = provider;
this._provider = provider;
this.address = Address.fromDynamicInput(address);
}

/**
* The provider used to interact with the network.
*
* @returns A Provider instance.
*
* @throws `FuelError` if the provider is not set.
*/
get provider(): Provider {
if (!this._provider) {
throw new FuelError(ErrorCode.MISSING_PROVIDER, 'Provider not set');
}

return this._provider;
}

/**
* Sets the provider for the account.
*
* @param provider - A Provider instance.
*/
set provider(provider: Provider) {
this._provider = provider;
}

/**
* Changes the provider connection for the account.
*
* @param provider - A Provider instance.
* @returns The updated Provider instance.
*/
connect(provider: Provider): Provider {
this.provider = provider;
this._provider = provider;
return this.provider;
}

Expand Down
10 changes: 2 additions & 8 deletions packages/wallet/src/base-unlocked-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ export class BaseWalletUnlocked extends Account {
*/
static defaultPath = "m/44'/1179993420'/0'/0/0";

/**
* The provider used to interact with the Fuel network.
*/
provider: Provider;

/**
* A function that returns the wallet's signer.
*/
Expand All @@ -36,13 +31,12 @@ export class BaseWalletUnlocked extends Account {
* Creates a new BaseWalletUnlocked instance.
*
* @param privateKey - The private key of the wallet.
* @param provider - A Provider instance.
* @param provider - A Provider instance (optional).
*/
constructor(privateKey: BytesLike, provider: Provider) {
constructor(privateKey: BytesLike, provider?: Provider) {
const signer = new Signer(privateKey);
super(signer.address, provider);
this.signer = () => signer;
this.provider = provider;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/wallet/src/types/GenerateOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import type { BytesLike } from 'ethers';
export interface GenerateOptions {
/** Additional entropy for the random bytes */
entropy?: BytesLike;
provider: Provider;
provider?: Provider;
}
89 changes: 58 additions & 31 deletions packages/wallet/src/wallet-unlocked.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,48 +150,75 @@ describe('WalletUnlocked', () => {
expect(wallet.address).toEqual(recoveredAddress);
});

it('Create wallet from seed', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const wallet = WalletUnlocked.fromSeed(walletSpec.seed, provider, walletSpec.account_1.path);
describe('WalletUnlocked.fromSeed', () => {
it('Create wallet from seed', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const wallet = WalletUnlocked.fromSeed(walletSpec.seed, provider, walletSpec.account_1.path);

expect(wallet.publicKey).toBe(walletSpec.account_1.publicKey);
expect(wallet.provider.url).toBe(walletSpec.providerUrl);
});
expect(wallet.publicKey).toBe(walletSpec.account_1.publicKey);
expect(wallet.provider.url).toBe(walletSpec.providerUrl);
});

it('Create wallet from mnemonic', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const wallet = WalletUnlocked.fromMnemonic(
walletSpec.mnemonic,
provider,
walletSpec.account_1.path,
undefined
);
it('Create wallet from seed with default path', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const wallet = WalletUnlocked.fromSeed(walletSpec.seed, provider);

expect(wallet.publicKey).toBe(walletSpec.account_1.publicKey);
expect(wallet.provider.url).toBe(walletSpec.providerUrl);
});
expect(wallet.publicKey).toBe(walletSpec.account_0.publicKey);
expect(wallet.provider.url).toBe(walletSpec.providerUrl);
});

it('Create wallet from mnemonic with default path', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const wallet = WalletUnlocked.fromMnemonic(walletSpec.mnemonic, provider);
it('Create wallet from seed with default path, without a provider', () => {
const wallet = WalletUnlocked.fromSeed(walletSpec.seed);

expect(wallet.publicKey).toBe(walletSpec.account_0.publicKey);
expect(wallet.publicKey).toBe(walletSpec.account_0.publicKey);
expect(() => wallet.provider).toThrowError('Provider not set');
});
});

it('Create wallet from extendedKey', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const wallet = WalletUnlocked.fromExtendedKey(walletSpec.account_0.xprv, provider);
describe('WalletUnlocked.fromMnemonic', () => {
it('Create wallet from mnemonic', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const wallet = WalletUnlocked.fromMnemonic(
walletSpec.mnemonic,
provider,
walletSpec.account_1.path,
undefined
);

expect(wallet.publicKey).toBe(walletSpec.account_1.publicKey);
expect(wallet.provider.url).toBe(walletSpec.providerUrl);
});

it('Create wallet from mnemonic with default path', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const wallet = WalletUnlocked.fromMnemonic(walletSpec.mnemonic, provider);

expect(wallet.publicKey).toBe(walletSpec.account_0.publicKey);
});

it('Create wallet from mnemonic with default path, without a provider', () => {
const wallet = WalletUnlocked.fromMnemonic(walletSpec.mnemonic);

expect(wallet.publicKey).toBe(walletSpec.account_0.publicKey);
expect(wallet.provider.url).toBe(walletSpec.providerUrl);
expect(wallet.publicKey).toBe(walletSpec.account_0.publicKey);
expect(() => wallet.provider).toThrowError('Provider not set');
});
});

it('Create wallet from seed with default path', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const wallet = WalletUnlocked.fromSeed(walletSpec.seed, provider);
describe('WalletUnlocked.extendedKey', () => {
it('Create wallet from extendedKey', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const wallet = WalletUnlocked.fromExtendedKey(walletSpec.account_0.xprv, provider);

expect(wallet.publicKey).toBe(walletSpec.account_0.publicKey);
expect(wallet.provider.url).toBe(walletSpec.providerUrl);
});

expect(wallet.publicKey).toBe(walletSpec.account_0.publicKey);
expect(wallet.provider.url).toBe(walletSpec.providerUrl);
it('Create wallet from extendedKey, without provider', () => {
const wallet = WalletUnlocked.fromExtendedKey(walletSpec.account_0.xprv);

expect(wallet.publicKey).toBe(walletSpec.account_0.publicKey);
expect(() => wallet.provider).toThrowError('Provider not set');
});
});

it('Create wallet and lock it', async () => {
Expand Down
Loading
Loading