Last active
June 14, 2019 13:48
-
-
Save Sajjon/402259c1710728b32ec7b97616d823a8 to your computer and use it in GitHub Desktop.
ChangeAccount ChangeUniverse
This file contains hidden or 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
| public enum NodeFinding { | |
| case anySuitableNode(discovery: NodesDiscovery) | |
| case connectToSpecific(urlToNodes: [URL], ifAllSpecifiedNodesAreUnsuitable: StrategyWhenSpecifiedNodesAreUnsuitable) | |
| } | |
| public extension NodeFinding { | |
| struct StrategyWhenSpecifiedNodesAreUnsuitable { | |
| let strategyWhenSpecifiedNodesAreOffline: StrategyWhenSpecifiedNodesAreOffline | |
| let strategyWhenSpecifiedNodesDontServeShard: StrategyWhenSpecifiedNodesDontServeShard | |
| public init( | |
| ifOffline strategyWhenSpecifiedNodesAreOffline: StrategyWhenSpecifiedNodesAreOffline = .default, | |
| ifShardMismatch strategyWhenSpecifiedNodesDontServeShard: StrategyWhenSpecifiedNodesDontServeShard = .default | |
| ) { | |
| self.strategyWhenSpecifiedNodesAreOffline = strategyWhenSpecifiedNodesAreOffline | |
| self.strategyWhenSpecifiedNodesDontServeShard = strategyWhenSpecifiedNodesDontServeShard | |
| } | |
| } | |
| enum StrategyWhenSpecifiedNodesAreOffline { | |
| case throwError | |
| case fallbackToAnySuitableNode | |
| case pollAndReconnectWhenOnline | |
| public static var `default`: StrategyWhenSpecifiedNodesAreOffline { | |
| return .fallbackToAnySuitableNode | |
| } | |
| } | |
| enum StrategyWhenSpecifiedNodesDontServeShard { | |
| case throwError | |
| case fallbackToAnySuitableNode | |
| public static var `default`: StrategyWhenSpecifiedNodesDontServeShard { | |
| return .fallbackToAnySuitableNode | |
| } | |
| } | |
| } | |
| // USAGE | |
| let hdRootPrivateKey = PrivateKey() // user backs up mnemonic | |
| let hdRootKey = HierarchicalDeterministicRoot(private: hdRootPrivateKey) | |
| let identity = AbstractIdentity(hdRootKey: hdRootKey, alias: "Alice", create: 1.account) | |
| identity.newAccountFromHDRootKeyIfPresent(at: 0) | |
| let ledger = DefaultLedger(universeConfig: .betanet) | |
| let applicationClient = DefaultRadixApplicationClient(ledger: ledger, identity: identity) | |
| applicationClient.changeUniverse( | |
| config: .mainnet, | |
| nodeFinding: .connectToSpecific( | |
| urlToNodes: ["123.1.2.3", "222.111.0.1"], | |
| ifAllSpecifiedNodesAreUnsuitable: .init(ifOffline: .fallbackToAnySuitableNode, ifShardMismatch: .fallbackToAnySuitableNode) | |
| ) | |
| ) | |
| applicationClient.changeUniverse( | |
| config: .betanet, | |
| nodeFinding: .anySuitableNode(discovery: NodesFinder(originNodeFinder: .betanet)) | |
| ) | |
| applicationClient.changeUniverse( | |
| config: .betanet, | |
| nodeFinding: .anySuitableNode(discovery: NodesFinder(urlToSomeOriginNode: "33.22.11.0")) | |
| ) | |
| // Adding a new account from HD root key | |
| applicationClient.identity.newAccountFromHDRootKeyIfPresent(at: 1) | |
| applicationClient.changeAccount { $0.last } | |
| // Adding a new account using a hardware key (private key stored in the hardware key) | |
| // Thus this library will trigger any client (wallet) to present user | |
| // with "sign using hardware key" flow | |
| let radixPlasticKey = HardwareKey.fromNFCScanner() | |
| applicationClient.identity.addAccountForHardwardKey(radixPlasticKey) | |
| applicationClient.changeAccount { $0.last } | |
| // Hardware key is active account, thus a "sign on device" flow is presented to the user | |
| let createTokenAction = CreateToken(name: "SuperCoin", symbol: "SC", granularity: 1234, etc: ()) | |
| class ScreenInWallet {} | |
| class SignTransacionScreen: ScreenInWallet { | |
| func createToken() { | |
| let callback = PromptUserToProvideSigningKey(timeout: nil) | |
| applicationClient.createToken( | |
| createTokenAction, | |
| ifNoSigningKeyPresent: .promptUserToProvideKey(callback) | |
| ) | |
| promptUserToSignAction(callback: callback) | |
| } | |
| func promptUserToSignAction(callback: PromptUserToProvideSigningKey) { | |
| // when user flow is done, call onResult | |
| callback.onResult(.cancelled) | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment