Overview
ð¡ TIP
ãã®ãã¥ãŒããªã¢ã«ã®ã¹ã¿ãŒã¿ãŒã³ãŒãã Flow Playground ã§éããŸããããã¯ãååã®ãã¥ãŒããªã¢ã«ãšåãã³ãŒãã§ãã
https://play.flow.com/47d92bae-5234-463c-ae14-3dbd452a004f
ãã¥ãŒããªã¢ã«ã§ã¯ããã®ã³ãŒããšããåãããããã«ãããŸããŸãªæäœãè¡ãããæ±ããããŸãã
ACTION
ã¢ã¯ã·ã§ã³ãå¿
èŠãªæ瀺ã¯ãåžžã«ãã®ãããªå¹ãåºãããã¯ã¹å
ã«èšèŒãããŸãã ãã€ã©ã€ããããã¢ã¯ã·ã§ã³ã¯ãã³ãŒããå®è¡ããããã«å¿
èŠãªãã¹ãŠã§ãããèšèªã®èšèšãç解ããããã«ã¯ãæ®ãã®éšåãèªãããšãå¿
èŠã§ãã
ãã®ãã¥ãŒããªã¢ã«ã¯ãååã®Resource
ãã¥ãŒããªã¢ã«ãåæãšããŠããŸãã ãã®ãã¥ãŒããªã¢ã«ãå§ããåã«ãã¢ã«ãŠã³ãããã©ã³ã¶ã¯ã·ã§ã³ããªãœãŒã¹ãããã³çœ²åè
(signer)ãããŒã·ãã¯ãã£ãŒã«ãã¿ã€ããšã©ã®ããã«é£æºããããç解ããŠããå¿
èŠããããŸãã ãã®ãã¥ãŒããªã¢ã«ã§ã¯ãã¢ã«ãŠã³ããšãªãœãŒã¹ã®ç解ãåæãšããŠããŸãã
æš©é(Capability)ãšæš©éä»äž(Entitlement)ã䜿çšããŠãªãœãŒã¹ãšããåãããæ¹æ³ãåŠç¿ããŸãã
Cadenceã§ã¯ããªãœãŒã¹ã¯æ§é äœãã¯ã©ã¹ãšåæ§ã«è€åå(composite type)ã§ãããããã€ãã®ç¹å¥ãªã«ãŒã«ããããŸãã
- ãªãœãŒã¹ã®åã€ã³ã¹ã¿ã³ã¹ã¯ãå³å¯ã«1ã€ã®å Žæã«ã®ã¿ååšã§ããã³ããŒããããšã¯ã§ããŸããã
- ãªãœãŒã¹ã¯ãã¢ã¯ã»ã¹æã«æ瀺çã«1ã€ã®å Žæããå¥ã®å Žæã«ç§»åããå¿ èŠããããŸãã
- ãŸãããªãœãŒã¹ã¯é¢æ°å®è¡ã®çµäºæã«ã¹ã³ãŒãå€ã«åºãããšã¯ã§ãããæ瀺çã«ã©ããã«ä¿åããããç Žæ£ããå¿ èŠããããŸãã
Use-Cases for Capabilities and Entitlements
çŸå®çãªç¶æ³(real-world context)ã§ãªãœãŒã¹ãžã®ã¢ã¯ã»ã¹ãæ¡å€§ããããã«ãæ©èœ(Capability)ãšæš©é(Entitlement)ã䜿çšãããçç±ãèŠãŠã¿ãŸãããã
å®éã®ãŠãŒã¶ãŒã®ã¢ã«ãŠã³ããä¿åããããªããžã§ã¯ãã«ã¯ãããŸããŸãªã¬ãã«ã®ã¢ã¯ã»ã¹ç¯å²ããã©ã€ãã·ãŒãå¿ èŠãªé¢æ°ããã£ãŒã«ããå«ãŸããŸããäŸãã°ããŠãŒã¶ãŒéã§ããŒã¯ã³ã亀æã§ããã¢ããªãéçºããŠãããšããŸããããŒã¯ã³ãã¢ã«ãŠã³ãããåŒãåºã(withdraw)æ©èœã¯ãããŒã¯ã³ã®ææè ã®ã¿ãã¢ã¯ã»ã¹ã§ããããã«ããããšèããã§ããããããããã¢ããªã§ã¯èª°ã§ãããŒã¯ã³ãé ãããã(deposit)ããã«ãã¹ãã§ãã
æ©èœ(Capability)ãšæš©é(Entitlement)ã¯ãææè³ç£ãžã®ã¢ã¯ã»ã¹ã詳现ã«å¶åŸ¡ããããšãå¯èœã«ããŸãã ããã«ãã£ãŠããŠãŒã¶ãŒã¯ãã¢ã«ãŠã³ããææç©ã®æ©èœã®ãã¡ãã©ã®æ©èœã«èªåèªèº«ãä¿¡é Œããå人ãäžè¬ãŠãŒã¶ãŒãã¢ã¯ã»ã¹ã§ããããæå®ããããšãã§ããŸãã
ããšãã°ããŠãŒã¶ãŒã¯èªåã®å人ã«ãèªåã®ãéã䜿ã£ãŠäœããè³Œå ¥ããŠããããããšèãããããããŸããããã®å Žåããã®å人ã«ã¢ã«ãŠã³ãã®ãã®éšåã®ã¿ãžã®ã¢ã¯ã»ã¹æš©ãäžããæš©éãä»äžããããšãã§ããŸãã
å¥ã®äŸãšããŠã¯ããŠãŒã¶ãŒãååŒã¢ããªãåããŠèªèšŒããéã«ããŠãŒã¶ãŒã®ã¢ã«ãŠã³ãã®ååŒæ©èœã«ã¢ããªãã¢ã¯ã»ã¹ã§ããããã«ããCapabilityãªããžã§ã¯ãããŠãŒã¶ãŒã«èŠæ±ããã¢ããªãæ¯åãŠãŒã¶ãŒã«çœ²åãæ±ããå¿ èŠããªãããã«ããããšãã§ããŸãã
ãã®ãã¥ãŒããªã¢ã«ã§ã¯ã以äžã®ããšãè¡ããŸãã
- ãã©ã³ã¶ã¯ã·ã§ã³ã䜿çšããŠäœæãããªãœãŒã¹ãšããåããã
- ãªãœãŒã¹ã®ã¢ã¯ã»ã¹ç¯å²ãæ¡åŒµããCapabilityãäœæãã
- CapabilityãéããŠãªãœãŒã¹ãšããåãããã¹ã¯ãªãããå®è¡ãã
Accessing Resources with Capabilities
ãã®ãã¥ãŒããªã¢ã«ãéå§ããåã«ãHelloWorld
ã¹ããŒãã³ã³ãã©ã¯ããã¢ã«ãŠã³ã0x06
ã«ãããã€ãããŠããå¿
èŠããããŸããããã¯ãäžã€åã®ãªãœãŒã¹ã¹ããŒãã³ã³ãã©ã¯ããã¥ãŒããªã¢ã«ãšåæ§ã§ãã
ACTION
HelloWorldResource.cdc
ãšããååã®ãã¡ã€ã«ãããã¢ã«ãŠã³ã0x06
ã¿ããéããŸããHelloWorldResource.cdc
ã«ã¯ä»¥äžã®ã³ãŒããå«ãŸããŠããã¯ãã§ãã
access(all) contract HelloWorld {
// Declare a resource that only includes one function.
access(all) resource HelloAsset {
// A transaction can call this function to get the "Hello, World!"
// message from the resource.
access(all) fun hello(): String {
return "Hello, World!"
}
}
// We're going to use the built-in create function to create a new instance
// of the HelloAsset resource
access(all) fun createHelloAsset(): @HelloAsset {
return <-create HelloAsset()
}
}
ACTION
ãã®ã³ãŒããã¢ã«ãŠã³ã0x06
ã«ãããã€ããã«ã¯ãDeploy
ãã¿ã³ã䜿çšããŸãã
ACTION
Create Hello
ãã©ã³ã¶ã¯ã·ã§ã³ãã¯ãªãã¯ãã眲åè
(signer)ãšããŠ0x06
ãæå®ããŠéä¿¡ããŸãã
äžèšã®ã¹ããŒãã³ã³ãã©ã¯ããšãã©ã³ã¶ã¯ã·ã§ã³ã¯ããã®ãã¥ãŒããªã¢ã«ã§äœ¿çšãããªãœãŒã¹ã®äœæã»ä¿åãè¡ããŸããã¹ããŒãã³ã³ãã©ã¯ããšãã©ã³ã¶ã¯ã·ã§ã³ã®è©³çŽ°ã«ã€ããŠã¯ãååã®ãã¥ãŒããªã¢ã«ãåç §ããŠãã ããã
Creating Capabilities and References to Stored Resources
ã¹ãã¬ãŒãžã«ã¢ã¯ã»ã¹ããã«ã¯ãã¢ã«ãŠã³ãã®ææè ã®æ瀺çãªèš±å¯ãå¿ èŠã«ãªããŸããæ©èœ(Capability)ã«ãããã¢ã«ãŠã³ãã®ææè ã¯ãã¢ã«ãŠã³ãã«ä¿åãããŠãããªããžã§ã¯ãã®ç¹å®ã®ãã£ãŒã«ããé¢æ°ãžã®ã¢ã¯ã»ã¹ãèš±å¯ããããšãã§ããŸããïŒè©³çŽ°ã¯ä»¥äžã§èª¬æããŸãïŒ
次ã®ãã©ã³ã¶ã¯ã·ã§ã³ã§ã¯ãissue
é¢æ°ã䜿çšããŠæ°ããæ©èœ(Capability)ãäœæããŸããããã«ãããHelloAsset
ãªãœãŒã¹ãªããžã§ã¯ã(ãªãœãŒã¹ã€ã³ã¹ã¿ã³ã¹)ãžã®ãªã³ã¯ãäœæãããŸãã次ã«ããã®ãªã³ã¯ãã¢ã«ãŠã³ãã®ãããªãã¯ã¹ããŒã¹ã«å
¬éããä»ã®ãŠãŒã¶ãŒãã¢ã¯ã»ã¹ã§ããããã«ããŸãã
次ã«ã誰ã§ããã®ãªã³ã¯ã䜿çšããŠããã®ãªããžã§ã¯ãã®åç
§(Reference)ãåããŠhello()
é¢æ°ãåŒã³åºãããšãã§ããããšãè©ŠããŸãããã®ãã©ã³ã¶ã¯ã·ã§ã³ã§äœãèµ·ãã£ãŠããã®ãã®è©³çŽ°ãªèª¬æã¯ããã©ã³ã¶ã¯ã·ã§ã³ã³ãŒãã®äžã«ãããŸããããåãããªããªã£ãããèªã¿é²ããŠãã ããã
ACTION
Create Link
ïŒãªã³ã¯ã®äœæïŒãšããååã®ãã©ã³ã¶ã¯ã·ã§ã³ãéããŸãã
Create Link
ã«ã¯ä»¥äžã®ã³ãŒããå«ãŸããŠããŸãã
import HelloWorld from 0x06
/// This transaction issues a new capability for the HelloAsset resource
/// in storage and publishes it
///
/// Other accounts and scripts can use this public capability
/// to create a reference to the private object to be able to
/// access its fields and call its methods.
transaction {
// We use `auth(IssueStorageCapabilityController, PublishCapability) &Account` to
// ensure that the only thing that this transaction is allowed to do with the signer's account
// is issue and publish capabilities
prepare(account: auth(IssueStorageCapabilityController, PublishCapability) &Account) {
// Create a capability by linking the capability to
// an object in account storage at the specified path
// The capability allows access to the object of the type specified
// without needing to actually possess the object
let capability = account.capabilities.storage.issue<&HelloWorld.HelloAsset>(/storage/HelloAssetTutorial)
// Publish the capability so it is accessible to all
account.capabilities.publish(capability, at: /public/HelloAssetTutorial)
// Use the capability's borrow method to create a new reference
// to the object that the capability links to
// We use optional chaining "??" to get the value because
// result of the borrow could fail, so it is an optional.
// If the optional is nil,
// the panic will happen with a descriptive error message
let helloReference = capability.borrow()
?? panic("Could not borrow a reference to the HelloAsset capability. This could be"
.concat("because the resource is not stored or the capability wasn't published.")
.concat("Run the Create Hello transaction again to store the resource"))
// Call the hello function using the reference
// to the HelloAsset resource.
//
log(helloReference.hello())
}
}
ACTION
ã¢ã«ãŠã³ã0x06
ããã©ã³ã¶ã¯ã·ã§ã³çœ²åè
ãšããŠéžæããããŸãŸã«ãªã£ãŠããããšã確èªããŸãã
Send
ãã¿ã³ãã¯ãªãã¯ããŠãã©ã³ã¶ã¯ã·ã§ã³ãéä¿¡(å®è¡)ããŸãã
ãã®ãã©ã³ã¶ã¯ã·ã§ã³ã§ã¯ãprepareãã§ãŒãºã§ä»¥äžãè¡ããŸãã
- ã¢ã«ãŠã³ãã®ãã¹
/storage/HelloAssetTutorial
ã«ä¿åãããŠãããªããžã§ã¯ãHelloWorld.HelloAsset
ã«å¯Ÿããæ©èœçŸ€(Capability)ãaccount.capabilities.storage.issue
ã¡ãœããã䜿çšããŠäœæããŸãã - ã¢ã«ãŠã³ããã¹
/public/HelloAssetTutorial
ã«Capabilityãä¿åããŸãã -
borrow
ã¡ãœããã䜿çšããŠããªã³ã¯ãããªããžã§ã¯ããžã®åç §(helloReference
ãšåŒã¶)ãäœæããŸãã - äœæããåç
§ã
helloReference
ã䜿çšããŠãhello()
é¢æ°ãåŒã³åºããŸãã
ã³ã³ãœãŒã«ã«åã³ãHello, World
ããšè¡šç€ºãããã¯ãã§ããHelloAsset
ãªããžã§ã¯ãã®ã¡ãœãããåŒã³åºããããšã«æ··ä¹±ããããããããŸãããå®éã«ã¹ãã¬ãŒãžããèªã¿èŸŒãã§å¶åŸ¡ããå¿
èŠããªãã®ã«åŒã³åºããã®ã§ãïŒæ¬åœãªãããã¯ãã¢ã«ãŠã³ãã®/storage/
ãã¡ã€ã³ã«ä¿åãããŠããããã©ã€ããŒãã§ããã¹ãã§ãã
ããã¯ãHelloAsset
ãªããžã§ã¯ãã® æ©èœçŸ€(Capability)ãäœæããããã§ããCapabilityã¯ãä»ã®èšèªã«ããããã€ã³ã¿ã®ãããªãã®ã§ããããããã现ããå¶åŸ¡ãå¯èœã§ãã
Capability Based Access Control
æ©èœçŸ€(Capability)ã«ããããªããžã§ã¯ãã®ææè ã¯ããã©ã€ããŒããªãªããžã§ã¯ãã®ã©ã®æ©èœãä»ã®ãŠãŒã¶ãŒã«å ¬éãããããæå®ããããšãã§ããŸãããã®æŠå¿µã«éŠŽæã¿ã®ããæ¹ã§ããã°ãã¢ã«ãŠã³ãã®APIã®ãããªãã®ãšèããŠãã ããã
ã¢ã«ãŠã³ãã®ææè ã¯ãåéåããéã®ããã«ããã©ã€ããŒããªãªããžã§ã¯ããã¹ãã¬ãŒãžã«ä¿ç®¡ããŠããŸãããããã§ããã¢ã«ãŠã³ãå ã®åéåãä»ã®ãŠãŒã¶ãŒã«ãèŠãããããã«ãããå Žåããç¹å®ã®è³ç£ã®é éæ©èœã«èª°ã§ãã¢ã¯ã»ã¹ã§ããããã«ãããå Žåãããã§ãããããããã®ãªããžã§ã¯ãã¯ããã©ã«ãã§ãã©ã€ããŒãã¹ãã¬ãŒãžã«ä¿ç®¡ããããããææè ã¯ãå®å šãªã³ã³ãããŒã«ãç¶æããªããããããã®ã¢ã¯ã»ã¹ãéæŸããããã«äœããã®æªçœ®ãè¬ããå¿ èŠããããŸãããã®ç®çã®ããã«ãç§ãã¡ã¯æ©èœçŸ€(Capability)ãäœæããŸãã
äŸãã°ãHelloAsset
ã®ãªãŒããŒã¯ãä»ã®ãŠãŒã¶ãŒã«hello
ã¡ãœãããåŒã³åºããããã«ããããšèãããããããŸãããããããæ©èœïŒcapabilityïŒã®ç®çã§ããæ©èœçŸ€(Capability)ã¯ããªã³ã¯ãäœæãããéã«æå®ãããåãæã€ãã¢ã«ãŠã³ãã®ã¹ãã¬ãŒãžå
ã®ãªããžã§ã¯ããžã®ãªã³ã¯ãè¡šããŸãã
ãã®æ©èœ(Capability)ãæã€ä»ã®ãŠãŒã¶ãŒã¯ããã®æ©èœ(Capability)ããªã³ã¯ãããŠãããªããžã§ã¯ãã移åãŸãã¯åé€ã§ããªãããšã«æ³šæããŠãã ããã 圌ãã¯ããªãŒããŒãissue
ã¡ãœããã®åæå®ããã³æš©éã¬ãã«ã§æ瀺çã«å®£èšãããã£ãŒã«ãã®ã¿ã«ã¢ã¯ã»ã¹ã§ããŸãïŒåŸè¿°ïŒã
Capabilityèªäœã«ã¯æå³ã®ããæ©èœã¯ãããŸãããããã¹ãŠã®æš©é(Capability)ã«ã¯åborrow
ã¡ãœããããããããã¯æ©èœçŸ€(Capability)ããªã³ã¯ãããŠãããªããžã§ã¯ããžã®åç
§ãäœæããŸãããã®åç
§ã¯ãåç
§ã®ææè
ãå®éã®ãªããžã§ã¯ããæã£ãŠãããã®ããã«ãåç
§å
ã®ãªããžã§ã¯ãã®ãã£ãŒã«ãã®èªã¿åããã¡ãœããã®åŒã³åºãã«äœ¿çšãããŸãã
ããã¯ãã£ãŒã«ããšã¡ãœãããžã®ã¢ã¯ã»ã¹ã®ã¿ãèš±å¯ãããã®ã§ããããšã«æ³šæããŠãã ãããå ã®ãªããžã§ã¯ãã®çŽæ¥çãªã³ããŒã移åããŸãã¯ä¿®æ£ã¯èš±å¯ãããŸããã
ãã®ãã©ã³ã¶ã¯ã·ã§ã³ã§äœãèµ·ãã£ãŠããã®ããå解ããŠã¿ãŸãããã
ãŸãã/storage/
å
ã®ãã©ã€ããŒããªHelloAsset
ãªããžã§ã¯ãã«å¯ŸããCapabilityãçºè¡ããŸãã
let capability = account.capabilities.storage.issue<&HelloWorld.HelloAsset>(/storage/HelloAssetTutorial)
Capabilityãäœæããã«ã¯ãAccount.capabilities.issue()
ã¡ãœããã䜿çšããŠãã¹ãã¬ãŒãžå
ã®ãªããžã§ã¯ãã«æ°ããCapabilityãçºè¡ããŸãã <>
ã«å«ãŸããåã¯ããã®Capabilityãè¡šãåç
§åã§ãã ããã¯ããã®Capabilityããåç
§ãåçšãã人ã¯èª°ã§ãã<>
ã®åãšæš©éã«ãã£ãŠæå®ããããã£ãŒã«ããšã¡ãœããã«ã¢ã¯ã»ã¹ã§ããããšãæå³ããŸã æå®ãããåã¯ããªã³ã¯å
ã®ãªããžã§ã¯ãã®åã®ãµãã¿ã€ãã§ãªããã°ãªããŸãããã€ãŸãããªã³ã¯å
ã®ãªããžã§ã¯ãã«ãªããã£ãŒã«ããé¢æ°ã¯å«ãŸããŠããªããšããããšã§ãã
åç
§ã¯ã&
èšå·ã§åç
§ãããŸããããã§ã¯ããã®Capabilityã¯HelloAsset
ãªããžã§ã¯ããåç
§ããã®ã§ã<&HelloWorld.HelloAsset>
ãåãšããŠæå®ããHelloAsset
ãªããžã§ã¯ãã®ãã¹ãŠã«ã¢ã¯ã»ã¹ã§ããããã«ããŸã
issue
é¢æ°ã®åŒæ°ã«ã¯ããªã³ã¯å
ã®ã¹ãã¬ãŒãžå
ã®ãªããžã§ã¯ããžã®ãã¹(è£è¶³: ã€ãŸããã€ã³ã¿ãæãå
ã®ã€ã³ã¹ã¿ã³ã¹ã®å Žæ)ãæå®ããŸããæ©èœ(Capability)ãçºè¡ããããšãAccount.Capabilities
å
ã«ãã®æ©èœ(Capability)çšã®Capability ã³ã³ãããŒã©ãäœæãããæ©èœ(Capability)ã®äœæè
ã¯æ©èœ(Capability)ã«å¯ŸããŠãã现ããå¶åŸ¡ãè¡ãããšãã§ããŸãã
æ©èœ(Capability)ã¯éåžžã/storage/
ãã¡ã€ã³å
ã®ãªããžã§ã¯ãã«ãªã³ã¯ãããŸãããAccount
ãªããžã§ã¯ãçšã«äœæããããšãã§ããŸããAccount Capabilityã«ã€ããŠã¯ããã®ãã¥ãŒããªã¢ã«ã§ã¯åãäžããŸããã
æ©èœ(Capability)ãèšå®ããåŸãã©ããã«ä¿åãããããã®å Žåã¯account.capabilities.publish()
ã¡ãœããã䜿çšããŠã¢ã«ãŠã³ãã®ãããªãã¯ã»ã¯ã·ã§ã³ã«ä¿åããŸããåŒã³åºãåŽã¯ãä¿åããæ©èœ(Capability)ãšä¿åå
ãšãªããããªãã¯ãã¹ãæå®ããŸãã
æ©èœ(Capability)ãããªããžã§ã¯ããåç
§ããã«ã¯ãæ©èœ(Capability)ã®borrow
ã¡ãœããã䜿çšããŸãã
let helloReference = capability.borrow()
?? panic("Could not borrow a reference to the hello capability")
ãã®ã¡ãœããã¯ãissue
é¢æ°ã®äžã§<>
ã§æå®ããåãšããŠåç
§ãäœæããŸããåç
§ãåçšããŠããéãåç
§ã®åçšã倱æããå¯èœæ§ãããããããªãã·ã§ãã«ãã§ã€ãã³ã°(optional chaining)ã䜿çšããŸããåç
§ã¯ã察象ã®ã¹ãã¬ãŒãžã¹ãããã空ã®å Žåããã§ã«åçšãããŠããå ŽåããŸãã¯èŠæ±ãããåãæ©èœ(Capability)ã«ãã£ãŠèš±å¯ãããŠãããã®ãã倧ããå Žåãnil
ã«ãªãå¯èœæ§ããããŸããåŒã³åºãå
ãåé¡ãããæ£ç¢ºã«ææ¡ã§ããããã«ããããããããšã©ãŒã¡ãã»ãŒãžã衚瀺ããŠãããã¯(panic)ãèµ·ãããŸãã
ããã«ããªãœãŒã¹ãªããžã§ã¯ã(ã€ã³ã¹ã¿ã³ã¹)ã®ææè
ã¯ãä¿åæã«äœæãããæ©èœ(Capability)ã«å¯ŸããŠãCapability Controllerã®delete
ã¡ãœããã䜿çšããããšã§ãèªèº«ãäœæããæ©èœ(Capability)ãå¹æçã«åãæ¶ãããšãã§ããŸãã
ããã«ãã¹ãã¬ãŒãžå ã®åç §ãªããžã§ã¯ãïŒãªãœãŒã¹ã®ã€ã³ã¹ã¿ã³ã¹ïŒã移åãããå Žåããã®ã¹ãã¬ãŒãžã®ãã¹ããäœæãããæ©èœ(Capability)ã¯ç¡å¹ã«ãªããŸãã
èšèªãªãã¡ã¬ã³ã¹ã«ã¯ãæ©èœ(Capability)ã«é¢ãããã詳现ãªããã¥ã¡ã³ãããããŸãã
ããã§ã/public/Hello
ã§ãããªãã¯ãªCapabilityã䜿ã£ãŠåç
§ãåçšããã°ã誰ã§ãHelloAsset
ãªããžã§ã¯ãã®hello()
ã¡ãœãããåŒã³åºããããã«ãªããŸãã!ïŒæ¬¡ã®ã»ã¯ã·ã§ã³ã§èª¬æããŸãïŒ
æåŸã«ãåçšããåç
§ã䜿ã£ãŠhello()
ã¡ãœãããåŒã³åºããŸãã
// Call the hello function using the reference to the HelloAsset resource
log(helloReference.hello())
ãã©ã³ã¶ã¯ã·ã§ã³å®è¡ã®çµäºæã«ã¯ãhelloReference
ã®å€ã¯å€±ãããŸãããå®éã®ãªãœãŒã¹ãã®ãã®ã§ã¯ãªãããã倱ãããŠãåé¡ãããŸããã
次ã®ã»ã¯ã·ã§ã³ã§ã¯ãæ©èœ(Capability)ãã¹ã¯ãªããã®ã¢ã«ãŠã³ããžã®ã¢ã¯ã»ã¹ãã©ã®ããã«æ¡åŒµã§ããããèŠãŠãããŸãã
Executing Scripts
ã¹ã¯ãªããã¯ãCadenceã«ãããéåžžã«ã·ã³ãã«ãªãã©ã³ã¶ã¯ã·ã§ã³åã§ããããããã¯ãã§ãŒã³ãžã®æžã蟌ã¿ã¯å®è¡ã§ãããã¢ã«ãŠã³ããŸãã¯ã¹ããŒãã³ã³ãã©ã¯ãã®ç¶æ ãèªã¿åãã®ã¿ã§ãã
ã¹ã¯ãªãããå®è¡ããã«ã¯ãaccess(all) fun main()
ãšåŒã°ããé¢æ°ãèšè¿°ããŸããã¹ã¯ãªãããå®è¡ããã«ã¯ã[Execute Script] ãã¿ã³ãã¯ãªãã¯ããŸããã¹ã¯ãªããã®çµæã¯ã³ã³ãœãŒã«åºåã«è¡šç€ºãããŸãã
ACTION
Get Greeting
ã®ãã¡ã€ã« ãéããŸãã
Get Greeting
ã¯ä»¥äžã®ããã«ãªã£ãŠããã¯ãã§ãã
import HelloWorld from 0x06
access(all) fun main(): String {
// Cadence code can get an account's public account object
// by using the getAccount() built-in function.
let helloAccount = getAccount(0x06)
// Borrow the public capability from the public path of the owner's account
let helloReference = helloAccount.capabilities
.borrow<&HelloWorld.HelloAsset>(/public/HelloAssetTutorial)
?? panic("Could not borrow a reference to the HelloAsset capability")
// The log built-in function logs its argument to stdout.
//
return helloReference.hello()
}
ãã®ã¹ã¯ãªããã®åäœã¯ä»¥äžã®éã:
-
getAccount
ã䜿çšããŠãããªãã¯ãªAccount
åç §ãååŸãããããå€æ°helloAccount
ã«å²ãåœãŠãŸãã -
Create Link
ãã©ã³ã¶ã¯ã·ã§ã³ã§åŸãCapabilityããborrow
ã¡ãœããã䜿çšããŠåç §ãåçšãããããå€æ°helloReference
ã«å²ãåœãŠãŸãã -
helloReference
ããhello()
é¢æ°ã®çµæãåŒã³åºãå ã«è¿ããŸãã
let helloAccount = getAccount(0x06)
&Account
åç
§(reference)ã¯ããããã¯ãŒã¯äžã®ãã¹ãŠã®ãŠãŒã¶ãŒãå©çšã§ããŸãããã¢ã«ãŠã³ãã®/public/
ãã¡ã€ã³ããèªã¿åããé¢æ°ã®ã¿ã«ã¢ã¯ã»ã¹ã§ããŸãã
ãããŠãã¹ã¯ãªããã¯Create Link
ã§äœæãããæ©èœ(Capability)ãæå(borrow)ããŸãã
// Borrow the public capability from the public path of the owner's account
let helloReference = helloAccount.capabilities
.borrow<&HelloWorld.HelloAsset>(/public/HelloAssetTutorial)
?? panic("Could not borrow a reference to the HelloAsset capability")
ã¢ã«ãŠã³ãã«ä¿åãããŠããæ©èœ(Capability)ãå©çšããã«ã¯ãaccount.capabilities.borrow()
é¢æ°ã䜿çšããŸããborrow()
ã¯ãæ©èœ(Capability)ã®å¯Ÿè±¡ãšãªãã¹ãã¬ãŒãžãªããžã§ã¯ããžã®åç
§ãè¿ããŸããæ©èœ(Capability)ãååšããªãå Žåãæ©èœ(Capability)ã®å¯Ÿè±¡ãšãªãã¹ãã¬ãŒãžãã¹ã«å€ãä¿åãããŠããªãå ŽåããŸãã¯æå®ãããåã§å€ãåçšã§ããªãå Žåãborrow
ã¯nil
ãè¿ããŸãã
ãã®åŸãã¹ã¯ãªããã¯åç
§ã䜿çšããŠhello()
é¢æ°ãåŒã³åºããçµæãè¿ããŸãã
ã¹ã¯ãªãããå®è¡ããŠãæ£ããåäœããããšã確èªããŠã¿ãŸãããã
ACTION
ãã¬ã€ã°ã©ãŠã³ã(Playground)ã®Execute
ãã¿ã³ãã¯ãªãã¯ããŸãã
ãã®ãããªåºåçµæã«ãªãã¯ãã§ãã
> Result > "Hello, World"
ããã§ããŸããïŒ ã¹ã¯ãªããã¯æ£åžžã«å®è¡ãããŸããã
ã¹ã¯ãªããã®æ¬åœã«çŽ æŽãããæ©èœã®ã²ãšã€ã¯ããªã³ãã§ãŒã³ã®ãã®ãå®éã«å€æŽã§ããªãã«ãããããããããããã¢ã«ãŠã³ãã®ãã©ã€ããŒãã¹ãã¬ãŒãžããªããžã§ã¯ãã«ã¢ã¯ã»ã¹ã§ããããšã§ããããã«ãããã¹ã¯ãªããã¯ãã§ãŒã³ã®å®å šãªç¶æ (full state)ãææ¡åãããã«é«ãŸãããŸããå®éã«å€æŽãå ããããšãã§ããªããããå®å šæ§ã確ä¿ãããŸãããŸãããªã³ãã§ãŒã³ã®ãã®ã¯ãã¹ãŠå ¬éãããŠããããããããã¯ãã§ãŒã³ããã°ã©ãã³ã°èšèªã«ãšã£ãŠãããã¯è«ççãªæ©èœã§ãã
ã¹ã¯ãªããã¯ãçµã¿èŸŒã¿ã®getAuthAccount()
é¢æ°ã䜿çšããŠãã¢ã«ãŠã³ãã®ã¢ãã¬ã¹ã®&Account
åç
§ãååŸã§ããŸãã
view fun getAuthAccount<T: &Account>(_ address: Address): &T
åŒã³åºãåŽã¯ãã¢ã¯ã»ã¹ãããã¢ã«ãŠã³ãã®ã©ã®éšåã«å¯ŸããŠãã©ã®æš©é(Entitlement)ãå¿
èŠãšãããã<>
ã«æå®ããå¿
èŠããããŸãã以äžã«äŸã瀺ããŸãã
access(all) fun main(address: Address) {
let entitledAccount = getAuthAccount<auth(BorrowValue) &Account>(address)
}
ã¢ã«ãŠã³ãã®è©³çŽ°ã«ã€ããŠã¯ãèšèªãªãã¡ã¬ã³ã¹ãåç §ããŠãã ããã
Reviewing Capabilities
ãã®ãã¥ãŒããªã¢ã«ã§ã¯ãCadenceã®ãªãœãŒã¹ã®èããåºããæ©èœ(Capability)ã䜿çšããŠãªãœãŒã¹ãžã®ã¢ã¯ã»ã¹ç¯å²ãæ¡å€§ããããå€ãã®ã¢ã«ãŠã³ãã¹ãã¬ãŒãžAPIã®ãŠãŒã¹ã±ãŒã¹ãã«ããŒããŸããã
ãªãœãŒã¹ãå«ãã ã¹ããŒãã³ã³ãã©ã¯ãããããã€ãããã®ãªãœãŒã¹ãžã®ã¢ã¯ã»ã¹ãèš±å¯ããæ©èœ(Capability)ãäœæããŸããããã®æ©èœ(Capability)ã䜿çšããŠãborrowã¡ãœããã§åç
§ãäœæãããã®åç
§ã䜿çšããŠãªãœãŒã¹ã®hello()
é¢æ°ãåŒã³åºããŸãããæåŸã«ãã¹ã¯ãªããã䜿çšããŠåãæ©èœ(Capability)ã®åçšãšåç
§ã®äœæãè¡ããã¹ã¯ãªãããããªãœãŒã¹ã®hello()
é¢æ°ãåŒã³åºããããã«ããŸãããããã¯éèŠãªããšã§ãããªããªããæ©èœ(Capability)ã䜿çšããã«ã¹ã¯ãªããããã¢ã«ãŠã³ãã¹ãã¬ãŒãžã«ã¢ã¯ã»ã¹ããããšã¯ã§ããªãããã§ãã
ãã¥ãŒããªã¢ã«ãå®äºããã®ã§ã以äžã®ããšãåºæ¥ãã·ã³ãã«ãªCadenceããã°ã©ã ãæžãããã®åºæ¬çãªç¥èãåŸãããŸããã
- ã¹ããŒãã³ã³ãã©ã¯ãã«ãªãœãŒã¹ãå®è£ ãã
- ã¢ã«ãŠã³ãã®ãªãœãŒã¹ãžã®ã¢ã¯ã»ã¹ãèš±å¯ããæ©èœ(Capability)ãäœæãã
- 眲åæžã¿ãã©ã³ã¶ã¯ã·ã§ã³ãšã¹ã¯ãªããã®äž¡æ¹ã䜿çšããŠãªãœãŒã¹ãšããåããã
ã¹ããŒãã³ã³ãã©ã¯ããèªç±ã«å€æŽããŠãç°ãªããªãœãŒã¹ãäœæããããå©çšå¯èœãªã¢ã«ãŠã³ãã¹ãã¬ãŒãžAPIãè©Šããããã¹ããŒãã³ã³ãã©ã¯ãããç°ãªãé¢æ°ãå®è¡ããæ°ãããã©ã³ã¶ã¯ã·ã§ã³ãã¹ã¯ãªãããèšè¿°ãããããããšãã§ããããã«ãªããŸããã CapabilityããŒã¹ã®ã¢ã¯ã»ã¹å¶åŸ¡ããŒãžãåç §ããŠãCapabilityã§ã§ããããšã«ã€ããŠããã«è©³ãã確èªããŠãã ããã
Cadence ã䜿çšããŠããè€éãªã¢ããªã±ãŒã·ã§ã³ãæ§ç¯ããæ¹åæ§ã¯æ£ããã®ã§ãã¢ããªã±ãŒã·ã§ã³ãè€éåããŠããä»ãããCadence Best Practices ããã¥ã¡ã³ããšAnti-patterns ããã¥ã¡ã³ãã確èªãã絶奜ã®ã¿ã€ãã³ã°ã§ãã
翻蚳å ->https://cadence-lang.org/docs/tutorial/capabilities
Previous << 3. Resource Contract Tutorial
Flow Blockchainã®Cadence version1.0ããã¥ã¡ã³ã (4. Capability Tutorial)