
# Cardano Example Dapp

## **Token-Gated Sign-In**

Backend-less 'sign in with Cardano' for native asset holders only. The wallet blocks on an await/balance condition until the gating token is actually held, then signs a dapp-supplied nonce with CIP-8 and verifies its own signature before returning. The dapp learns the bare-minimum to verify: the realm, the address, the proof and nothing else.


## Try it online: 

-  Visit [HTML+JS Dapp](https://gamechangerfinance.github.io/gamechanger.wallet/examples/Token-Gated%20Sign-In.html)
-  Visit [HTML+React Dapp](https://gamechangerfinance.github.io/gamechanger.wallet/examples/Token-Gated%20Sign-In_react.html)
-  Visit [Native HTML dapp](https://gamechangerfinance.github.io/gamechanger.wallet/examples/Token-Gated%20Sign-In_nolib.html)
-  Run [Standalone URL dapp connection](https://wallet.gamechanger.finance/api/2/run/1-H4sIAAAAAAAAA7VX32_iOBD-VyxeAhJmobD0x1u3e7etTqpOKqd7duIh-HDsrO1sy1X87ze2kxAo7G7RHsoD8XjG33wznpm89pxwEno3vYVeg6JfmANOnkSu6IPqDXscbGZE6YRWuOcTy3ATpxKsJYnFXUQo8izcitwxw5nSCVlqQxRz4hsQZi04stKSg7FEK7kZkcUKyDOTEgWp1NnarxOGzzMT7kPKJFMZkEwrLvyhpFJOSOJQK0ejKifO4yTCEpa5Cg1tyAokH_otinhIKCGclSW1VVlKge4o7W1GmA9_0is8j5NvYMRSgCXCIYbnqMtcZYCkgE4AMYBvCs-MqL1NIoEZPMHjSZkBWggliqpAVNHg5ibIDDBZBEyEcW6QrvhSGq2X4Xil3cq7A9LCCIl2m9JHIbKN7_BSauNubROZEBgflxCWCK1389orNPd6klnX2w57zOTWLwcAuJ6zArIVUzkYyqHQqBvYQNGEf1xeZxdAp2x2SWd8ktJrNgc6zq6WF5fpbML4FLeXWopsQwVHFT67nlxMZ_wqnY3T-ZRlwNIxv5xeXGdznk4mk2w6R_E1jPk0m0_m8wyfyTRFMyEXqEI4dAUvaOvVOrPQ9_DST35na_jr6XMy2OLGrxXDkLuNR-g9MlXwM68wwYJrNVMFy4x3p5YHr2hRWUdToIyidaTXnxPS0Lg-5swCVftJFCXDHFw_8YSNgnIyGJD2N0xi0niDmA-YUrUW-eFvSFrDyYAMyLbhPKLDwHuEUJTBxx08FPSF_c2vP4Wz-m8BNgj34KGihxgs3iBSA6VkG5ZKIFLnIlwWv4fF1-QowpAw5_IXlA_4C2u_ir8OurP4awDu-OvA2-dPOGIzXUK85LuigDdcq1gFjjPYXpRzWWwN1EwOEyx5WDPC8gN_B5cdeF2rx4GeRWgX6-A40i6t50LdrxrnErtvJbBbIw6SRxScSe6h5Rp2U8POBdzoN1eqRtssv-dWddHuzJ7CmTv6L2BZ7cLMivKTyB-rop_k7jjKYTJOBqdh5njZHBi8UNjovf0jeL-Dc4tdQIF71mb9oJa60wNQ5bEj2O37AzanWgV69o8Wqp_Q2peMZSsYdQ4YcekWLEe3Tsjr_2FPAIiJGrv8Pra7yhhQ7raW4TbPT2dPGHoQWBhy_LqfleyXMOiEru_X1kL53luPRs2l8JLmvnmnjt6i9gr5FL-Pbfd08h5039djAfHR8PHAmQJnOBwrFvDiTlMdmb4JTN8awzb9X0D528bztpV37dSx8ZUqxKrFfs_s6jR2u2IfJxddwHtOJ7W1MNJ1rLQTXD2YtB2kuwfXPjPH_saRtMmOTg7V1B-g9-HhqNSN4wEu71ASYdXjLe-cGgfUpwbP_3V43Pi0c3tve6Br1JJSZ5RvStbdNVP_d-a8VjPWrOjTXr2Cr_035zVsYGI4U4HvWG73IZLpSoaJvJ7h20-C-LWwA9tUqY7pXauqEDt1hvmbjHDwE8ZqieVdYP7ulZrDNKpL3FIo_zlz2vVmpn99k__bnwvgQeL_KIz2XSHc_gdFVU6cSw4AAA)

## Source code:

- dapp connection code: [GCScript DSL](Token-Gated%20Sign-In.gcscript)
- frontend (using @gamechanger-finance/gc): [HTML+JS dapp](Token-Gated%20Sign-In.html)
- frontend (using @gamechanger-finance/gc): [HTML+React dapp](Token-Gated%20Sign-In_react.html)
- frontend (without official library): [Native HTML dapp](Token-Gated%20Sign-In_nolib.html)

Dapp code was autogenerated by [Playground IDE in GameChanger Wallet ](https://wallet.gamechanger.finance/playground)

## GCScript code (dapp connection):
```json
{
  "title": "Token-Gated Sign-In",
  "description": "Backend-less 'sign in with Cardano' for native asset holders only. The wallet blocks on an await/balance condition until the gating token is actually held, then signs a dapp-supplied nonce with CIP-8 and verifies its own signature before returning. The dapp learns the bare-minimum to verify: the realm, the address, the proof and nothing else.",
  "type": "script",
  "exportAs": "TokenGatedSignIn",
  "return": {
    "mode": "last"
  },
  "args": {
    "realm": "gamechanger-demo",
    "nonce": "1d5f9c2e-3a47-4d1b-9a6e-0c8f27b41ad3",
    "policy-id": "d491234d8b40b63aceab0d7329c6db111c3634d9e0d3c6166c66c13b",
    "asset-name-hex": "{strToHex('FakeUSD')}",
    "quantity": "1"
  },
  "run": {
    "guards": {
      "type": "macro",
      "run": {
        "nonce-must-be-a-string": "{ assert( isType('string',get('args.nonce'))          ,'nonce must be a string'                                  , get('args') ) }",
        "nonce-must-not-be-empty": "{ assert( not(isEmptyString(get('args.nonce')))       ,'nonce must not be empty: a replayable login is not a login' , get('args') ) }",
        "realm-must-be-a-string": "{ assert( isType('string',get('args.realm'))          ,'realm must be a string'                                  , get('args') ) }",
        "realm-must-not-be-empty": "{ assert( not(isEmptyString(get('args.realm')))       ,'realm must not be empty: it scopes the signature to one dapp' , get('args') ) }",
        "policy-id-must-be-a-string": "{ assert( isType('string',get('args.policy-id'))    ,'gate policyId must be a string'                         , get('args.policy-id') ) }",
        "policy-id-must-not-be-empty": "{ assert( not(isEmptyString(get('args.policy-id'))) ,'gate policyId must not be empty'                         , get('args.policy-id') ) }",
        "asset-name-hex-must-be-a-string": "{ assert( isType('string',get('args.asset-name-hex'))  ,'gate assetName must be a string'                         , get('args.asset-name-hex') ) }",
        "quantity-must-be-a-string": "{ assert( isType('string',get('args.quantity'))     ,'gate quantity must be a string'                          , get('args.quantity') ) }",
        "quantity-must-be-gt-zero": "{ assert( cmpBigNum('gt',get('args.quantity'),'0')  ,'gate quantity must be greater than zero'                  , get('args.quantity') ) }"
      }
    },
    "networkInfo": {
      "type": "getNetworkInfo"
    },
    "networkKey": {
      "type": "macro",
      "run": "{ join('-',get('cache.networkInfo.dltTag'),get('cache.networkInfo.networkTag')) }"
    },
    "address": {
      "type": "getCurrentAddress"
    },
    "gate": {
      "type": "await",
      "until": {
        "holdsGatingToken": {
          "kind": "balance",
          "asset": {
            "policyId": "{get('args.policy-id')}",
            "assetNameHex": "{get('args.asset-name-hex')}",
            "quantity": "{get('args.quantity')}"
          }
        }
      }
    },
    "challengeText": {
      "type": "macro",
      "run": "{join(':',getArray(get('cache.networkInfo.dltTag'),get('cache.networkInfo.networkTag'),get('args.realm'),get('args.nonce'),get('cache.address')))}"
    },
    "challengeHash": {
      "type": "macro",
      "run": "{sha512(get('cache.challengeText'))}"
    },
    "proof": {
      "type": "script",
      "run": {
        "signature": {
          "type": "signDataWithAddress",
          "address": "{get('cache.address')}",
          "dataHex": "{get('cache.challengeHash')}"
        },
        "verified": {
          "type": "verifySignatureWithAddress",
          "address": "{get('cache.address')}",
          "dataHex": "{get('cache.challengeHash')}",
          "dataSignature": "{get('cache.proof.signature')}"
        }
      }
    },
    "postConditions": {
      "type": "macro",
      "run": {
        "signature-must-verify": "{ assert( eq(get('cache.proof.verified'),true) ,'the wallet could not verify its own CIP-8 signature' , get('cache.proof') ) }",
        "audit-trail": "{ console('info',get('cache.challengeText')) }"
      }
    },
    "finally": {
      "type": "macro",
      "run": {
        "realm": "{get('args.realm')}",
        "address": "{get('cache.address')}",
        "challengeHash": "{get('cache.challengeHash')}",
        "signature": "{get('cache.proof.signature')}"
      }
    }
  }
}
```

## Run standalone QR dapp connection: 

You can use [Playground IDE in GameChanger Wallet ](https://wallet.gamechanger.finance/playground) in `QR URL Transport` mode to capture results

[![This GCScript/URL is too large! make it shorter uploading parts to GCFS. Unable to generate QR code](Token-Gated%20Sign-In.png)](https://gamechangerfinance.github.io/gamechanger.wallet/examples/Token-Gated%20Sign-In.png)

## Resources
- [How to connect?](https://www.npmjs.com/package/@gamechanger-finance/gc)
- [Docs and examples on Github](https://github.com/GameChangerFinance/gamechanger.wallet/)
- [GCScript documentation](https://wallet.gamechanger.finance/doc/api/v2)
- [Playground IDE in GameChanger Wallet ](https://wallet.gamechanger.finance/playground)
- [Tutorials on Youtube](https://www.youtube.com/@gamechanger.finance)
- [Support on Discord](https://discord.gg/vpbfyRaDKG)
- [News on Twitter](https://twitter.com/GameChangerOk)
- [Website](https://gamechanger.finance)

## License
MIT 
    
