-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(smart-wallet): contract for wallet factory
- Loading branch information
Showing
15 changed files
with
367 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// @ts-check | ||
/** | ||
* @file Wallet Factory | ||
* | ||
* Contract to make smart wallets. | ||
*/ | ||
import '@agoric/deploy-script-support/exported.js'; | ||
import '@agoric/wallet-backend/src/types.js'; // TODO avoid ambient types | ||
|
||
import { | ||
makeStoredSubscription, | ||
makeSubscriptionKit, | ||
observeIteration, | ||
} from '@agoric/notifier'; | ||
import spawn from '@agoric/wallet-backend/src/wallet.js'; | ||
import '@agoric/zoe/exported.js'; | ||
import { E, Far } from '@endo/far'; | ||
import { makeScalarBigMapStore } from '@agoric/vat-data'; | ||
|
||
const { assign, entries, keys, fromEntries } = Object; | ||
|
||
/** | ||
* @typedef {{ | ||
* agoricNames: NameHub, | ||
* board: Board, | ||
* namesByAddress: NameHub, | ||
* }} SmartWalletContractTerms | ||
*/ | ||
|
||
/** | ||
* | ||
* @param {ZCF<SmartWalletContractTerms>} zcf | ||
* @param {{ | ||
* storageNode?: ERef<StorageNode>, | ||
* marshaller?: ERef<Marshaller>, | ||
* }} privateArgs | ||
*/ | ||
export const start = async (zcf, privateArgs) => { | ||
const { agoricNames, namesByAddress, board } = zcf.getTerms(); | ||
assert(board, 'missing board'); | ||
assert(namesByAddress, 'missing namesByAddress'); | ||
assert(agoricNames, 'missing agoricNames'); | ||
const zoe = zcf.getZoeService(); | ||
const { storageNode, marshaller } = privateArgs; | ||
|
||
/** @type {MapStore<string, unknown>} */ | ||
const walletsByAddress = makeScalarBigMapStore('walletsByAddress'); | ||
|
||
/** | ||
* | ||
* @param {string} address | ||
* @param {ERef<import('@agoric/vats/src/vat-bank').Bank>} bank | ||
* @param {ERef<MyAddressNameAdmin>} myAddressNameAdmin | ||
*/ | ||
const provideSmartWallet = async (address, bank, myAddressNameAdmin) => { | ||
assert.typeof(address, 'string', 'invalid address'); | ||
assert(bank, 'missing bank'); | ||
assert(myAddressNameAdmin, 'missing myAddressNameAdmin'); | ||
|
||
const walletVat = spawn({ | ||
agoricNames, | ||
namesByAddress, | ||
// ??? why do we make this instead of passing the address itself? | ||
myAddressNameAdmin, | ||
zoe, | ||
board, | ||
localTimerService: undefined, | ||
publicFacetName: 'smartWallet', | ||
}); | ||
|
||
const wallet = await E(walletVat).getWallet(bank); | ||
const admin = E(wallet).getAdminFacet(); | ||
|
||
/** @type {Record<string, ERef<Notifier<unknown>>>} */ | ||
const notifierParts = { | ||
contacts: E(admin).getContactsNotifier(), | ||
dapps: E(admin).getDappsNotifier(), | ||
issuers: E(admin).getIssuersNotifier(), | ||
offers: E(admin).getOffersNotifier(), | ||
payments: E(admin).getPaymentsNotifier(), | ||
purses: E(admin).getPursesNotifier(), | ||
}; | ||
const mutableState = fromEntries(keys(notifierParts).map(key => [key, []])); | ||
const { subscription, publication } = makeSubscriptionKit(); | ||
publication.updateState({ ...mutableState }); | ||
entries(notifierParts).forEach(([key, notifier]) => { | ||
void observeIteration(notifier, { | ||
updateState: value => | ||
publication.updateState({ | ||
...assign(mutableState, { [key]: value }), | ||
}), | ||
}); | ||
}); | ||
|
||
const myWalletStorageNode = | ||
storageNode && E(storageNode).getChildNode(address); | ||
const storedSubscription = makeStoredSubscription( | ||
subscription, | ||
myWalletStorageNode, | ||
marshaller, | ||
); | ||
|
||
const smartWallet = Far('SmartWallet', { | ||
...wallet, | ||
getSubscription: () => storedSubscription, | ||
}); | ||
walletsByAddress.init(address, smartWallet); | ||
return smartWallet; | ||
}; | ||
|
||
return { | ||
creatorFacet: Far('walletFactoryCreator', { | ||
provideSmartWallet, | ||
}), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const makeWalletHolder = () => {}; | ||
harden(makeWalletHolder); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.