Adding an Identity
You can associate a crosschain identity with a DEMOS address by creating a signature using your private key and sending it to the DEMOS network.
0. Imports
import { Demos, DemosWebAuth } from "@kynesyslabs/demosdk/websdk"
import { Identities } from "@kynesyslabs/demosdk/abstraction"
import { InferFromSignaturePayload } from "@kynesyslabs/demosdk/types/abstraction"
1. Creating the signature
const message = "Hello, world!"
const signature = await instance.signMessage(message)
const verified = await instance.verifyMessage(
message,
signature,
instance.getAddress(),
)
expect(verified).toBe(true)
2. Creating the DEMOS payload
The payload to add a Sepolia address would look like this:
const payload: InferFromSignaturePayload = {
method: "identity_assign_from_signature",
target_identity: {
chain: "eth",
chainId: instance.chainId,
subchain: "sepolia",
isEVM: true,
signature: signature,
signedData: message,
targetAddress: instance.getAddress(),
},
}
3. Connecting to the network
Create a new Demos
object and connnect the RPC and your private key.
const rpc = "https://demosnode.discus.sh"
// create new keypair
const identity = DemosWebAuth.getInstance()
await identity.create()
// connect to the DEMOS rpc
const demos = new Demos()
await demos.connect(rpc)
// connect private key
await demos.connectWallet(identity.keypair.privateKey as Uint8Array)
4. Sending the payload
Then send the identity payload to the network using the identities
instance as shown:
Use identities.inferIdentity
to create a transaction that adds your identity to the blockchain. Then broadcast the transaction using demos.broadcast
.
const identities = new Identities()
const validityData = await identities.inferXmIdentity(demos, payload)
const res = await demos.broadcast(validityData)
console.log(res)
// {
// result: 200,
// response: { message: 'Signature verified. Transaction applied.' },
// require_reply: false,
// extra: { confirmationBlock: 3 }
// }
Getting Identities
After the confirmationBlock
has been forged, you can retrieve all the identities associated with your DEMOS address as shown:
const res = await identities.getIdentities(demos)
console.log(res)
// {
// "result": 200,
// "response": {
// "xm": {
// "evm": {
// "sepolia": [
// "0x4298A9D2A573dA64102255d11d6908b7e3d89b02"
// ]
// }
// },
// "web2": {}
// },
// "require_reply": false,
// "extra": null
// }
Removing an Identity
You can remove your crosschain identity from DEMOS as shown:
const validityData = await identities.removeXmIdentity(demos, {
chain: "evm",
subchain: "sepolia",
targetAddress: instance.getAddress(), // the address to remove
})
const res = await demos.broadcast(validityData)
console.log(res)
// {
// result: 200,
// response: { message: 'Transaction applied, waiting for confirmation' },
// require_reply: false,
// extra: { confirmationBlock: 25 }
// }