Skip to content

Commit

Permalink
Merge pull request #599 from input-output-hk/fix/cip30-addresses-as-c…
Browse files Browse the repository at this point in the history
…bor-instead-of-bech32

fix/cip30 addresses as cbor instead of bech32
  • Loading branch information
mkazlauskas committed Feb 10, 2023
2 parents 67a0c90 + cae6081 commit 99a5717
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"files": {
"main.css": "/static/css/main.3b75acbb.css",
"main.js": "/static/js/main.b93f874b.js",
"main.js": "/static/js/main.b060ab28.js",
"static/js/787.eb14398a.chunk.js": "/static/js/787.eb14398a.chunk.js",
"static/media/icons-20.eot": "/static/media/icons-20.cde033c5d3f24283f757.eot",
"static/media/icons-20.woff": "/static/media/icons-20.1ef633d3a28d0986f63e.woff",
Expand All @@ -11,11 +11,11 @@
"static/media/icons-16.ttf": "/static/media/icons-16.13933033991f62d6bb64.ttf",
"index.html": "/index.html",
"main.3b75acbb.css.map": "/static/css/main.3b75acbb.css.map",
"main.b93f874b.js.map": "/static/js/main.b93f874b.js.map",
"main.b060ab28.js.map": "/static/js/main.b060ab28.js.map",
"787.eb14398a.chunk.js.map": "/static/js/787.eb14398a.chunk.js.map"
},
"entrypoints": [
"static/css/main.3b75acbb.css",
"static/js/main.b93f874b.js"
"static/js/main.b060ab28.js"
]
}
2 changes: 1 addition & 1 deletion packages/e2e/test/web-extension/dapp/build/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>React App</title><script defer="defer" src="/static/js/main.b93f874b.js"></script><link href="/static/css/main.3b75acbb.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>React App</title><script defer="defer" src="/static/js/main.b060ab28.js"></script><link href="/static/css/main.3b75acbb.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>

Large diffs are not rendered by default.

Large diffs are not rendered by default.

This file was deleted.

3 changes: 2 additions & 1 deletion packages/e2e/test/web-extension/extension/ui.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ <h2>ADA price</h2>
<button id="clearAllowList">Clear allow list</button>
<div>Name: <span id="observableWalletName">-</span></div>
<div>Address: <span id="address">-</span></div>
<div>Stake Address: <span id="stakeAddress">-</span></div>
<div>Balance: <span id="balance">-</span></div>
<div>Network-wide staked: <span id="supplyDistribution">-</span></div>
<button id="buildAndSignTx">Build & Sign TX</button>
<div>Signature: <span id="signature">-</span></div>
<script src="ui.js"></script>
</body>

</html>
</html>
11 changes: 7 additions & 4 deletions packages/e2e/test/web-extension/extension/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ combineLatest([supplyDistribution.lovelaceSupply$, supplyDistribution.stake$]).s
(document.querySelector('#supplyDistribution')!.textContent = `${stake.live} out of ${lovelaceSupply.total}`)
);

const setAddress = (text: string): void => {
document.querySelector('#address')!.textContent = text;
const setAddresses = ({ address, stakeAddress }: { address: string; stakeAddress: string }): void => {
document.querySelector('#address')!.textContent = address;
document.querySelector('#stakeAddress')!.textContent = stakeAddress;
};

const setBalance = (text: string): void => {
Expand All @@ -85,7 +86,7 @@ const setName = (text: string): void => {

const clearWalletValues = (): void => {
setName('-');
setAddress('-');
setAddresses({ address: '-', stakeAddress: '-' });
setBalance('-');
setSignature('-');
};
Expand All @@ -106,7 +107,9 @@ const walletManager = new WalletManagerUi({ walletName }, { logger, runtime });
const wallet = walletManager.wallet;

// Wallet can be subscribed can be used even before it is actually created.
wallet.addresses$.subscribe(([{ address }]) => setAddress(address.toString()));
wallet.addresses$.subscribe(([{ address, rewardAccount }]) =>
setAddresses({ address: address.toString(), stakeAddress: rewardAccount.valueOf() })
);
wallet.balance.utxo.available$.subscribe(({ coins }) => setBalance(coins.toString()));

const createWallet = async (accountIndex: number) => {
Expand Down
26 changes: 25 additions & 1 deletion packages/e2e/test/web-extension/specs/wallet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,24 @@ describe('wallet', () => {
const deactivateWallet = '#deactivateWallet';
const destroyWallet = '#destroyWallet';
const spanAddress = '#address';
const spanStakeAddress = '#stakeAddress';

const spanBalance = '#balance';
const spanSupplyDistribution = '#supplyDistribution';
const divAdaPrice = '#adaPrice';
const btnSignAndBuildTx = '#buildAndSignTx';
const divSignature = '#signature';
const activeWalletName = '#observableWalletName';

const dappBtnRun = '#bp3-tab-panel_TabsExample_1 > div > button';
const dappSubmittedTxConfirmation = '#root > div > p:last-child';
const dappChangeAddress = '#root > div > p:nth-child(11)';
const dappStakingAddress = '#root > div > p:nth-child(12)';
const dappUsedAddress = '#root > div > p:nth-child(13)';

// The address is filled in by the tests, which are order dependent
let walletAddr1 = '';
let walletStakeAddr1 = '';

const buildAndSign = async () => {
await $(btnSignAndBuildTx).click();
Expand Down Expand Up @@ -74,16 +83,31 @@ describe('wallet', () => {
}
});
walletAddr1 = await $(spanAddress).getText();
walletStakeAddr1 = await $(spanStakeAddress).getText();
expect(walletAddr1).toHaveTextContaining('addr');
expect(walletStakeAddr1).toHaveTextContaining('stake');
await expect($(activeWalletName)).toHaveText(getObservableWalletName(0));
});
it('dapp has access to cip30 WalletApi', async () => {
await browser.switchWindow('React App');
await expect($(pNetworkId)).toHaveText('Network Id (0 = testnet; 1 = mainnet): 0');
await browser.waitUntil($(liFirstUtxo).isExisting, { timeout: 60_000 });
await switchToWalletUi();
});

it('dapp can build and send a transaction using cip30 WalletApi', async () => {
await browser.switchWindow('React App');
await $(dappBtnRun).click();
await expect($(dappSubmittedTxConfirmation)).toHaveTextContaining('check your wallet');
});

it('dapp gets correct addresses from cip30 wallet api', async () => {
await expect($(dappChangeAddress)).toHaveTextContaining(walletAddr1);
await expect($(dappStakingAddress)).toHaveTextContaining(walletStakeAddr1);
await expect($(dappUsedAddress)).toHaveTextContaining(walletAddr1);
});

it('can build and sign a transaction', async () => {
await switchToWalletUi();
await buildAndSign();
});
it('can switch to another wallet', async () => {
Expand Down
21 changes: 15 additions & 6 deletions packages/wallet/src/cip30.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
TxSignErrorCode,
WalletApi
} from '@cardano-sdk/dapp-connector';
import { CML, Cardano, cmlToCore, coreToCml } from '@cardano-sdk/core';
import { CML, Cardano, cmlToCore, coreToCml, parseCmlAddress } from '@cardano-sdk/core';
import { HexBlob, ManagedFreeableScope, usingAutoFree } from '@cardano-sdk/util';
import { InputSelectionError } from '@cardano-sdk/input-selection';
import { Logger } from 'ts-log';
Expand Down Expand Up @@ -74,6 +74,15 @@ const compareUtxos = (utxo: Cardano.Utxo, comparedTo: Cardano.Utxo) => {
return 0;
};

const cardanoAddressToCbor = (address: Cardano.Address | Cardano.RewardAccount): Cbor =>
usingAutoFree((scope) => {
const cmlAddr = parseCmlAddress(scope, address.toString());
if (!cmlAddr) {
throw new ApiError(APIErrorCode.InternalError, `could not transform address ${address} to CBOR`);
}
return Buffer.from(cmlAddr.to_bytes()).toString('hex');
});

export const createWalletApi = (
wallet$: Observable<ObservableWallet>,
confirmationCallback: CallbackConfirmation,
Expand All @@ -98,16 +107,16 @@ export const createWalletApi = (

if (!address) {
logger.error('could not get change address');
throw new ApiError(500, 'could not get change address');
throw new ApiError(APIErrorCode.InternalError, 'could not get change address');
} else {
return address.toString();
return cardanoAddressToCbor(address);
}
} catch (error) {
logger.error(error);
if (error instanceof ApiError) {
throw error;
}
throw new ApiError(500, 'Nope');
throw new ApiError(APIErrorCode.InternalError, 'Nope');
}
},
// eslint-disable-next-line max-statements
Expand Down Expand Up @@ -172,7 +181,7 @@ export const createWalletApi = (
if (!rewardAccount) {
throw new ApiError(APIErrorCode.InternalError, 'could not get reward address');
} else {
return [rewardAccount.toString()];
return [cardanoAddressToCbor(rewardAccount)];
}
} catch (error) {
logger.error(error);
Expand All @@ -195,7 +204,7 @@ export const createWalletApi = (
if (!address) {
throw new ApiError(APIErrorCode.InternalError, 'could not get used addresses');
} else {
return [address.toString()];
return [cardanoAddressToCbor(address)];
}
},
getUtxos: async (amount?: Cbor, paginate?: Paginate): Promise<Cbor[] | undefined> => {
Expand Down
10 changes: 7 additions & 3 deletions packages/wallet/test/integration/cip30mapping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('cip30', () => {
test('api.getUsedAddresses', async () => {
const cipUsedAddressess = await api.getUsedAddresses();
const [{ address: walletAddress }] = await firstValueFrom(wallet.addresses$);
expect(cipUsedAddressess).toEqual([walletAddress]);
expect(cipUsedAddressess.map((cipAddr) => Cardano.Address(cipAddr))).toEqual([walletAddress]);
});

test('api.getUnusedAddresses', async () => {
Expand All @@ -95,11 +95,15 @@ describe('cip30', () => {
test('api.getChangeAddress', async () => {
const cipChangeAddress = await api.getChangeAddress();
const [{ address: walletAddress }] = await firstValueFrom(wallet.addresses$);
expect(cipChangeAddress).toEqual(walletAddress);
expect(Cardano.Address(cipChangeAddress)).toEqual(walletAddress);
});

test('api.getRewardAddresses', async () => {
const cipRewardAddresses = await api.getRewardAddresses();
const cipRewardAddressesCbor = await api.getRewardAddresses();
const cipRewardAddresses = cipRewardAddressesCbor.map((cipAddr) =>
scope.manage(CML.Address.from_bytes(Buffer.from(cipAddr, 'hex'))).to_bech32()
);

const [{ rewardAccount: walletRewardAccount }] = await firstValueFrom(wallet.addresses$);
expect(cipRewardAddresses).toEqual([walletRewardAccount]);
});
Expand Down

0 comments on commit 99a5717

Please sign in to comment.