Skip to content

Commit

Permalink
Signer Extend icrc49 call canister to support [sc-12847] (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriiIdentityLabs authored Jun 13, 2024
1 parent c979abd commit 3aaf368
Show file tree
Hide file tree
Showing 21 changed files with 613 additions and 147 deletions.
50 changes: 30 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[workspace]
members = ["examples/target/src/target"]
members = ["examples/signer-backend"]
resolver = "2"
2 changes: 1 addition & 1 deletion canister_ids.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dev": "mquaw-4yaaa-aaaap-abwlq-cai",
"ic": "vehwg-aiaaa-aaaag-aciuq-cai"
},
"target": {
"signer-backend": {
"dev": "do25a-dyaaa-aaaak-qifua-cai",
"ic": "2wdkf-viaaa-aaaam-ackqq-cai"
},
Expand Down
9 changes: 5 additions & 4 deletions dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
"source": ["docs/out"],
"type": "assets"
},
"target": {
"candid": "examples/target/src/target/target.did",
"package": "target",
"type": "rust"
"signer-backend": {
"candid": "examples/signer-backend/signer-backend.did",
"package": "signer-backend",
"type": "rust",
"optimize": "cycles"
}
},
"defaults": {
Expand Down
12 changes: 6 additions & 6 deletions examples/react-dapp/src/data/icrc49_call_canister.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import { ISection } from "../ui/organisms/section.js"
export const basicRequest = {
method: "icrc49_call_canister",
params: {
canisterId: "rdmx6-jaaaa-aaaaa-aaadq-cai",
canisterId: "vg4p4-3yaaa-aaaad-aad6q-cai",
sender: "535yc-uxytb-gfk7h-tny7p-vjkoe-i4krp-3qmcl-uqfgr-cpgej-yqtjq-rqe",
method: "lookup",
arg: "[10000]",
method: "greet",
arg: '["me"]',
},
}

export const withConsentMessage = {
method: "icrc49_call_canister",
params: {
canisterId: "rdmx6-jaaaa-aaaaa-aaadq-cai",
canisterId: "vg4p4-3yaaa-aaaad-aad6q-cai",
sender: "535yc-uxytb-gfk7h-tny7p-vjkoe-i4krp-3qmcl-uqfgr-cpgej-yqtjq-rqe",
method: "lookup",
arg: "[10000]",
method: "greet",
arg: '["me"]',
consentMessage: "Please confirm the transfer of 100 ICP to Alice",
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const icrc49CallCanisterMethodComponent: MethodComponent = {
return "icrc49_call_canister"
},
getComponent(componentData: ComponentData, setState: Dispatch<SetStateAction<State>>) {
const { origin, methodName, canisterId, sender, args, onApprove, onReject } =
const { origin, methodName, canisterId, sender, args, consentMessage, onApprove, onReject } =
componentData as CallCanisterComponentData
return (
<CallCanister
Expand All @@ -22,6 +22,7 @@ export const icrc49CallCanisterMethodComponent: MethodComponent = {
canisterId={canisterId}
sender={sender}
args={args}
consentMessage={consentMessage}
/>
)
},
Expand Down
51 changes: 51 additions & 0 deletions examples/react-signer/src/idl/consent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type { ActorMethod } from "@dfinity/agent"
import type { IDL } from "@dfinity/candid"

export interface Icrc21ConsentInfo {
metadata: Icrc21ConsentMessageMetadata
consent_message: Icrc21ConsentMessage
}
export type Icrc21ConsentMessage =
| {
LineDisplayMessage: { pages: Array<Icrc21LineDisplayPage> }
}
| { GenericDisplayMessage: string }
export interface Icrc21ConsentMessageMetadata {
language: string
}
export interface Icrc21ConsentMessageRequest {
arg: Uint8Array | number[]
method: string
user_preferences: Icrc21ConsentMessageSpec
}
export interface Icrc21ConsentMessageSpec {
metadata: Icrc21ConsentMessageMetadata
device_spec: [] | [Icrc21DeviceSpec]
}
export type Icrc21DeviceSpec =
| { GenericDisplay: null }
| {
LineDisplay: {
characters_per_line: number
lines_per_page: number
}
}
export type Icrc21Error =
| {
GenericError: { description: string; error_code: bigint }
}
| { InsufficientPayment: Icrc21ErrorInfo }
| { UnsupportedCanisterCall: Icrc21ErrorInfo }
| { ConsentMessageUnavailable: Icrc21ErrorInfo }
export interface Icrc21ErrorInfo {
description: string
}
export interface Icrc21LineDisplayPage {
lines: Array<string>
}
export type Result = { Ok: Icrc21ConsentInfo } | { Err: Icrc21Error }
export interface _SERVICE {
icrc21_canister_call_consent_message: ActorMethod<[Icrc21ConsentMessageRequest], Result>
}
export declare const idlFactory: IDL.InterfaceFactory
export declare const init: (args: { IDL: typeof IDL }) => IDL.Type[]
49 changes: 49 additions & 0 deletions examples/react-signer/src/idl/consent_idl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

export const idlFactory = ({ IDL }: any) => {
const Icrc21ConsentMessageMetadata = IDL.Record({ language: IDL.Text })
const Icrc21DeviceSpec = IDL.Variant({
GenericDisplay: IDL.Null,
LineDisplay: IDL.Record({
characters_per_line: IDL.Nat16,
lines_per_page: IDL.Nat16,
}),
})
const Icrc21ConsentMessageSpec = IDL.Record({
metadata: Icrc21ConsentMessageMetadata,
device_spec: IDL.Opt(Icrc21DeviceSpec),
})
const Icrc21ConsentMessageRequest = IDL.Record({
arg: IDL.Vec(IDL.Nat8),
method: IDL.Text,
user_preferences: Icrc21ConsentMessageSpec,
})
const Icrc21LineDisplayPage = IDL.Record({ lines: IDL.Vec(IDL.Text) })
const Icrc21ConsentMessage = IDL.Variant({
LineDisplayMessage: IDL.Record({
pages: IDL.Vec(Icrc21LineDisplayPage),
}),
GenericDisplayMessage: IDL.Text,
})
const Icrc21ConsentInfo = IDL.Record({
metadata: Icrc21ConsentMessageMetadata,
consent_message: Icrc21ConsentMessage,
})
const Icrc21ErrorInfo = IDL.Record({ description: IDL.Text })
const Icrc21Error = IDL.Variant({
GenericError: IDL.Record({
description: IDL.Text,
error_code: IDL.Nat,
}),
InsufficientPayment: Icrc21ErrorInfo,
UnsupportedCanisterCall: Icrc21ErrorInfo,
ConsentMessageUnavailable: Icrc21ErrorInfo,
})
const Result = IDL.Variant({ Ok: Icrc21ConsentInfo, Err: Icrc21Error })
return IDL.Service({
icrc21_canister_call_consent_message: IDL.Func([Icrc21ConsentMessageRequest], [Result], []),
})
}
export const init = () => {
return []
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { DelegationChain, DelegationIdentity, Ed25519KeyIdentity } from "@dfinity/identity"
import { JsonnableEd25519KeyIdentity } from "@dfinity/identity/lib/cjs/identity/ed25519.js"
import { callCanisterService, CallCanisterRequest } from "./call-canister.service"
import { Agent, HttpAgent, Identity } from "@nfid/agent"

const IC_HOSTNAME = "https://ic0.app"
const HOUR = 3_600_000
const PUBLIC_IDENTITY: JsonnableEd25519KeyIdentity = [
"302a300506032b65700321003b6a27bcceb6a42d62a3a8d02a6f0d73653215771de243a63ac048a18b59da29",
Expand All @@ -23,17 +25,20 @@ describe("Call Canister Service", function () {
{}
)
const delegation = DelegationIdentity.fromDelegation(sessionKey, chain)
const agent: Agent = new HttpAgent({
host: IC_HOSTNAME,
identity: delegation as unknown as Identity,
})
const request: CallCanisterRequest = {
delegation,
canisterId: "rdmx6-jaaaa-aaaaa-aaadq-cai",
calledMethodName: "lookup",
parameters: "[10101]",
agent,
}
const response = await callCanisterService.call(request)
const origins = response.result.result[0] as { origin: string[] }

console.log("result", JSON.stringify(response, null, 2))

expect(response.result.verification.contentMap).toMatch(/^d9d9f7a467636f6e74656e74a7636172674f/)
expect(response.result.verification.certificate).toMatch(/^d9d9f7a36474726565830183018/)
expect(origins.origin[0]).toBe("https://identity.ic0.app")
Expand Down
Loading

0 comments on commit 3aaf368

Please sign in to comment.