Skip to content

Commit

Permalink
add publicKey member in Address (#492)
Browse files Browse the repository at this point in the history
* add public key in Address

* test publicKey is in getNextAddress call

* fix provider

* marina-provider 3.0.1
  • Loading branch information
louisinger authored Aug 17, 2023
1 parent 0c1ed5b commit 9c97a59
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"liquidjs-lib": "^6.0.2-liquid.27",
"lodash.debounce": "^4.0.8",
"lottie-web": "^5.7.8",
"marina-provider": "^2.0.0",
"marina-provider": "^3.0.1",
"moment": "^2.29.4",
"path-browserify": "^1.0.1",
"postcss": "^7.0.35",
Expand Down
9 changes: 9 additions & 0 deletions playwright-tests/injected-script.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ pwTest(
pwExpect(page.getByText('Wrong network, switch to the Testnet')).toBeTruthy();
const provider = new PlaywrightMarinaProvider(page);
pwExpect(await provider.isEnabled()).toBe(true);

const address = await provider.getNextAddress();
pwExpect(address).toBeTruthy();
pwExpect(address.confidentialAddress).toBeTruthy();
pwExpect(address.blindingPrivateKey).toBeTruthy();
pwExpect(address.publicKey).toBeTruthy();
pwExpect(address.script).toBeTruthy();
}
);

Expand All @@ -47,6 +54,7 @@ pwTest(
await popup.getByRole('button', { name: 'Connect' }).click();
}
const toFaucet = await provider.getNextAddress();
if (!toFaucet.confidentialAddress) throw new Error('confidentialAddress is undefined');
await faucet(toFaucet.confidentialAddress, 1); // send 1 L-BTC to the address
await page.goto(marinaURL(extensionId, 'popup.html'));
await page.waitForSelector('text=1 L-BTC');
Expand Down Expand Up @@ -116,6 +124,7 @@ pwTest(
await popup.getByRole('button', { name: 'Connect' }).click();
}
const toFaucet = await provider.getNextAddress();
if (!toFaucet.confidentialAddress) throw new Error('confidentialAddress is undefined');
await faucet(toFaucet.confidentialAddress, 1); // send 1 L-BTC to the address
await page.goto(marinaURL(extensionId, 'popup.html'));
await page.waitForSelector('text=1 L-BTC');
Expand Down
4 changes: 4 additions & 0 deletions playwright-tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ function bufferCast<T extends Record<string, any>>(obj: T): T {
// implements only the methods that are used in the tests
export class PlaywrightMarinaProvider implements MarinaProvider {
constructor(private page: Page) {}

importScript(accountName: string, scriptHex: string, blindingPrivateKey?: string | undefined): Promise<void> {
throw new Error('Method not implemented.');
}

enable(): Promise<void> {
return this.page.evaluate(
Expand Down
20 changes: 17 additions & 3 deletions src/application/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ export class Account {
case AccountType.P2WPKH: {
return Object.entries(scripts).map(([script, details]) => ({
confidentialAddress: this.createP2WPKHAddress(Buffer.from(script, 'hex')),
publicKey: details.derivationPath
? this.node
.derivePath(details.derivationPath.replace('m/', ''))
.publicKey.toString('hex')
: '',
script,
...details,
}));
}
Expand All @@ -160,6 +166,12 @@ export class Account {
return Object.entries(scripts).map(([script, details]) => ({
confidentialAddress: this.createTaprootAddress(Buffer.from(script, 'hex')),
...details,
script,
publicKey: details.derivationPath
? this.node
.derivePath(details.derivationPath.replace('m/', ''))
.publicKey.toString('hex')
: '',
contract: isIonioScriptDetails(details)
? new Contract(details.artifact, details.params, this.network, zkp)
: undefined,
Expand All @@ -181,7 +193,7 @@ export class Account {

const nextIndexes = await this.getNextIndexes();
const next = isInternal ? nextIndexes.internal : nextIndexes.external;
const publicKeys = this.deriveBatchPublicKeys(next, next + 1, isInternal);
const keyPair = this.deriveBatchPublicKeys(next, next + 1, isInternal)[0];
const type = await this.getAccountType();

let confidentialAddress = undefined;
Expand All @@ -190,14 +202,14 @@ export class Account {

switch (type) {
case AccountType.P2WPKH:
[script, scriptDetails] = this.createP2PWKHScript(publicKeys[0]);
[script, scriptDetails] = this.createP2PWKHScript(keyPair);
confidentialAddress = this.createP2WPKHAddress(Buffer.from(script, 'hex'));
break;
case AccountType.Ionio:
if (!artifactWithArgs)
throw new Error('Artifact with args is required for Ionio account type');
[script, scriptDetails] = this.createTaprootScript(
publicKeys[0],
keyPair,
artifactWithArgs,
await ZKPLib()
);
Expand All @@ -221,6 +233,8 @@ export class Account {
]);

return {
publicKey: keyPair.publicKey.toString('hex'),
script,
confidentialAddress,
...scriptDetails,
contract: isIonioScriptDetails(scriptDetails)
Expand Down
11 changes: 7 additions & 4 deletions src/domain/pset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,18 @@ export class PsetBuilder {
const accountFactory = await AccountFactory.create(this.walletRepository);
const accountName = network === 'liquid' ? MainAccount : MainAccountTest;
const mainAccount = await accountFactory.make(network, accountName);
const changeAddress = await mainAccount.getNextAddress(true);
const changeAddress = (await mainAccount.getNextAddress(true)).confidentialAddress;
if (!changeAddress) {
throw new Error('change address not found');
}

updater.addOutputs(
coinSelection.changeOutputs.map((change) => ({
amount: change.amount,
asset: change.asset,
script: address.toOutputScript(changeAddress.confidentialAddress),
script: address.toOutputScript(changeAddress),
blinderIndex: 0,
blindingPublicKey: address.fromConfidential(changeAddress.confidentialAddress)
.blindingKey,
blindingPublicKey: address.fromConfidential(changeAddress).blindingKey,
}))
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/extension/wallet/receive/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const ReceiveView: React.FC = () => {
const accountFactory = await AccountFactory.create(walletRepository);
const mainAccount = await accountFactory.make(network, accountName);
const addr = await mainAccount.getNextAddress(false);
setConfidentialAddress(addr.confidentialAddress);
if (addr.confidentialAddress) setConfidentialAddress(addr.confidentialAddress);
else console.warn('something went wrong while generating address: ', JSON.stringify(addr));
})().catch(console.error);
}, []);

Expand Down
4 changes: 4 additions & 0 deletions src/inject/marina/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export default class Marina extends WindowProxy<keyof MarinaProvider> implements
this.eventHandler = new MarinaEventHandler();
}

importScript(_: string, __: string, ___?: string | undefined): Promise<void> {
throw new Error('Method not implemented.');
}

createAccount(accountID: string, accountType: AccountType): Promise<void> {
return this.proxy('createAccount', [accountID, accountType]);
}
Expand Down
7 changes: 7 additions & 0 deletions test/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ describe('Application Layer', () => {
.nextKeyIndexes['regtest'].external;
const address = await account.getNextAddress(false);
expect(address).toBeDefined();
if (!address.confidentialAddress) throw new Error('Address not generated');
const scriptFromAddress = toOutputScript(address.confidentialAddress).toString('hex');
const scripts = Object.keys(
await walletRepository.getAccountScripts('regtest', MainAccountTest)
Expand All @@ -110,6 +111,7 @@ describe('Application Layer', () => {
.nextKeyIndexes['regtest'].internal;
const address = await account.getNextAddress(true);
expect(address).toBeDefined();
if (!address.confidentialAddress) throw new Error('Address not generated');
const scriptFromAddress = toOutputScript(address.confidentialAddress).toString('hex');
const scripts = Object.keys(
await walletRepository.getAccountScripts('regtest', MainAccountTest)
Expand Down Expand Up @@ -182,12 +184,15 @@ describe('Application Layer', () => {
// generate and faucet addresses
let account = await factory.make('regtest', randomAccountName);
const address = await account.getNextAddress(false);
if (!address.confidentialAddress) throw new Error('Address not generated');
const txid0 = await faucet(address.confidentialAddress, 1);
const txid1 = await faucet(address.confidentialAddress, 1);
const addressBis = await account.getNextAddress(false);
if (!addressBis.confidentialAddress) throw new Error('Address not generated');
const txid2 = await faucet(addressBis.confidentialAddress, 1);
const txid3 = await faucet(addressBis.confidentialAddress, 1);
const changeAddress = await account.getNextAddress(true);
if (!changeAddress.confidentialAddress) throw new Error('Address not generated');
const txid4 = await faucet(changeAddress.confidentialAddress, 1);
const txid5 = await faucet(changeAddress.confidentialAddress, 1);
await sleep(5000); // wait for the txs to be confirmed
Expand Down Expand Up @@ -293,6 +298,7 @@ describe('Application Layer', () => {
// faucet it
const account = await factory.make('regtest', accountName);
const address = await account.getNextAddress(false);
if (!address.confidentialAddress) throw new Error('Address not generated');
await faucet(address.confidentialAddress, 1);
await faucet(address.confidentialAddress, 1);

Expand All @@ -305,6 +311,7 @@ describe('Application Layer', () => {
]),
args: { sum: 10 },
});
if (!captchaAddress.confidentialAddress) throw new Error('Address not generated');
await faucet(captchaAddress.confidentialAddress, 1);
await sleep(5000); // wait for the txs to be confirmed
const chainSource = await appRepository.getChainSource('regtest');
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5609,10 +5609,10 @@ map-age-cleaner@^0.1.3:
dependencies:
p-defer "^1.0.0"

marina-provider@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/marina-provider/-/marina-provider-2.0.0.tgz#f684998e2c64be88c8f4674d2ae4d9887ce566d0"
integrity sha512-C/TToZV5tYXsGeUe8ux6+gE+g+ihcdXfxj1VoDo3S4fhdvjwMKvNLzUEmpZJHyJ/rMcbGUHVagKR1MPsueE3Ig==
marina-provider@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/marina-provider/-/marina-provider-3.0.1.tgz#a6d3b7ead8a2a06b9ea48ce75e9605f39a20a3a6"
integrity sha512-EWHn0sUDhf0/1gediCT+xByghfS7ISs5XVUNn1ZK1KN7iky+9jfl9SraMvbC/3A9BQNgNOhji3yFMBrv5k4tow==

marky@^1.2.2:
version "1.2.5"
Expand Down

0 comments on commit 9c97a59

Please sign in to comment.