Ejemplo n.º 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
Ejemplo n.º 2
0
    def testEncodeArray(self):
        empty_array = _EncodeTestParams(
            "Empty",
            cadence.Array([]),
            '{"type":"Array","value":[]}',
        )

        int_array = _EncodeTestParams(
            "Integers",
            cadence.Array([
                cadence.Int(1),
                cadence.Int(2),
                cadence.Int(3),
            ]),
            """{"type":"Array",
                "value":[{"type":"Int","value":"1"},{"type":"Int","value":"2"},{"type":"Int","value":"3"}]}""",
        )

        resource_array = _EncodeTestParams(
            "Resources",
            cadence.Array([
                cadence.Resource([cadence.Int(1)], _foo_resource_type),
                cadence.Resource([cadence.Int(2)], _foo_resource_type),
                cadence.Resource([cadence.Int(3)], _foo_resource_type),
            ]),
            """{"type":"Array","value":[
                {"type":"Resource","value":{"id":"S.test.Foo",
                    "fields":[{"name":"bar","value":{"type":"Int","value":"1"}}]}},
                {"type":"Resource","value":{"id":"S.test.Foo",
                    "fields":[{"name":"bar","value":{"type":"Int","value":"2"}}]}},
                {"type":"Resource","value":{"id":"S.test.Foo",
                    "fields":[{"name":"bar","value":{"type":"Int","value":"3"}}]}}
            ]}""",
        )

        self._encodeAndDecodeAll([
            empty_array,
            int_array,
            resource_array,
        ])
Ejemplo n.º 3
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)