コード例 #1
0
    def msg_deployed(self, message):
        print('msg_deployed method of the solana spl token')
        if self.contract.state != 'WAITING_FOR_DEPLOYMENT':
            take_off_blocking(self.contract.network.name)
            return
        else:
            conn = SolanaInt(self.contract.network.name).connect()
            key = Keypair.from_secret_key(bytes(SOLANA_KEYPAIR[0:32]))
            token_address = PublicKey(self.solana_contract.address)
            tok_int = Token(conn, token_address, TOKEN_PROGRAM_ID, key)
            holders = self.contract.tokenholder_set.all()
            if holders:
                print('transfering premint tokens')
                for th in holders:
                    holder_addr = PublicKey(th.address)
                    try:
                        associated_address = tok_int.create_associated_token_account(holder_addr)
                        print(f'created associated account {associated_address}')
                    except RPCException:
                        print('associated token account already created')
                        associated_address = get_associated_token_address(holder_addr, tok_int.pubkey)
                    response = tok_int.mint_to(associated_address, key, int(th.amount))
                    print(f'tx_hash = {response["result"]}')

            print('transferring of mint authority started')
            owner = PublicKey(self.admin_address)
            address = self.solana_contract.address
            tok_int.set_authority(address, key.public_key, 0, owner)
            print('successfully transferred mint authority')

            self.initialized({})
コード例 #2
0
    def deploy(self):
        print('deploying solana SPL token')
        conn = SolanaInt(self.contract.network.name).connect()
        owner = PublicKey(str(self.admin_address))
        key = Keypair.from_secret_key(bytes(SOLANA_KEYPAIR[0:32]))
        balance_needed = Token.get_min_balance_rent_for_exempt_for_mint(conn)
        token, txn, payer, mint_account, opts = Token._create_mint_args(conn, key, key.public_key, self.decimals,
                                                                        TOKEN_PROGRAM_ID,
                                                                        owner, False, balance_needed, Token)

        response = conn.send_transaction(txn, payer, mint_account, opts=opts)
        print(response)
        error = response['result']['meta']['err']
        if error:
            raise Exception(f'error while deploying \n {error}')
        else:
            tx_hash = response['result']['transaction']['signatures'][0]
            contract_address = response['result']['transaction']['message']['accountKeys'][1]
            solana_contract = SolanaContract()
            solana_contract.contract = self.contract
            solana_contract.original_contract = self.contract
            solana_contract.tx_hash = tx_hash
            solana_contract.address = contract_address
            solana_contract.save()
            self.solana_contract = solana_contract
            self.save()
            self.msg_deployed({})
コード例 #3
0
ファイル: test_keypair.py プロジェクト: michaelhly/solana-py
def test_create_from_secret_key() -> None:
    """Test creation with 64-byte secret key."""
    secret_key = base64.b64decode(
        "mdqVWeFekT7pqy5T49+tV12jO0m+ESW7ki4zSU9JiCgbL0kJbj5dvQ/PqcDAzZLZqzshVEs01d1KZdmLh4uZIg=="
    )
    keypair = Keypair.from_secret_key(secret_key)
    assert str(keypair.public_key) == "2q7pyhPwAwZ3QMfZrnAbDhnh9mDUqycszcpf86VgQxhF"
    assert keypair.secret_key == secret_key
コード例 #4
0
ファイル: SolanaNet.py プロジェクト: f80dev/elMoney
    def account(self, name):
        s=open("./PEM/"+name+".json","r").readlines()[0]
        ints=s.replace("[","").replace("]","").replace("\n","").split(",")

        rc=bytearray(64)
        for i in range(len(ints)):
            rc[i]=int(ints[i])

        rc=Keypair.from_secret_key(bytes(rc[0:32]))
        return rc
コード例 #5
0
def test_withdraw_from_vote_account():
    withdrawer_keypair = Keypair.from_secret_key(
        bytes(
            [
                134,
                123,
                27,
                208,
                227,
                175,
                253,
                99,
                4,
                81,
                170,
                231,
                186,
                141,
                177,
                142,
                197,
                139,
                94,
                6,
                157,
                2,
                163,
                89,
                150,
                121,
                235,
                86,
                185,
                22,
                1,
                233,
                58,
                133,
                229,
                39,
                212,
                71,
                254,
                72,
                246,
                45,
                160,
                156,
                129,
                199,
                18,
                189,
                53,
                143,
                98,
                72,
                182,
                106,
                69,
                29,
                38,
                145,
                119,
                190,
                13,
                105,
                157,
                112,
            ]
        )
    )
    vote_account_pubkey = PublicKey("CWqJy1JpmBcx7awpeANfrPk6AsQKkmego8ujjaYPGFEk")
    receiver_account_pubkey = PublicKey("A1V5gsis39WY42djdTKUFsgE5oamk4nrtg16WnKTuzZK")

    # solana withdraw-from-vote-account --dump-transaction-message \
    #   CWqJy1JpmBcx7awpeANfrPk6AsQKkmego8ujjaYPGFEk A1V5gsis39WY42djdTKUFsgE5oamk4nrtg16WnKTuzZK \
    # --authorized-withdrawer withdrawer.json \
    # 2 \
    # --blockhash Add1tV7kJgNHhTtx3Dgs6dhC7kyXrGJQZ2tJGW15tLDH \
    # --sign-only -k withdrawer.json
    wire_msg = base64.b64decode(
        b"AQABBDqF5SfUR/5I9i2gnIHHEr01j2JItmpFHSaRd74NaZ1wqxUGDtH5ah3TqEKWjcTmfHkpZC1h57NJL8Sx7Q6Olm2F2O70oOvzt1HgIVu+nySaSrWtJiK1eDacPPDWRxCwFgdhSB01dHS7fE12JOvTvbPYNV5z0RBD/A2jU4AAAAAAjxrQaMS7FjmaR++mvFr3XE6XbzMUTMJUIpITrUWBzGwBAwMBAgAMAwAAAACUNXcAAAAA"  # noqa: E501 pylint: disable=line-too-long
    )

    txn = txlib.Transaction(fee_payer=withdrawer_keypair.public_key)
    txn.recent_blockhash = "Add1tV7kJgNHhTtx3Dgs6dhC7kyXrGJQZ2tJGW15tLDH"

    txn.add(
        vp.withdraw_from_vote_account(
            vp.WithdrawFromVoteAccountParams(
                vote_account_from_pubkey=vote_account_pubkey,
                to_pubkey=receiver_account_pubkey,
                withdrawer=withdrawer_keypair.public_key,
                lamports=2_000_000_000,
            )
        )
    )

    serialized_message = txn.serialize_message()
    assert serialized_message == wire_msg
コード例 #6
0
def keypair_from_file(keyfile_name: str) -> Keypair:
    with open(keyfile_name, 'r') as keyfile:
        data = keyfile.read()
    int_list = json.loads(data)
    bytes_list = [value.to_bytes(1, 'little') for value in int_list]
    return Keypair.from_secret_key(b''.join(bytes_list))
コード例 #7
0
def test_create_nonce_account():
    from_keypair = Keypair.from_secret_key(
        bytes([
            134,
            123,
            27,
            208,
            227,
            175,
            253,
            99,
            4,
            81,
            170,
            231,
            186,
            141,
            177,
            142,
            197,
            139,
            94,
            6,
            157,
            2,
            163,
            89,
            150,
            121,
            235,
            86,
            185,
            22,
            1,
            233,
            58,
            133,
            229,
            39,
            212,
            71,
            254,
            72,
            246,
            45,
            160,
            156,
            129,
            199,
            18,
            189,
            53,
            143,
            98,
            72,
            182,
            106,
            69,
            29,
            38,
            145,
            119,
            190,
            13,
            105,
            157,
            112,
        ]))
    nonce_keypair = Keypair.from_secret_key(
        bytes([
            139,
            81,
            72,
            75,
            252,
            57,
            73,
            247,
            63,
            130,
            201,
            76,
            183,
            43,
            60,
            197,
            65,
            154,
            28,
            240,
            134,
            0,
            232,
            108,
            61,
            123,
            56,
            26,
            35,
            201,
            13,
            39,
            188,
            128,
            179,
            175,
            136,
            5,
            89,
            185,
            92,
            183,
            175,
            131,
            56,
            53,
            228,
            11,
            20,
            34,
            138,
            148,
            51,
            27,
            205,
            76,
            75,
            148,
            184,
            34,
            74,
            129,
            238,
            225,
        ]))

    wire_txn = base64.b64decode(
        b"AtZYPHSaLIQsFnHm4O7Lk0YdQRzovtsp0eKbKRPknDvZINd62tZaLPRzhm6N1LeINLzy31iHY6QE0bGW5c9aegu9g9SQqwsj"
        b"dKfNTYI0JLmzQd98HCUczjMM5H/gvGx+4k+sM/SreWkC3y1X+I1yh4rXehtVW5Sqo5nyyl7z88wOAgADBTqF5SfUR/5I9i2g"
        b"nIHHEr01j2JItmpFHSaRd74NaZ1wvICzr4gFWblct6+DODXkCxQiipQzG81MS5S4IkqB7uEGp9UXGSxWjuCKhF9z0peIzwNc"
        b"MUWyGrNE2AYuqUAAAAan1RcZLFxRIYzJTD1K8X9Y2u4Im6H9ROPb2YoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
        b"AAAAAABXbYHxIfw3Z5Qq1LH8aj6Sj6LuqbCuwFhAmo21XevlfwIEAgABNAAAAACAhB4AAAAAAFAAAAAAAAAAAAAAAAAAAAAA"
        b"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAwECAyQGAAAAOoXlJ9RH/kj2LaCcgccSvTWPYki2akUdJpF3vg1pnXA="
    )
    expected_txn = txlib.Transaction.deserialize(wire_txn)

    create_account_txn = sp.create_nonce_account(
        sp.CreateNonceAccountParams(
            from_pubkey=from_keypair.public_key,
            nonce_pubkey=nonce_keypair.public_key,
            authorized_pubkey=from_keypair.public_key,
            lamports=2000000,
        ))
    create_account_txn.recent_blockhash = "6tHKVLgLBEm25jaDsmatPTfoeHqSobTecJMESteTkPS6"

    create_account_hash = create_account_txn.serialize_message()

    create_account_txn.add_signature(
        from_keypair.public_key,
        from_keypair.sign(create_account_hash).signature)
    create_account_txn.add_signature(
        nonce_keypair.public_key,
        nonce_keypair.sign(create_account_hash).signature)

    assert create_account_txn == expected_txn
コード例 #8
0
def test_advance_nonce_and_transfer():
    from_keypair = Keypair.from_secret_key(
        bytes([
            134,
            123,
            27,
            208,
            227,
            175,
            253,
            99,
            4,
            81,
            170,
            231,
            186,
            141,
            177,
            142,
            197,
            139,
            94,
            6,
            157,
            2,
            163,
            89,
            150,
            121,
            235,
            86,
            185,
            22,
            1,
            233,
            58,
            133,
            229,
            39,
            212,
            71,
            254,
            72,
            246,
            45,
            160,
            156,
            129,
            199,
            18,
            189,
            53,
            143,
            98,
            72,
            182,
            106,
            69,
            29,
            38,
            145,
            119,
            190,
            13,
            105,
            157,
            112,
        ]))
    nonce_keypair = Keypair.from_secret_key(
        bytes([
            139,
            81,
            72,
            75,
            252,
            57,
            73,
            247,
            63,
            130,
            201,
            76,
            183,
            43,
            60,
            197,
            65,
            154,
            28,
            240,
            134,
            0,
            232,
            108,
            61,
            123,
            56,
            26,
            35,
            201,
            13,
            39,
            188,
            128,
            179,
            175,
            136,
            5,
            89,
            185,
            92,
            183,
            175,
            131,
            56,
            53,
            228,
            11,
            20,
            34,
            138,
            148,
            51,
            27,
            205,
            76,
            75,
            148,
            184,
            34,
            74,
            129,
            238,
            225,
        ]))
    to_keypair = Keypair.from_secret_key(
        bytes([
            56,
            246,
            74,
            56,
            168,
            158,
            189,
            97,
            126,
            149,
            175,
            70,
            23,
            14,
            251,
            206,
            172,
            69,
            61,
            247,
            39,
            226,
            8,
            68,
            97,
            159,
            11,
            196,
            212,
            57,
            2,
            1,
            252,
            124,
            54,
            3,
            18,
            109,
            223,
            27,
            225,
            28,
            59,
            202,
            49,
            248,
            244,
            17,
            165,
            33,
            101,
            59,
            217,
            79,
            234,
            217,
            251,
            85,
            9,
            6,
            40,
            0,
            221,
            10,
        ]))

    wire_txn = base64.b64decode(
        b"Abh4hJNaP/IUJlHGpQttaGNWkjOZx71uLEnVpT0SBaedmThsTogjsh87FW+EHeuJrsZii+tJbrq3oJ5UYXPzXwwBAAIFOoXl"
        b"J9RH/kj2LaCcgccSvTWPYki2akUdJpF3vg1pnXC8gLOviAVZuVy3r4M4NeQLFCKKlDMbzUxLlLgiSoHu4fx8NgMSbd8b4Rw7"
        b"yjH49BGlIWU72U/q2ftVCQYoAN0KBqfVFxksVo7gioRfc9KXiM8DXDFFshqzRNgGLqlAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
        b"AAAAAAAAAAAAAAAAAE13Mu8zaQSpG0zzGHpG62nK56DbGhuS4kXMF/ChHY1jAgQDAQMABAQAAAAEAgACDAIAAACAhB4AAAAA"
        b"AA==")

    expected_txn = txlib.Transaction.deserialize(wire_txn)

    txn = txlib.Transaction(fee_payer=from_keypair.public_key)
    txn.recent_blockhash = "6DPp9aRRX6cLBqj5FepEvoccHFs3s8gUhd9t9ftTwAta"

    txn.add(
        sp.nonce_advance(
            sp.AdvanceNonceParams(
                nonce_pubkey=nonce_keypair.public_key,
                authorized_pubkey=from_keypair.public_key,
            )))

    txn.add(
        sp.transfer(
            sp.TransferParams(from_pubkey=from_keypair.public_key,
                              to_pubkey=to_keypair.public_key,
                              lamports=2000000)))

    txn_hash = txn.serialize_message()

    txn.add_signature(from_keypair.public_key,
                      from_keypair.sign(txn_hash).signature)

    assert txn == expected_txn