예제 #1
0
def create_account_template(
    *,
    keys: list[AccountKey],
    reference_block_id: bytes = None,
    payer: cadence.Address = None,
    proposal_key: ProposalKey = None,
    contracts: dict[Annotated[str, "name"], Annotated[str, "source"]] = None
) -> Tx:
    if keys:
        cadence_public_keys = cadence.Array(
            [cadence.String(k.hex()) for k in keys])
    else:
        cadence_public_keys = cadence.Array([])
    if contracts:
        cadence_contracts = cadence.Dictionary([
            cadence.KeyValuePair(cadence.String(k),
                                 cadence.String(v.encode("utf-8").hex()))
            for (k, v) in contracts.items()
        ])
    else:
        cadence_contracts = cadence.Dictionary([])

    tx = (Tx(
        code="""
                transaction(publicKeys: [String], contracts:{String: String}) {
                    prepare(signer: AuthAccount) {
                        let acct = AuthAccount(payer: signer)
    
                        for key in publicKeys {
                            acct.addPublicKey(key.decodeHex())
                        }
    
                        for contract in contracts.keys {
                            acct.contracts.add(name: contract, code: contracts[contract]!.decodeHex())
                        }
                    }
                }
            """,
        reference_block_id=reference_block_id,
        payer=payer,
        proposal_key=proposal_key,
    ).add_arguments(cadence_public_keys).add_arguments(cadence_contracts))

    return tx
예제 #2
0
    async def run(self, ctx: Config):
        async with flow_client(host=ctx.access_node_host,
                               port=ctx.access_node_port) as client:
            pub_key, _ = random_key_pair(SignAlgo.ECDSA_secp256k1)

            # prepare parameters
            account_key = AccountKey(
                public_key=pub_key,
                sign_algo=SignAlgo.ECDSA_secp256k1,
                hash_algo=HashAlgo.SHA3_256,
            )
            account_key_hex = cadence.String(account_key.hex())
            cadence_public_keys = cadence.Array([account_key_hex])
            cadence_contracts = cadence.Dictionary([])

            block = await client.get_latest_block()
            proposer = await client.get_account_at_latest_block(
                address=ctx.service_account_address.bytes)

            tx = (Tx(
                code="""
                    transaction(publicKeys: [String], contracts:{String: String}) {
                        prepare(signer: AuthAccount) {
                            let acct = AuthAccount(payer: signer)
                    
                            for key in publicKeys {
                                acct.addPublicKey(key.decodeHex())
                            }
                    
                            for contract in contracts.keys {
                                acct.contracts.add(name: contract, code: contracts[contract]!.decodeHex())
                            }
                        }
                    }
                """,
                reference_block_id=block.id,
                payer=ctx.service_account_address,
                proposal_key=ProposalKey(
                    key_address=ctx.service_account_address,
                    key_id=ctx.service_account_key_id,
                    key_sequence_number=proposer.keys[
                        ctx.service_account_key_id].sequence_number,
                ),
            ).add_authorizers(
                ctx.service_account_address).add_arguments(cadence_public_keys)
                  .add_arguments(cadence_contracts).with_payload_signature(
                      ctx.service_account_address, 0,
                      ctx.service_account_signer).with_envelope_signature(
                          ctx.service_account_address, 0,
                          ctx.service_account_signer))

            await client.execute_transaction(tx)
예제 #3
0
    def testEncodeDictionary(self):
        simple_dict = _EncodeTestParams(
            "Simple",
            cadence.Dictionary([
                cadence.KeyValuePair(
                    cadence.String("a"),
                    cadence.Int(1),
                ),
                cadence.KeyValuePair(
                    cadence.String("b"),
                    cadence.Int(2),
                ),
                cadence.KeyValuePair(
                    cadence.String("c"),
                    cadence.Int(3),
                ),
            ]),
            """{"type":"Dictionary","value":[
                {"key":{"type":"String","value":"a"},"value":{"type":"Int","value":"1"}},
                {"key":{"type":"String","value":"b"},"value":{"type":"Int","value":"2"}},
                {"key":{"type":"String","value":"c"},"value":{"type":"Int","value":"3"}}
            ]}""",
        )

        nested_dict = _EncodeTestParams(
            "Nested",
            cadence.Dictionary([
                cadence.KeyValuePair(
                    cadence.String("a"),
                    cadence.Dictionary([
                        cadence.KeyValuePair(
                            cadence.String("1"),
                            cadence.Int(1),
                        ),
                    ]),
                ),
                cadence.KeyValuePair(
                    cadence.String("b"),
                    cadence.Dictionary([
                        cadence.KeyValuePair(
                            cadence.String("2"),
                            cadence.Int(2),
                        ),
                    ]),
                ),
                cadence.KeyValuePair(
                    cadence.String("c"),
                    cadence.Dictionary([
                        cadence.KeyValuePair(
                            cadence.String("3"),
                            cadence.Int(3),
                        ),
                    ]),
                ),
            ]),
            """
            {
                "type": "Dictionary",
                "value": [
                {
                  "key": { "type": "String", "value": "a" },
                  "value": {
                    "type": "Dictionary",
                    "value": [
                      {
                        "key": { "type": "String", "value": "1" },
                        "value": { "type": "Int", "value": "1" }
                      }
                    ]
                  }
                },
                {
                  "key": { "type": "String", "value": "b" },
                  "value": {
                    "type": "Dictionary",
                    "value": [
                      {
                        "key": { "type": "String", "value": "2" },
                        "value": { "type": "Int", "value": "2" }
                      }
                    ]
                  }
                },
                {
                  "key": { "type": "String", "value": "c" },
                  "value": {
                    "type": "Dictionary",
                    "value": [
                      {
                        "key": { "type": "String", "value": "3" },
                        "value": { "type": "Int", "value": "3" }
                      }
                    ]
                  }
                }
                ]
            }
            """,
        )

        resource_dict = _EncodeTestParams(
            "Resources",
            cadence.Dictionary([
                cadence.KeyValuePair(
                    cadence.String("a"),
                    cadence.Resource([cadence.Int(1)], _foo_resource_type),
                ),
                cadence.KeyValuePair(
                    cadence.String("b"),
                    cadence.Resource([cadence.Int(2)], _foo_resource_type),
                ),
                cadence.KeyValuePair(
                    cadence.String("c"),
                    cadence.Resource([cadence.Int(3)], _foo_resource_type),
                ),
            ]),
            """
                {
                  "type": "Dictionary",
                  "value": [
                    {
                      "key": { "type": "String", "value": "a" },
                      "value": {
                        "type": "Resource",
                        "value": {
                          "id": "S.test.Foo",
                          "fields": [
                            { "name": "bar", "value": { "type": "Int", "value": "1" } }
                          ]
                        }
                      }
                    },
                    {
                      "key": { "type": "String", "value": "b" },
                      "value": {
                        "type": "Resource",
                        "value": {
                          "id": "S.test.Foo",
                          "fields": [
                            { "name": "bar", "value": { "type": "Int", "value": "2" } }
                          ]
                        }
                      }
                    },
                    {
                      "key": { "type": "String", "value": "c" },
                      "value": {
                        "type": "Resource",
                        "value": {
                          "id": "S.test.Foo",
                          "fields": [
                            { "name": "bar", "value": { "type": "Int", "value": "3" } }
                          ]
                        }
                      }
                    }
                  ]
                }
            """,
        )

        self._encodeAndDecodeAll([
            simple_dict,
            nested_dict,
            resource_dict,
        ])