-
Notifications
You must be signed in to change notification settings - Fork 5
/
inscribeImage.ts
59 lines (48 loc) · 1.73 KB
/
inscribeImage.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
import { readFileSync, writeFileSync } from 'fs'
import { TestWallet, toByteString, sha256, Addr } from 'scrypt-ts'
import { myAddress, myPrivateKey } from '../utils/privateKey'
import { join } from 'path'
import { HashLockNFT } from '../contracts/hashLockNFT'
import {
ContentType,
OrdiMethodCallOptions,
OrdiNFTP2PKH,
OrdiProvider,
} from '../scrypt-ord'
/**
* @returns mainnet signer
*/
function getSigner() {
return new TestWallet(myPrivateKey, new OrdiProvider())
}
/**
* @returns base64 encoded image binary data
*/
function readImage(): string {
const path = join(__dirname, '..', '..', 'logo.png')
return readFileSync(path).toString('base64')
}
async function main() {
HashLockNFT.loadArtifact('tests/artifacts/contracts/hashLockNFT.json')
// create contract instance
const message = toByteString('Hello sCrypt', true)
const hash = sha256(message)
const hashLock = new HashLockNFT(hash)
await hashLock.connect(getSigner())
// read image data
const image = readImage()
// inscribe image into contract instance
const mintTx = await hashLock.inscribeImage(image, ContentType.PNG)
console.log(`Mint tx: ${mintTx.id}`)
const inscription = hashLock.getInscription().content as Buffer
writeFileSync('inscription.png', inscription)
// for now, the contract instance holds the image inscription
// this inscription can be transferred only when the hash lock is solved
const address = myAddress
const receiver = new OrdiNFTP2PKH(Addr(address.toByteString()))
const { tx: transferTx } = await hashLock.methods.unlock(message, {
transfer: receiver,
} as OrdiMethodCallOptions<HashLockNFT>)
console.log(`Transfer tx: ${transferTx.id}`)
}
main()