Skip to content

Commit

Permalink
feat(datastore): allow importing an argon account
Browse files Browse the repository at this point in the history
  • Loading branch information
blakebyrnes committed Oct 10, 2024
1 parent 891fc0d commit e9d5d54
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
4 changes: 1 addition & 3 deletions datastore/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,7 @@ export default class DatastoreCore extends TypedEventEmitter<{
if (this.options.localchainConfig?.localchainPath) {
this.localchain = await LocalchainWithSync.load(this.options.localchainConfig);
if (this.options.localchainConfig?.localchainCreateIfMissing) {
if (!(await this.localchain.isCreated())) {
await this.localchain.create();
}
await this.localchain.createIfMissing();
}

this.upstreamDatastorePaymentService = await this.localchain.createPaymentService(
Expand Down
41 changes: 37 additions & 4 deletions datastore/main/payments/LocalchainWithSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,45 @@ export default class LocalchainWithSync
this.afterLoad();
}

public async isCreated(): Promise<boolean> {
const accounts = await this.#localchain.accounts.list();
return accounts.length > 0;
public async bindToExportedAccount(accountJson: KeyringPair$Json, passphrase?: string): Promise<void> {
const keyring = new Keyring();
const pair = keyring.addFromJson(accountJson);
pair.decodePkcs8(passphrase);
await this.#localchain.keystore.useExternal(
pair.address,
async (address, signatureMessage) => {
let hasPair = true;
try {
keyring.getPair(address);
} catch (e) {
hasPair = false;
}
if (!hasPair) {
const accounts = await this.#localchain.accounts.list(true);
const account = accounts.find(x => x.address === address);
if (!account) {
throw new Error(`Account not found for address: ${address}`);
}
const derived = pair.derive(account.hdPath);
keyring.addPair(derived);
}
return keyring.getPair(address)?.sign(signatureMessage, { withType: true });
},
async hdPath => {
const derived = pair.derive(hdPath);
keyring.addPair(derived);
return derived.address;
},
);
}

public async create(account?: { suri?: string; cryptoScheme?: CryptoScheme }): Promise<void> {
public async createIfMissing(account?: {
suri?: string;
cryptoScheme?: CryptoScheme;
}): Promise<void> {
const accounts = await this.#localchain.accounts.list();
if (accounts.length) return;

log.info('Creating localchain', { path: Localchain.getDefaultPath() } as any);
if (account?.suri) {
const keystorePassword = this.getPassword();
Expand Down

0 comments on commit e9d5d54

Please sign in to comment.