-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathspore-maker.ts
73 lines (63 loc) · 2.88 KB
/
spore-maker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { serializeScript } from '@nervosnetwork/ckb-sdk-utils'
import { Collector } from '../src/collector'
import { addressFromP256PrivateKey, append0x, keyFromP256Private } from '../src/utils'
import { Aggregator } from '../src/aggregator'
import { ConnectResponseData } from '@joyid/ckb'
import { CKBAsset, JoyIDConfig } from '../src/types'
import { buildMakerTx } from '../src/order'
import { signSecp256r1Tx } from './secp256r1'
import { calculateNFTMakerListPackage } from '../src/order/maker'
// SECP256R1 private key
const SELLER_MAIN_PRIVATE_KEY = '0x0000000000000000000000000000000000000000000000000000000000000001'
const maker = async () => {
const collector = new Collector({
ckbNodeUrl: 'https://testnet.ckb.dev/rpc',
ckbIndexerUrl: 'https://testnet.ckb.dev/indexer',
})
const seller = addressFromP256PrivateKey(SELLER_MAIN_PRIVATE_KEY)
// ckt1qrfrwcdnvssswdwpn3s9v8fp87emat306ctjwsm3nmlkjg8qyza2cqgqq98mx5lm42zd7mwyq54pg49cln850mj2957np7az
// ckt1qrfrwcdnvssswdwpn3s9v8fp87emat306ctjwsm3nmlkjg8qyza2cqgqqxqyukftmpfang0z2ks6w6syjutass94fujlf09a
console.log('seller address: ', seller)
const aggregator = new Aggregator('https://cota.nervina.dev/aggregator')
// The connectData is the response of the connect with @joyid/ckb
const connectData: ConnectResponseData = {
address: seller,
ethAddress: '',
nostrPubkey: '',
pubkey: '',
keyType: 'main_key',
alg: -7,
taproot: { address: '', pubkey: '' },
nativeSegwit: { address: '', pubkey: '' },
}
// The JoyIDConfig is needed if the dapps use JoyID Wallet to connect and sign ckb transaction
const joyID: JoyIDConfig = {
aggregator,
connectData,
}
// The difference between the capacity occupied by the owner lock and the seller lock and the result may be negative
const listPackage = calculateNFTMakerListPackage(seller)
const totalValue = BigInt(800_0000_0000) + listPackage
const sporeType: CKBComponents.Script = {
codeHash: '0x5e063b4c0e7abeaa6a428df3b693521a3050934cf3b0ae97a800d1bc31449398',
hashType: 'data1',
args: '0x22a0eb5644badac17316e17660bd5535f32665b806b1cbd243bb1dddbcca3bbd',
}
const { rawTx, txFee } = await buildMakerTx({
collector,
joyID,
seller,
// The price whose unit is shannon for CKB native token
totalValue,
assetType: append0x(serializeScript(sporeType)),
ckbAsset: CKBAsset.SPORE,
})
const key = keyFromP256Private(SELLER_MAIN_PRIVATE_KEY)
const signedTx = signSecp256r1Tx(key, rawTx)
// You can call the `signRawTransaction` method to sign the raw tx with JoyID wallet through @joyid/ckb SDK
// please make sure the buyer address is the JoyID wallet ckb address
// const signedTx = await signRawTransaction(rawTx as CKBTransaction, seller)
let txHash = await collector.getCkb().rpc.sendTransaction(signedTx, 'passthrough')
console.info(`The Spore asset has been listed with tx hash: ${txHash}`)
}
maker()