{"id":13523485,"url":"https://github.com/rocklabs-io/ic-py","last_synced_at":"2025-04-01T00:31:54.466Z","repository":{"id":37130053,"uuid":"428586610","full_name":"rocklabs-io/ic-py","owner":"rocklabs-io","description":"Python Agent Library for the DFINITY Internet Computer","archived":false,"fork":false,"pushed_at":"2024-06-15T09:36:03.000Z","size":4672,"stargazers_count":131,"open_issues_count":19,"forks_count":30,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-03-07T12:03:45.490Z","etag":null,"topics":["agent","dfinity","ic","internet-computer","python","sdk"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rocklabs-io.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-11-16T09:08:00.000Z","updated_at":"2025-02-25T06:19:21.000Z","dependencies_parsed_at":"2024-06-19T00:09:43.267Z","dependency_job_id":"d967adb0-c8ca-4a54-a34a-7fc0c9b35d6d","html_url":"https://github.com/rocklabs-io/ic-py","commit_stats":{"total_commits":169,"total_committers":11,"mean_commits":"15.363636363636363","dds":0.6153846153846154,"last_synced_commit":"53c375a1d6c1d09e8d24588142dece550b801cef"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rocklabs-io%2Fic-py","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rocklabs-io%2Fic-py/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rocklabs-io%2Fic-py/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rocklabs-io%2Fic-py/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rocklabs-io","download_url":"https://codeload.github.com/rocklabs-io/ic-py/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246563353,"owners_count":20797443,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["agent","dfinity","ic","internet-computer","python","sdk"],"created_at":"2024-08-01T06:01:00.565Z","updated_at":"2025-04-01T00:31:53.885Z","avatar_url":"https://github.com/rocklabs-io.png","language":"Python","funding_links":[],"categories":["Client Libraries (Agents)","CDK\u0026Agent"],"sub_categories":["Python"],"readme":"## Python Agent Library for the Internet Computer\n\n![ic-py](./pics/ic-py.png)\n\n`ic-py` provides basic modules to interact with canisters on the DFINITY Internet Computer.\n\n### Install\n\n```\npip3 install ic-py\n```\n\n### Features\n\n1. candid types encode \u0026 decode\n2. support secp256k1 \u0026 ed25519 identity, pem file import\n3. canister DID file parsing\n4. canister class, initialized with canister id and DID file\n5. common canister interfaces: ledger, management, nns, cycles wallet\n6. async support\n\n### Modules \u0026 Usage\n\n#### 1. Principal\n\nCreate an instance:\n\n```python\nfrom ic.principal import Principal\np = Principal() # default is management canister id `aaaaa-aa`\np1 = Principal(bytes=b'') # create an instance from bytes\np2 = Principal.anonymous() # create anonymous principal\np3 = Principal.self_authenticating(pubkey) # create a principal from public key\np4 = Principal.from_str('aaaaa-aa') # create an instance from string\np5 = Principal.from_hex('xxx') # create an instance from hex\n```\n\nClass methods:\n\n```python\np.bytes # principal bytes\np.len # byte array length\np.to_str() # convert to string\n```\n\n#### 2. Identity\n\nCreate an instance:\n\n```python\nfrom ic.identity import Identity\ni = Identity() # create an identity instance, key is randomly generated\ni1 = Identity(privkey = \"833fe62409237b9d62ec77587520911e9a759cec1d19755b7da901b96dca3d42\") # create an instance from private key\n```\n\nSign a message:\n\n```python\nmsg = b”ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f“\nsig = i.sign(msg) # sig = (der_encoded_pubkey, signature)\n```\n\n#### 3. Client\n\nCreate an instance:\n\n```python\nfrom ic.client import Client\nclient = Client(url = \"https://ic0.app\")\n```\n\n#### 4. Candid\n\nEncode parameters:\n\n```python\nfrom ic.candid import encode, decode, Types\n# params is an array, return value is encoded bytes\nparams = [{'type': Types.Nat, 'value': 10}]\ndata = encode(params)\n```\n\nDecode parameters:\n\n```python\n# data is bytes, return value is an parameter array\nparams = decode(data)\n```\n\n#### 5. Agent\n\nCreate an instance:\n\n```python\nfrom ic.client import Client\nfrom ic.identity import Identity\nfrom ic.agent import Agent\n# Identity and Client are dependencies of Agent\niden = Identity()\nclient = Client()\nagent = Agent(iden, client)\n```\n\nQuery call:\n\n```python\n# query the name of token canister `gvbup-jyaaa-aaaah-qcdwa-cai`\nname = agent.query_raw(\"gvbup-jyaaa-aaaah-qcdwa-cai\", \"name\", encode([]))\n```\n\nUpdate call:\n\n```python\n# transfer 100 token to blackhole address `aaaaa-aa`\nparams = [\n\t{'type': Types.Principal, 'value': 'aaaaa-aa'},\n\t{'type': Types.Nat, 'value': 10000000000}\n]\nresult = agent.update_raw(\"gvbup-jyaaa-aaaah-qcdwa-cai\", \"transfer\", encode(params))\n```\n\n#### 6. Canister\n\nCreate a canister instance with candid interface file and canister id, and call canister method with canister instance:\n\n```python\nfrom ic.canister import Canister\nfrom ic.client import Client\nfrom ic.identity import Identity\nfrom ic.agent import Agent\nfrom ic.candid import Types\n\niden = Identity()\nclient = Client()\nagent = Agent(iden, client)\n# read governance candid from file\ngovernance_did = open(\"governance.did\").read()\n# create a governance canister instance\ngovernance = Canister(agent=agent, canister_id=\"rrkah-fqaaa-aaaaa-aaaaq-cai\", candid=governance_did)\n# call canister method with instance\nres = governance.list_proposals(\n    {\n        'include_reward_status': [],\n        'before_proposal': [],\n        'limit': 100,\n        'exclude_topic': [],\n        'include_status': [1]\n    }\n)\n```\n\n### 7. Async request\n\nic-py also supports async requests:\n\n```python\nimport asyncio\nfrom ic.canister import Canister\nfrom ic.client import Client\nfrom ic.identity import Identity\nfrom ic.agent import Agent\nfrom ic.candid import Types\n\niden = Identity()\nclient = Client()\nagent = Agent(iden, client)\n# read governance candid from file\ngovernance_did = open(\"governance.did\").read()\n# create a governance canister instance\ngovernance = Canister(agent=agent, canister_id=\"rrkah-fqaaa-aaaaa-aaaaq-cai\", candid=governance_did)\n# async call\nasync def async_test():\n  res = await governance.list_proposals_async(\n    {\n        'include_reward_status': [], \n        'before_proposal': [],\n        'limit': 100, \n        'exclude_topic': [], \n        'include_status': [1]\n    }\n  )\n  print(res)\nasyncio.run(async_test())\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frocklabs-io%2Fic-py","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frocklabs-io%2Fic-py","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frocklabs-io%2Fic-py/lists"}