Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example of P-chain partial signing #3325

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions wallet/subnet/primary/examples/p-partial-signing/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package main

import (
"context"
"encoding/json"
"log"

"github.com/ava-labs/avalanchego/genesis"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/crypto/keychain"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/platformvm"
"github.com/ava-labs/avalanchego/vms/platformvm/fx"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/wallet/chain/p/builder"
"github.com/ava-labs/avalanchego/wallet/chain/p/signer"
"github.com/ava-labs/avalanchego/wallet/chain/p/wallet"
"github.com/ava-labs/avalanchego/wallet/subnet/primary"
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
)

func makeWallet(
ctx context.Context,
uri string,
addresses set.Set[ids.ShortID],
keychains ...keychain.Keychain,
) (builder.Builder, []signer.Signer, error) {
state, err := primary.FetchState(ctx, uri, addresses)
if err != nil {
return nil, nil, err
}

utxos := common.NewChainUTXOs(constants.PlatformChainID, state.UTXOs)
backend := wallet.NewBackend(state.PCTX, utxos, make(map[ids.ID]fx.Owner))
builder := builder.New(addresses, state.PCTX, backend)

signers := make([]signer.Signer, len(keychains))
for i, keychain := range keychains {
signers[i] = signer.New(keychain, backend)
}
return builder, signers, nil
}

func main() {
ctx := context.Background()
builder, signers, err := makeWallet(
ctx,
primary.LocalAPIURI,
set.Of(
genesis.EWOQKey.Address(),
genesis.VMRQKey.Address(),
),
secp256k1fx.NewKeychain(genesis.EWOQKey),
secp256k1fx.NewKeychain(genesis.VMRQKey),
)
if err != nil {
log.Fatal(err)
}

pContext := builder.Context()
unsignedTx, err := builder.NewBaseTx([]*avax.TransferableOutput{
{
Asset: avax.Asset{ID: pContext.AVAXAssetID},
Out: &secp256k1fx.TransferOutput{
Amt: 27 * units.MegaAvax,
OutputOwners: secp256k1fx.OutputOwners{
Threshold: 2,
Addrs: []ids.ShortID{
genesis.EWOQKey.Address(),
genesis.VMRQKey.Address(),
},
},
},
},
})
if err != nil {
log.Fatal(err)
}

signedTx := &txs.Tx{Unsigned: unsignedTx}
for _, signer := range signers {
if err := signer.Sign(ctx, signedTx); err != nil {
log.Fatal(err)
}
}
Comment on lines +87 to +92
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these signers aren't related to each other, this transaction could be passed around to untrusting parties to sign (the keys do not need to be co-located).


client := platformvm.NewClient(primary.LocalAPIURI)
txID, err := client.IssueTx(ctx, signedTx.Bytes())
if err != nil {
log.Fatal(err)
}

txJSON, err := json.MarshalIndent(signedTx, "", "\t")
if err != nil {
log.Fatal(err)
}
log.Printf("Issued transaction %s\n%s", txID, string(txJSON))
}
Loading