-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into kyle/Secp256k1PublicKey-compatibility
- Loading branch information
Showing
6 changed files
with
179 additions
and
11 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { HttpAgent } from '@dfinity/agent'; | ||
import { Ed25519PublicKey } from '../identity/ed25519'; | ||
import { PartialIdentity } from './partial'; | ||
describe('Partial Identity', () => { | ||
it('should create a partial identity from a public key', async () => { | ||
const key = Ed25519PublicKey.fromRaw(new Uint8Array(32).fill(0)); | ||
const partial = new PartialIdentity(key); | ||
|
||
const agent = new HttpAgent({ identity: partial }); | ||
expect((await agent.getPrincipal()).toText()).toBe( | ||
'deffl-liaaa-aaaaa-aaaaa-aaaaa-aaaaa-aaaaa-aaaaa-aaaaa-aaaaa-aaaaa-aaa', | ||
); | ||
|
||
const rawKey = partial.rawKey; | ||
expect(rawKey).toBeDefined(); | ||
|
||
const derKey = partial.derKey; | ||
expect(derKey).toBeDefined(); | ||
|
||
const toDer = partial.toDer(); | ||
expect(toDer).toBeDefined(); | ||
expect(toDer).toEqual(derKey); | ||
|
||
const publicKey = partial.getPublicKey(); | ||
expect(publicKey).toBeDefined(); | ||
expect(publicKey).toEqual(key); | ||
}); | ||
it('should throw an error when attempting to sign', async () => { | ||
const key = Ed25519PublicKey.fromRaw(new Uint8Array(32).fill(0)); | ||
const partial = new PartialIdentity(key); | ||
await partial.transformRequest().catch(e => { | ||
expect(e).toContain('Not implemented.'); | ||
}); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Identity, PublicKey } from '@dfinity/agent'; | ||
import { Principal } from '@dfinity/principal'; | ||
|
||
/** | ||
* A partial delegated identity, representing a delegation chain and the public key that it targets | ||
*/ | ||
export class PartialIdentity implements Identity { | ||
#inner: PublicKey; | ||
|
||
/** | ||
* The raw public key of this identity. | ||
*/ | ||
get rawKey(): ArrayBuffer | undefined { | ||
return this.#inner.rawKey; | ||
} | ||
|
||
/** | ||
* The DER-encoded public key of this identity. | ||
*/ | ||
get derKey(): ArrayBuffer | undefined { | ||
return this.#inner.derKey; | ||
} | ||
|
||
/** | ||
* The DER-encoded public key of this identity. | ||
*/ | ||
public toDer(): ArrayBuffer { | ||
return this.#inner.toDer(); | ||
} | ||
|
||
/** | ||
* The inner {@link PublicKey} used by this identity. | ||
*/ | ||
public getPublicKey(): PublicKey { | ||
return this.#inner; | ||
} | ||
|
||
/** | ||
* The {@link Principal} of this identity. | ||
*/ | ||
public getPrincipal(): Principal { | ||
return Principal.from(this.#inner.rawKey); | ||
} | ||
|
||
/** | ||
* Required for the Identity interface, but cannot implemented for just a public key. | ||
*/ | ||
public transformRequest(): Promise<never> { | ||
return Promise.reject( | ||
'Not implemented. You are attempting to use a partial identity to sign calls, but this identity only has access to the public key.To sign calls, use a DelegationIdentity instead.', | ||
); | ||
} | ||
|
||
constructor(inner: PublicKey) { | ||
this.#inner = inner; | ||
} | ||
} |