Created
January 31, 2020 10:25
-
-
Save tobowers/67a5f7f3c230ccfa4e37be986912d829 to your computer and use it in GitHub Desktop.
Multi-step ownership transfer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ts-node | |
import assert from 'assert'; | |
import { MemoryDatastore } from 'interface-datastore'; | |
import { ChainTree, EcdsaKey, Community, Repo, setOwnershipTransaction, Tupelo } from 'tupelo-wasm-sdk'; | |
const getRepo = async () => { | |
const repo = new Repo('test', { | |
lock: 'memory', | |
storageBackends: { | |
root: MemoryDatastore, | |
blocks: MemoryDatastore, | |
keys: MemoryDatastore, | |
datastore: MemoryDatastore | |
} | |
}); | |
await repo.init({}); | |
await repo.open(); | |
return repo; | |
}; | |
const main = async () => { | |
const repo = await getRepo(); | |
const community = await Community.getDefault(repo); | |
// Create a key pair representing Alice | |
const aliceKey = await EcdsaKey.generate(); | |
// Create a ChainTree representing a trading card, owned by Alice | |
const tradingCard = await ChainTree.newEmptyTree(community.blockservice, aliceKey); | |
// Create a key pair representing Bob | |
const bobKey = await EcdsaKey.generate(); | |
// Get the address of Bob's key pair | |
const bobAddress = await Tupelo.ecdsaPubkeyToAddress(bobKey.publicKey); | |
// Create a key pair representing Bob | |
const carolKey = await EcdsaKey.generate(); | |
// Get the address of Bob's key pair | |
const carolAddress = await Tupelo.ecdsaPubkeyToAddress(carolKey.publicKey); | |
console.log( | |
`* Transferring ownership of trading card from Alice to Bob...`, await tradingCard.id() | |
); | |
// Transfer ownership of trading card to Bob | |
await community.playTransactions(tradingCard, [ | |
setOwnershipTransaction([bobAddress]), | |
]); | |
await community.nextUpdate(); | |
const newOwners = (await tradingCard.resolve(["tree/_tupelo/authentications"])).value; | |
assert.deepEqual(newOwners, [bobAddress,]); | |
console.log(`* Transfer successfully made!`); | |
tradingCard.key = bobKey | |
console.log( | |
`* Transferring ownership of trading card from Bob to Carol...`, await tradingCard.id() | |
); | |
// Transfer ownership of trading card to Bob | |
await community.playTransactions(tradingCard, [ | |
setOwnershipTransaction([carolAddress]), | |
]); | |
await community.nextUpdate(); | |
assert.deepEqual((await tradingCard.resolve(["tree/_tupelo/authentications"])).value, [carolAddress,]); | |
console.log(`* Transfer successfully made!`); | |
}; | |
main() | |
.then(() => { | |
process.exit(0) | |
}) | |
.catch((err) => { | |
console.error(err) | |
process.exit(1) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment