Skip to content

Commit

Permalink
[PlatformVM][Deposit] Rework, Examples, UpgradeVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
knikos committed Jun 28, 2023
1 parent 154494c commit 7f1e0cb
Show file tree
Hide file tree
Showing 22 changed files with 2,116 additions and 62 deletions.
389 changes: 386 additions & 3 deletions e2e_tests/camino/pchain_nomock.test.ts

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions examples/platformvm/buildAddDepositOfferTx-Msig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { Avalanche, BinTools, Buffer } from "caminojs/index"
import {
PlatformVMAPI,
KeyChain,
UnsignedTx,
Tx,
DepositOffer,
OfferFlag,
PlatformVMConstants
} from "caminojs/apis/platformvm"
import { UnixNow, PChainAlias } from "caminojs/utils"
import { ExamplesConfig } from "../common/examplesConfig"
import BN from "bn.js"
import {
MultisigKeyChain,
MultisigKeyPair,
OutputOwners
} from "caminojs/common"
import createHash from "create-hash"

const config: ExamplesConfig = require("../common/examplesConfig.json")
const avalanche: Avalanche = new Avalanche(
config.host,
config.port,
config.protocol,
config.networkID
)
const bintools = BinTools.getInstance()
const multiSigAliasMember1PrivateKey =
"PrivateKey-2Vtf2ZhTRz6WcVcSH7cS7ghKneZxZ2L5W8assdCcaNDVdpoYfY" // P-kopernikus1jla8ty5c9ud6lsj8s4re2dvzvfxpzrxdcrd8q7
const msigAlias = "P-kopernikus1fwrv3kj5jqntuucw67lzgu9a9tkqyczxgcvpst"
const depositOwner = "P-kopernikus13kyf72ftu4l77kss7xm0kshm0au29s48zjaygq"

const depositOwnerPrivateKey =
"PrivateKey-Ge71NJhUY3TjZ9dLohijSnNq46QxobjqxHGMUDAPoVsNFA93w"
let pchain: PlatformVMAPI
let pKeychain: KeyChain
let pAddresses: Buffer[]
let pAddressStrings: string[]

const InitAvalanche = async () => {
await avalanche.fetchNetworkSettings()
pchain = avalanche.PChain()
pKeychain = pchain.keyChain()
pKeychain.importKey(multiSigAliasMember1PrivateKey)
pKeychain.importKey(depositOwnerPrivateKey)

pAddresses = pchain.keyChain().getAddresses()
pAddressStrings = pchain.keyChain().getAddressStrings()
}

const main = async (): Promise<any> => {
await InitAvalanche()

const msigAliasBuffer = pchain.parseAddress(msigAlias)
const owner = await pchain.getMultisigAlias(msigAlias)
const startTime = UnixNow().add(new BN(600 * 1))
const endTime: BN = startTime.add(new BN(26300000))

const depositOfferMsigCreator = msigAlias // Warning: this address must have the role OFFERS_CREATOR
const depositOfferMsigCreatorAuth: [number, string | Buffer][] = [
[0, depositOfferMsigCreator]
]
const offer: DepositOffer = {
upgradeVersion: 1,
id: undefined,
interestRateNominator: new BN(1000000000),
start: startTime,
end: endTime,
minAmount: new BN(1000000), // min deposit amount = 1 milliAvax
totalMaxAmount: undefined,
depositedAmount: undefined,
minDuration: 60,
maxDuration: 360,
unlockPeriodDuration: 20,
noRewardsPeriodDuration: 10,
memo: "post-genesis-deposit-offer",
flags: new BN(OfferFlag.NONE),
totalMaxRewardAmount: new BN(1000000),
rewardedAmount: new BN(0),
ownerAddress: depositOwner
}
const unsignedTx: UnsignedTx = await pchain.buildAddDepositOfferTx(
undefined,
[[depositOfferMsigCreator], pAddressStrings],
[depositOfferMsigCreator],
offer,
depositOfferMsigCreator,
depositOfferMsigCreatorAuth,
Buffer.from("new-deposit-offer")
)

// Create the hash from the tx
const txbuff = unsignedTx.toBuffer()
const msg: Buffer = Buffer.from(createHash("sha256").update(txbuff).digest())

// create MSKeychain to create proper sigindx
const msKeyChain = new MultisigKeyChain(
avalanche.getHRP(),
PChainAlias,
msg,
PlatformVMConstants.SECPMULTISIGCREDENTIAL,
unsignedTx.getTransaction().getOutputOwners(),
new Map([
[
msigAliasBuffer.toString("hex"),
new OutputOwners(
owner.addresses.map((a) => bintools.parseAddress(a, "P")),
new BN(owner.locktime),
owner.threshold
)
]
])
)

for (let address of pAddresses) {
// We need the keychain for signing
const keyPair = pKeychain.getKey(address)
// The signature
const signature = keyPair.sign(msg)
// add the signature
msKeyChain.addKey(new MultisigKeyPair(msKeyChain, address, signature))
}

msKeyChain.buildSignatureIndices()
const tx: Tx = unsignedTx.sign(msKeyChain)
const txid: string = await pchain.issueTx(tx)
console.log(`Success! TXID: ${txid}`)
}

main()
94 changes: 94 additions & 0 deletions examples/platformvm/buildAddDepositOfferTx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { Avalanche, Buffer } from "caminojs/index"
import {
PlatformVMAPI,
KeyChain,
UnsignedTx,
Tx,
DepositOffer,
OfferFlag
} from "caminojs/apis/platformvm"
import {
PrivateKeyPrefix,
DefaultLocalGenesisPrivateKey,
UnixNow
} from "caminojs/utils"
import { ExamplesConfig } from "../common/examplesConfig"
import BN from "bn.js"

const config: ExamplesConfig = require("../common/examplesConfig.json")
const avalanche: Avalanche = new Avalanche(
config.host,
config.port,
config.protocol,
config.networkID
)

/**
* @ignore
*/
let privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}`
const ownerPrivKey: string =
"PrivateKey-Ge71NJhUY3TjZ9dLohijSnNq46QxobjqxHGMUDAPoVsNFA93w"
let pchain: PlatformVMAPI
let pKeychain: KeyChain
let pAddresses: Buffer[]
let pAddressStrings: string[]

const InitAvalanche = async () => {
await avalanche.fetchNetworkSettings()
pchain = avalanche.PChain()
pKeychain = pchain.keyChain()
// P-local18jma8ppw3nhx5r4ap8clazz0dps7rv5u9xde7p
pKeychain.importKey(privKey)
// P-kopernikus13kyf72ftu4l77kss7xm0kshm0au29s48zjaygq
pKeychain.importKey(ownerPrivKey)

pAddresses = pchain.keyChain().getAddresses()
pAddressStrings = pchain.keyChain().getAddressStrings()
}

const main = async (): Promise<any> => {
await InitAvalanche()
const startTime = UnixNow().add(new BN(600 * 1))
const endTime: BN = startTime.add(new BN(26300000))

const depositOfferCreator = pAddressStrings[0] // Warning: this address must have the role OFFERS_CREATOR
const depositOfferCreatorAuth: [number, string | Buffer][] = [
[0, depositOfferCreator]
]
const depositOwner = pAddressStrings[1]
const offer: DepositOffer = {
upgradeVersion: 1,
id: undefined,
interestRateNominator: new BN(1000000000),
start: startTime,
end: endTime,
minAmount: new BN(1000000), // min deposit amount = 1 milliAvax
totalMaxAmount: undefined,
depositedAmount: undefined,
minDuration: 60,
maxDuration: 360,
unlockPeriodDuration: 20,
noRewardsPeriodDuration: 10,
memo: "post-genesis-deposit-offer",
flags: new BN(OfferFlag.NONE),
totalMaxRewardAmount: new BN(1000000),
rewardedAmount: new BN(0),
ownerAddress: depositOwner
}
const unsignedTx: UnsignedTx = await pchain.buildAddDepositOfferTx(
undefined,
[depositOfferCreator],
[depositOfferCreator],
offer,
depositOfferCreator,
depositOfferCreatorAuth,
Buffer.from("new-deposit-offer")
)

const tx: Tx = unsignedTx.sign(pKeychain)
const txid: string = await pchain.issueTx(tx)
console.log(`Success! TXID: ${txid}`)
}

main()
7 changes: 4 additions & 3 deletions examples/platformvm/buildAddressStateTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
PlatformVMAPI,
KeyChain,
UnsignedTx,
Tx
Tx,
AddressState
} from "caminojs/apis/platformvm"
import { ExamplesConfig } from "../common/examplesConfig"

Expand All @@ -29,7 +30,7 @@ const InitAvalanche = async () => {
await avalanche.fetchNetworkSettings()
pchain = avalanche.PChain()
pKeychain = pchain.keyChain()
// P-local18jma8ppw3nhx5r4ap8clazz0dps7rv5u9xde7p
// X-kopernikus1g65uqn6t77p656w64023nh8nd9updzmxh8ttv3
pKeychain.importKey(privKey)

pAddressStrings = pchain.keyChain().getAddressStrings()
Expand All @@ -39,7 +40,7 @@ const main = async (): Promise<any> => {
await InitAvalanche()

const address = pAddressStrings[0]
const state = 1 // AddressStateRoleKyc
const state = AddressState.ROLE_KYC
const remove = false
const memo: Buffer = Buffer.from(
"Utility function to create an AddressStateTx transaction"
Expand Down
103 changes: 103 additions & 0 deletions examples/platformvm/buildDepositTx-v1-MsigOwner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { Avalanche, BinTools, Buffer } from "caminojs/index"
import {
PlatformVMAPI,
KeyChain,
UnsignedTx,
Tx
} from "caminojs/apis/platformvm"
import { OutputOwners } from "caminojs/common/output"
import { PrivateKeyPrefix, DefaultLocalGenesisPrivateKey } from "caminojs/utils"
import { ExamplesConfig } from "../common/examplesConfig"
import BN from "bn.js"
import createHash from "create-hash"
import { SignerKeyPair } from "caminojs/common"

const config: ExamplesConfig = require("../common/examplesConfig.json")
const avalanche: Avalanche = new Avalanche(
config.host,
config.port,
config.protocol,
config.networkID
)

const msigAlias = "P-kopernikus1fwrv3kj5jqntuucw67lzgu9a9tkqyczxgcvpst"
const multiSigAliasMember1PrivateKey =
"PrivateKey-2Vtf2ZhTRz6WcVcSH7cS7ghKneZxZ2L5W8assdCcaNDVdpoYfY" // P-kopernikus1jla8ty5c9ud6lsj8s4re2dvzvfxpzrxdcrd8q7

const bintools: BinTools = BinTools.getInstance()
const privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}`
let pchain: PlatformVMAPI
let pKeychain: KeyChain
let pAddressStrings: string[]

const InitAvalanche = async () => {
await avalanche.fetchNetworkSettings()
pchain = avalanche.PChain()
pKeychain = pchain.keyChain()
// P-local18jma8ppw3nhx5r4ap8clazz0dps7rv5u9xde7p
pKeychain.importKey(privKey)
pKeychain.importKey(multiSigAliasMember1PrivateKey)
pAddressStrings = pchain.keyChain().getAddressStrings()
}

const main = async (): Promise<any> => {
await InitAvalanche()
const depositOwner = msigAlias
const msigAliasBuffer = pchain.parseAddress(msigAlias)
const amountToLock = new BN(10000000)
const depositOfferID = "27pLgKVoXqtSWqGtBTtGkVKmZUM3MYTsjDjNzuLEr9gWiVGXy3"
const depositDuration = 110
const memo: Buffer = Buffer.from("DepositTx v1 with msig deposit offer owner")
const owners = new OutputOwners(
pchain.keyChain().getAddresses(),
undefined,
1
)

const depositOfferCreator = pAddressStrings[0] // Warning: this address must have the role OFFERS_CREATOR
const depositOfferCreatorAuth: [number, string | Buffer][] = [
[0, depositOfferCreator]
]

const depositOfferOwnerAuth: [number, string | Buffer][] = [[0, depositOwner]]

// hash concatenated bytes of offer id and deposit owner address
const msgHashed: Buffer = Buffer.from(
createHash("sha256")
.update(
Buffer.concat([
bintools.cb58Decode(depositOfferID),
pchain.parseAddress(depositOfferCreator)
])
)
.digest()
)
const keypair: SignerKeyPair = pKeychain.getKey(
pKeychain.getAddresses()[1] // msig member 1
)
// sign the hash
const signatureBuffer: Buffer = keypair.sign(msgHashed)

const unsignedTx: UnsignedTx = await pchain.buildDepositTx(
1,
undefined,
pAddressStrings,
pAddressStrings,
depositOfferID,
depositDuration,
owners,
depositOfferCreator,
depositOfferCreatorAuth,
[signatureBuffer],
depositOfferOwnerAuth,
memo,
new BN(0),
amountToLock
)

const tx: Tx = unsignedTx.sign(pKeychain)
const txid: string = await pchain.issueTx(tx)
console.log(`Success! TXID: ${txid}`)
}

main()
4 changes: 4 additions & 0 deletions examples/platformvm/buildDepositTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ const main = async (): Promise<any> => {
depositOfferID,
depositDuration,
owners,
Buffer.alloc(20),
[],
[],
[],
memo,
new BN(0),
amountToLock
Expand Down
Loading

0 comments on commit 7f1e0cb

Please sign in to comment.