我运行Node项目十之八九都是编译不通过的(所以,不好测试):
所以在此针对 RawProvider简单参与一下。抛砖引玉~
1、更新pw-core/src/hashers/blake2b-hasher.ts的构造函数,以支持传入私钥,其它不变:
constructor(key: string = null) {
const keyData = !key || key.length === 0 ? null : Buffer.from(key);
const h = blake2b(
32,
keyData,
null,
new Uint8Array(Reader.fromRawString('ckb-default-hash').toArrayBuffer())
);
super(h);
}
gist: https://gist.github.com/taurenshaman/86f96ae81a3f26188bffa735218a70c0
2、增加文件:pw-core/src/signers/blake2b-signer.ts
import { Signer, Message } from '.';
import { Blake2bHasher } from '../hashers';
import { Provider } from '../providers';
export class Blake2bSigner extends Signer {
constructor(public readonly provider: Provider, key: string = null) {
super(new Blake2bHasher(key));
}
signMessage(message: string): Promise<string>{
return this.provider.sign(message);
}
async signMessages(messages: Message[]): Promise<string[]> {
const sigs = [];
for (const message of messages) {
if (
this.provider.address.toLockScript().toHash() === message.lock.toHash()
) {
sigs.push(await this.provider.sign(message.message));
} else {
sigs.push('0x');
}
}
return sigs;
}
}
gist: https://gist.github.com/taurenshaman/ed4dfd5019ade10e78a2dbacf24ac955
3、增加文件:pw-core/src/providers/raw-provider.ts,基于DummyProvider修改:
import { Provider, Platform } from './provider';
import { Address, AddressType } from '..';
import { Blake2bSigner } from '../signers/blake2b-signer';
export class RawProvider extends Provider {
secret: string;
signer: Blake2bSigner;
sign(message: string): Promise<string> {
console.log('message', message);
return this.signer.signMessage(message);
}
constructor(secret: string, platform: Platform = Platform.ckb) {
super(platform);
this.secret = secret;
this.signer = new Blake2bSigner(this, secret);
}
async init(): Promise<Provider> {
if (this.platform === Platform.eth) {
this.address = new Address(
'0x26C5F390FF2033CbB44377361c63A3Dd2DE3121d',
AddressType.eth
);
} else {
this.address = new Address(
'ckt1qyqxpayn272n8km2k08hzldynj992egs0waqnr8zjs',
AddressType.ckb
);
}
return this;
}
async close() {
console.log('do nothing');
}
}
gist: https://gist.github.com/taurenshaman/de4cb3ce973ac53799723124cb1b703b