Skip to content

Commit

Permalink
fix: add more types and refactor naming of facets
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Nov 19, 2020
1 parent a4a5571 commit 45fd2b9
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 120 deletions.
8 changes: 4 additions & 4 deletions packages/dapp-svelte-wallet/api/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ export default async function deployWallet(
const issuerToPetname = new Map();
const issuerToPursePetnameP = new Map();
const wallet = await E(walletVat).getWallet();
const walletInternals = E(wallet).getInternals();
const walletAdmin = E(wallet).getAdminFacet();
await Promise.all(
paymentInfo.map(async ({ issuerPetname, issuer }) => {
// Create some issuer petnames.
if (issuerToPetname.has(issuer)) {
return issuerToPetname.get(issuer);
}
issuerToPetname.set(issuer, issuerPetname);
await E(walletInternals).addIssuer(issuerPetname, issuer);
await E(walletAdmin).addIssuer(issuerPetname, issuer);
return issuerToPetname.get(issuer);
}),
);
Expand Down Expand Up @@ -110,7 +110,7 @@ export default async function deployWallet(
if (!issuerToPursePetnameP.has(issuer)) {
issuerToPursePetnameP.set(
issuer,
E(walletInternals)
E(walletAdmin)
.makeEmptyPurse(issuerPetname, pursePetname)
.then(
_ => pursePetname,
Expand All @@ -121,7 +121,7 @@ export default async function deployWallet(
pursePetname = await issuerToPursePetnameP.get(issuer);

// Deposit payment.
await E(walletInternals).deposit(pursePetname, payment);
await E(walletAdmin).deposit(pursePetname, payment);
}),
);

Expand Down
7 changes: 7 additions & 0 deletions packages/dapp-svelte-wallet/api/src/internal-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,10 @@
* @property {() => Promise<boolean>} refresh
* @property {() => Promise<boolean>} getAmountOf
*/

/* eslint-disable jsdoc/valid-types */
/**
* FIXME: Bit of a hack to extract the actual type exported by makeWallet.
*
* @typedef {ReturnType<typeof import('./wallet').makeWallet>['admin']} WalletAdminFacet
*/
74 changes: 44 additions & 30 deletions packages/dapp-svelte-wallet/api/src/lib-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const cmp = (a, b) => {
* @property {(state: any) => void} [inboxStateChangeHandler=noActionStateChangeHandler]
* @param {MakeWalletParams} param0
*/
export async function makeWallet({
export function makeWallet({
zoe,
board,
pursesStateChangeHandler = noActionStateChangeHandler,
Expand Down Expand Up @@ -121,10 +121,16 @@ export async function makeWallet({
// @qclass is lost.
const { unserialize: fillInSlots } = makeMarshal(noOp, identitySlotToValFn);

/** @type {NotifierRecord<OfferState[]>} */
const {
notifier: pursesNotifier,
updater: pursesUpdater,
} = /** @type {NotifierRecord<PursesFullState[]>} */ (makeNotifierKit([]));
notifier: offersNotifier,
updater: offersUpdater,
} = makeNotifierKit();

/** @type {NotifierRecord<PursesFullState[]>} */
const { notifier: pursesNotifier, updater: pursesUpdater } = makeNotifierKit(
[],
);

/**
* @param {Petname} pursePetname
Expand Down Expand Up @@ -291,6 +297,7 @@ export async function makeWallet({
inboxState.set(id, offerForDisplay);
if (doPush) {
// Only trigger a state change if this was a single update.
offersUpdater.updateState([...inboxState.values()]);
inboxStateChangeHandler(getInboxState());
}
}
Expand All @@ -303,6 +310,7 @@ export async function makeWallet({
),
);
// Now batch together all the state changes.
offersUpdater.updateState([...inboxState.values()]);
inboxStateChangeHandler(getInboxState());
}

Expand Down Expand Up @@ -1089,15 +1097,14 @@ export async function makeWallet({
};

// Allow people to send us payments.
const selfContact = await addContact('Self', {
const selfContact = addContact('Self', {
receive(payment) {
return addPayment(payment);
},
});

async function getDepositFacetId(_brandBoardId) {
// Always return the generic deposit facet.
return selfContact.depositBoardId;
return E.G(selfContact).depositBoardId;
}

async function disableAutoDeposit(pursePetname) {
Expand Down Expand Up @@ -1132,15 +1139,13 @@ export async function makeWallet({
return pendingP;
}

const pr = makePromiseKit();
pendingEnableAutoDeposits.init(brand, pr.promise);

const boardId = selfContact.depositBoardId;
const boardIdP = E.G(selfContact).depositBoardId;
pendingEnableAutoDeposits.init(brand, boardIdP);
const boardId = await boardIdP;
brandToDepositFacetId.init(brand, boardId);

pr.resolve(boardId);
await updateAllPurseState();
return pr.promise;
return boardIdP;
}

/**
Expand Down Expand Up @@ -1269,6 +1274,9 @@ export async function makeWallet({
getIssuersNotifier() {
return issuersNotifier;
},
getOffersNotifier() {
return offersNotifier;
},
addIssuer,
publishIssuer,
addInstance,
Expand All @@ -1287,6 +1295,11 @@ export async function makeWallet({
getPurse,
getPurseIssuer,
addOffer,
async addOfferInvitation(_offer, _invitation, _dappOrigin = undefined) {
// Will be part of the Rendezvous system, when landed.
// TODO unimplemented
throw Error(`Adding an invitation to an offer is unimplemented`);
},
declineOffer,
cancelOffer,
acceptOffer,
Expand All @@ -1309,21 +1322,22 @@ export async function makeWallet({
},
});

// Make Zoe invite purse
const ZOE_INVITE_BRAND_PETNAME = 'zoe invite';
const ZOE_INVITE_PURSE_PETNAME = 'Default Zoe invite purse';
const inviteIssuerP = E(zoe).getInvitationIssuer();
const addZoeIssuer = issuerP =>
wallet.addIssuer(ZOE_INVITE_BRAND_PETNAME, issuerP);
const makeInvitePurse = () =>
wallet.makeEmptyPurse(ZOE_INVITE_BRAND_PETNAME, ZOE_INVITE_PURSE_PETNAME);
const addInviteDepositFacet = () =>
E(wallet).enableAutoDeposit(ZOE_INVITE_PURSE_PETNAME);

await addZoeIssuer(inviteIssuerP)
.then(makeInvitePurse)
.then(addInviteDepositFacet);
zoeInvitePurse = wallet.getPurse(ZOE_INVITE_PURSE_PETNAME);

return wallet;
const initialize = async () => {
// Make Zoe invite purse
const ZOE_INVITE_BRAND_PETNAME = 'zoe invite';
const ZOE_INVITE_PURSE_PETNAME = 'Default Zoe invite purse';
const inviteIssuerP = E(zoe).getInvitationIssuer();
const addZoeIssuer = issuerP =>
wallet.addIssuer(ZOE_INVITE_BRAND_PETNAME, issuerP);
const makeInvitePurse = () =>
wallet.makeEmptyPurse(ZOE_INVITE_BRAND_PETNAME, ZOE_INVITE_PURSE_PETNAME);
const addInviteDepositFacet = () =>
E(wallet).enableAutoDeposit(ZOE_INVITE_PURSE_PETNAME);

await addZoeIssuer(inviteIssuerP)
.then(makeInvitePurse)
.then(addInviteDepositFacet);
zoeInvitePurse = wallet.getPurse(ZOE_INVITE_PURSE_PETNAME);
};
return { admin: wallet, initialized: initialize() };
}
63 changes: 34 additions & 29 deletions packages/dapp-svelte-wallet/api/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,22 @@
*/

/**
* @typedef {string | string[]} Petname
*/

/**
* @typedef {Object} PursesJSONState
* @property {string} brandBoardId
* @property {string=} depositBoardId
* @property {Petname} brandPetname
* @property {Petname} pursePetname
* @property {any} displayInfo
* @property {any} value
* @property {any} currentAmountSlots
* @property {any} currentAmount
* @typedef {string | string[]} Petname A petname can either be a plain string
* or a path for which the first element (the edgename) is a petname. We are
* migrating away from using plain strings, for consistency.
*/

/**
* @typedef {Object} WalletBridge The wallet methods available within the
* context of a Dapp.
* @property {(offer: OfferState) => Promise<void>} addOffer
* @property {(offer: OfferState) => Promise<string>} addOffer
* @property {(offer: OfferState, invitation: ERef<Payment>) => Promise<string>}
* addOfferInvitation add an invitation to the specified offer
* @property {(brandBoardId: string) => Promise<string>} getDepositFacetId
* @property {() => Promise<Notifier<Array<[string, PursesJSONState]>>>}
* @property {() => Promise<Notifier<Array<PursesFullState>>>}
* getPursesNotifier
* @property {() => Promise<Notifier<Array<[string, OfferJSONState]>>>}
* getOfferNotifier
* @property {() => Promise<Notifier<Array<OfferState>>>}
* getOffersNotifier
* @property {(petname: Petname, issuerBoardId: string) => Promise<void>}
* suggestIssuer
* @property {(petname: Petname, installationBoardId: string) => Promise<void>}
Expand All @@ -40,23 +32,36 @@
* suggestInstance
*/

/**
* @typedef {Object} WalletTodo
* @property {(offer: OfferState, invitation: ERef<Payment>) => Promise<string>}
* addOfferInvitation add an invitation to the specified offer
*/

/**
* @typedef {Object} WalletUser the presence exposed as `local.wallet` (or
* `home.wallet`)
* @property {() => Promise<WalletBridge>} getAnonymousBridge return the wallet
* bridge not bound to a given Dapp
* @property {() => Promise<any>} getInternals return the specialized backend
* for the wallet UI frontend
* @property {(suggestedDappPetname: Petname, dappOrigin: string) =>
* Promise<WalletBridge>} getBridge return a wallet bridge corresponding to an
* origin that must be approved in the wallet UI
* @property {() => Promise<WalletBridge>} getPreapprovedBridge return the
* wallet bridge that bypasses Dapp-authorization
* @property {(brandBoardId: string) => Promise<string>} getDepositFacetId
* @property {() => Promise<Array<[Petname, Issuer]>>} getIssuers
* @property {() => Array<[Petname, Issuer]>} getIssuers
* @property {(petname: Petname) => Issuer} getIssuer
* @property {() => Promise<Array<[Petname, Purse]>>} getPurses
* @property {() => Array<[Petname, Purse]>} getPurses
* @property {(petname: Petname) => Purse} getPurse
* @property {(payment: ERef<Payment>) => Promise<void>} addPayment
*/

/**
* @typedef {Object} PursesJSONState
* @property {string} brandBoardId
* @property {string=} depositBoardId
* @property {Petname} brandPetname
* @property {Petname} pursePetname
* @property {any} displayInfo
* @property {any} value
* @property {any} currentAmountSlots
* @property {any} currentAmount
*/

/**
* @typedef {Object} OfferState
* @property {any} requestContext
* @property {string} id
*/
Loading

0 comments on commit 45fd2b9

Please sign in to comment.