github.com/holochain/holochain-proto@v0.1.0-alpha-26.0.20200915073418-5c83169c9b5b/app_package.go (about) 1 // Copyright (C) 2013-2018, The MetaCurrency Project (Eric Harris-Braun, Arthur Brock, et. al.) 2 // Use of this source code is governed by GPLv3 found in the LICENSE file 3 //---------------------------------------------------------------------------------------- 4 // AppPackage implements loading DNA and other app information from an AppPackage structure 5 6 package holochain 7 8 import ( 9 "io" 10 ) 11 12 const ( 13 AppPackageVersion = "0.0.1" 14 ) 15 16 type AppPackageUIFile struct { 17 FileName string 18 Data string 19 Encoding string 20 } 21 22 type AppPackageTests struct { 23 Name string 24 TestSet TestSet 25 } 26 27 type AppPackageScenario struct { 28 Name string 29 Roles []AppPackageTests 30 Config TestConfig 31 } 32 33 type AppPackage struct { 34 Version string 35 Generator string 36 DNA DNA 37 TestSets []AppPackageTests 38 UI []AppPackageUIFile 39 Scenarios []AppPackageScenario 40 } 41 42 // LoadAppPackage decodes DNA and other appPackage data from appPackage file (via an io.reader) 43 func LoadAppPackage(reader io.Reader, encodingFormat string) (appPackageP *AppPackage, err error) { 44 var appPackage AppPackage 45 err = Decode(reader, encodingFormat, &appPackage) 46 if err != nil { 47 return 48 } 49 appPackageP = &appPackage 50 appPackage.DNA.PropertiesSchema = `{ 51 "title": "Properties Schema", 52 "type": "object", 53 "properties": { 54 "description": { 55 "type": "string" 56 }, 57 "language": { 58 "type": "string" 59 } 60 } 61 } 62 ` 63 return 64 } 65 66 const ( 67 BasicTemplateAppPackageFormat = "yml" 68 ) 69 70 var BasicTemplateAppPackage string = `{ 71 # AppPackage Version 72 # The app package schema version of this file. 73 "Version": "` + AppPackageVersion + `", 74 "Generator": "holochain", 75 76 "DNA": { 77 # This is a holochain application package yaml definition. http://ceptr.org/projects/holochain 78 79 # DNA File Version 80 # Version indicator for changes to DNA 81 "Version": 1, 82 83 # DNA Unique ID 84 # This ID differentiates your app from others. For example, to tell one Slack team from another with same code, so change it! 85 "UUID": "00000000-0000-0000-0000-000000000000", 86 87 # Application Name 88 # What would you like to call your holochain app? 89 "Name": "templateApp", 90 91 # Requires Holochain Version 92 # Version indicator for which minimal version of holochain is required by this DNA 93 "RequiresVersion": ` + VersionStr + `, 94 95 # Properties 96 # Properties that you want available across all Zomes. 97 "Properties": { 98 99 # Application Description 100 # Briefly describe your holochain app. 101 "description": "provides an application template", 102 103 # Language 104 # The base (human) language of this holochain app. 105 "language": "en" 106 }, 107 108 # Properties Schema File 109 # Describes the entries in the Properties section of your dna file. 110 "PropertiesSchemaFile": "properties_schema.json", 111 112 # DHT Settings 113 # Configure the properties of your Distributed Hash Table (e.g. hash algorithm, neighborhood size, etc.). 114 "DHTConfig": { 115 "HashType": "sha2-256" 116 }, 117 118 # Zomes 119 # List the Zomes your application will support. 120 "Zomes": [ 121 { 122 123 # Zome Name 124 # The name of this code module. 125 "Name": "sampleZome", 126 127 # Zome Description 128 # What is the purpose of this module? 129 "Description": "provide a sample zome", 130 131 # Ribosome Type 132 # What scripting language will you code in? 133 "RibosomeType": "js", 134 135 # Zome Entries 136 # Data stored and tracked by your Zome. 137 "Entries": [ 138 { 139 "Name": "sampleEntry", # The name of this entry. 140 "Required": true, # Is this entry required? 141 "DataFormat": "json", # What type of data should this entry store? 142 "Sharing": "public", # Should this entry be publicly accessible? 143 "Schema": "{\n \"title\": \"sampleEntry Schema\",\n \"type\": \"object\",\n \"properties\": {\n \"content\": {\n \"type\": \"string\"\n },\n \"timestamp\": {\n \"type\": \"integer\"\n }\n },\n \"required\": [\"content\", \"timestamp\"]\n}" 144 } 145 ], 146 147 # Zome Functions 148 # Functions which can be called in your Zome's API. 149 "Functions": [ 150 { 151 "Name": "sampleEntryCreate", # The name of this function. 152 "CallingType": "json", # Data format for parameters passed to this function. 153 "Exposure": "public" # Level to which is this function exposed. 154 }, 155 { 156 "Name": "sampleEntryRead", # The name of this function. 157 "CallingType": "json", # Data format for parameters passed to this function. 158 "Exposure": "public" # Level to which is this function exposed. 159 }, 160 { 161 "Name": "doSampleAction", # The name of this function. 162 "CallingType": "json", # Data format for parameters passed to this function. 163 "Exposure": "public" # Level to which is this function exposed. 164 } 165 ], 166 167 # Zome Source Code 168 # The logic that will control Zome behavior 169 "Code": "/*******************************************************************************\n * Utility functions\n ******************************************************************************/\n\n/**\n * Is this a valid entry type?\n *\n * @param {any} entryType The data to validate as an expected entryType.\n * @return {boolean} true if the passed argument is a valid entryType.\n */\nfunction isValidEntryType (entryType) {\n // Add additonal entry types here as they are added to dna.json.\n return [\"sampleEntry\"].includes(entryType);\n}\n\n/**\n * Returns the creator of an entity, given an entity hash.\n *\n * @param {string} hash The entity hash.\n * @return {string} The agent hash of the entity creator.\n */\nfunction getCreator (hash) {\n return get(hash, { GetMask: HC.GetMask.Sources })[0];\n}\n\n/*******************************************************************************\n * Required callbacks\n ******************************************************************************/\n\n/**\n * System genesis callback: Can the app start?\n *\n * Executes just after the initial genesis entries are committed to your chain\n * (1st - DNA entry, 2nd Identity entry). Enables you specify any additional\n * operations you want performed when a node joins your holochain.\n *\n * @return {boolean} true if genesis is successful and so the app may start.\n *\n * @see https://developer.holochain.org/API#genesis\n */\nfunction genesis () {\n return true;\n}\n\n/**\n * Validation callback: Can this entry be committed to a source chain?\n *\n * @param {string} entryType Type of the entry as per DNA config for this zome.\n * @param {string|object} entry Data with type as per DNA config for this zome.\n * @param {Header-object} header Header object for this entry.\n * @param {Package-object|null} pkg Package object for this entry, if exists.\n * @param {string[]} sources Array of agent hashes involved in this commit.\n * @return {boolean} true if this entry may be committed to a source chain.\n *\n * @see https://developer.holochain.org/API#validateCommit_entryType_entry_header_package_sources\n * @see https://developer.holochain.org/Validation_Functions\n */\nfunction validateCommit (entryType, entry, header, pkg, sources) {\n return isValidEntryType(entryType);\n}\n\n/**\n * Validation callback: Can this entry be committed to the DHT on any node?\n *\n * It is very likely that this validation routine should check the same data\n * integrity as validateCommit, but, as it happens during a different part of\n * the data life-cycle, it may require additional validation steps.\n *\n * This function will only get called on entry types with \"public\" sharing, as\n * they are the only types that get put to the DHT by the system.\n *\n * @param {string} entryType Type of the entry as per DNA config for this zome.\n * @param {string|object} entry Data with type as per DNA config for this zome.\n * @param {Header-object} header Header object for this entry.\n * @param {Package-object|null} pkg Package object for this entry, if exists.\n * @param {string[]} sources Array of agent hashes involved in this commit.\n * @return {boolean} true if this entry may be committed to the DHT.\n *\n * @see https://developer.holochain.org/API#validatePut_entryType_entry_header_package_sources\n * @see https://developer.holochain.org/Validation_Functions\n */\nfunction validatePut (entryType, entry, header, pkg, sources) {\n return validateCommit(entryType, entry, header, pkg, sources);\n}\n\n/**\n * Validation callback: Can this entry be modified?\n *\n * Validate that this entry can replace 'replaces' due to 'mod'.\n *\n * @param {string} entryType Type of the entry as per DNA config for this zome.\n * @param {string|object} entry Data with type as per DNA config for this zome.\n * @param {Header-object} header Header object for this entry.\n * @param {string} replaces The hash string of the entry being replaced.\n * @param {Package-object|null} pkg Package object for this entry, if exists.\n * @param {string[]} sources Array of agent hashes involved in this mod.\n * @return {boolean} true if this entry may replace 'replaces'.\n *\n * @see https://developer.holochain.org/API#validateMod_entryType_entry_header_replaces_package_sources\n * @see https://developer.holochain.org/Validation_Functions\n */\nfunction validateMod (entryType, entry, header, replaces, pkg, sources) {\n return validateCommit(entryType, entry, header, pkg, sources)\n // Only allow the creator of the entity to modify it.\n && getCreator(header.EntryLink) === getCreator(replaces);\n}\n\n/**\n * Validation callback: Can this entry be deleted?\n *\n * @param {string} entryType Name of the entry as per DNA config for this zome.\n * @param {string} hash The hash of the entry to be deleted.\n * @param {Package-object|null} pkg Package object for this entry, if exists.\n * @param {string[]} sources Array of agent hashes involved in this delete.\n * @return {boolean} true if this entry can be deleted.\n *\n * @see https://developer.holochain.org/API#validateDel_entryType_hash_package_sources\n * @see https://developer.holochain.org/Validation_Functions\n */\nfunction validateDel (entryType, hash, pkg, sources) {\n return isValidEntryType(entryType)\n // Only allow the creator of the entity to delete it.\n && getCreator(hash) === sources[0];\n}\n\n/**\n * Package callback: The package request for validateCommit() and valdiatePut().\n *\n * Both 'commit' and 'put' trigger 'validatePutPkg' as 'validateCommit' and\n * 'validatePut' must both have the same data.\n *\n * @param {string} entryType Name of the entry as per DNA config for this zome.\n * @return {PkgReq-object|null}\n * null if the data required is the Entry and Header.\n * Otherwise a \"Package Request\" object, which specifies what data to be sent\n * to the validating node.\n *\n * @see https://developer.holochain.org/API#validatePutPkg_entryType\n * @see https://developer.holochain.org/Validation_Packaging\n */\nfunction validatePutPkg (entryType) {\n return null;\n}\n\n/**\n * Package callback: The package request for validateMod().\n *\n * @param {string} entryType Name of the entry as per DNA config for this zome.\n * @return {PkgReq-object|null}\n * null if the data required is the Entry and Header.\n * Otherwise a \"Package Request\" object, which specifies what data to be sent\n * to the validating node.\n *\n * @see https://developer.holochain.org/API#validateModPkg_entryType\n * @see https://developer.holochain.org/Validation_Packaging\n */\nfunction validateModPkg (entryType) {\n return null;\n}\n\n/**\n * Package callback: The package request for validateDel().\n *\n * @param {string} entryType Name of the entry as per DNA config for this zome.\n * @return {PkgReq-object|null}\n * null if the data required is the Entry and Header.\n * Otherwise a \"Package Request\" object, which specifies what data to be sent\n * to the validating node.\n *\n * @see https://developer.holochain.org/API#validateDelPkg_entryType\n * @see https://developer.holochain.org/Validation_Packaging\n */\nfunction validateDelPkg (entryType) {\n return null;\n}" 170 } 171 ]}, 172 "TestSets":[{ 173 "Name":"sample", 174 "TestSet":{"Tests":[{"Convey":"Example: (which fails because we haven't actually implemented a sampleEntryCreate function) We can create a new sampleEntry","Zome":"sampleZome","FnName": "sampleEntryCreate","Input": {"content": "this is the entry body","stamp":12345},"Output":"\"%h1%\"","Exposure":"public"}]}} 175 ], 176 "UI":[ 177 {"FileName":"index.html", 178 "Data":"<html><body>Your UI here!</body></html>" 179 }, 180 {"FileName":"hc.js", 181 "Data":"function yourApp(){alert('your UI code here!')}" 182 }], 183 "Scenarios":[ 184 {"Name":"sampleScenario", 185 "Roles":[ 186 {"Name":"listener", 187 "TestSet":{"Tests":[ 188 {"Convey":"add listener test here"}]}}, 189 {"Name":"speaker", 190 "TestSet":{"Tests":[ 191 {"Convey":"add speaker test here"}]}}], 192 "Config":{"Duration":5,"GossipInterval":100}}] 193 } 194 `